Add mutex to ncurses output to avoid race condition at exit
This commit is contained in:
parent
8e991054e7
commit
feb5b51e51
|
@ -81,6 +81,9 @@ func runClient(cmd *cobra.Command, args []string) {
|
|||
select {
|
||||
case <-c:
|
||||
// funzt noch nicht:
|
||||
log.Println("Got a KILL")
|
||||
con.Close()
|
||||
close(renderQueue)
|
||||
run = false
|
||||
default:
|
||||
if data.GetChar() != 0 {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
nc "github.com/rthornton128/goncurses"
|
||||
)
|
||||
|
@ -11,23 +12,30 @@ import (
|
|||
var (
|
||||
win *nc.Window
|
||||
ncquit = make(chan bool)
|
||||
winMutex = &sync.Mutex{}
|
||||
)
|
||||
|
||||
func RenderFrame(f *Frame) {
|
||||
winMutex.Lock()
|
||||
if win != nil {
|
||||
win.Clear()
|
||||
for k, _ := range f.Data {
|
||||
win.MovePrint(k+1, 1, string(f.Data[k]))
|
||||
win.MovePrint(k, 0, string(f.Data[k]))
|
||||
}
|
||||
}
|
||||
winMutex.Unlock()
|
||||
}
|
||||
|
||||
func InitNC(killchan chan<- os.Signal) {
|
||||
var err error
|
||||
winMutex.Lock()
|
||||
win, err = nc.Init()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
win.Timeout(1)
|
||||
winMutex.Unlock()
|
||||
go func() {
|
||||
select {
|
||||
case <-ncquit:
|
||||
|
@ -38,13 +46,18 @@ func InitNC(killchan chan<- os.Signal) {
|
|||
}
|
||||
|
||||
func ExitNC() {
|
||||
//if win != nil {
|
||||
winMutex.Lock()
|
||||
if win != nil {
|
||||
win = nil
|
||||
nc.End()
|
||||
ncquit <- true
|
||||
// }
|
||||
}
|
||||
winMutex.Unlock()
|
||||
}
|
||||
|
||||
func GetChar() int {
|
||||
winMutex.Lock()
|
||||
defer winMutex.Unlock()
|
||||
if win != nil {
|
||||
k := win.GetChar()
|
||||
return int(k)
|
||||
|
@ -52,7 +65,7 @@ func GetChar() int {
|
|||
return 0
|
||||
}
|
||||
|
||||
func TestNC() (int, int) {
|
||||
func testNC() (int, int) {
|
||||
win, err := nc.Init()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
Loading…
Reference in New Issue
Block a user