Add mutex to ncurses output to avoid race condition at exit

This commit is contained in:
tkarrass 2016-02-06 12:06:50 +01:00
parent 8e991054e7
commit feb5b51e51
2 changed files with 26 additions and 10 deletions

View File

@ -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 {

View File

@ -4,30 +4,38 @@ import (
"fmt"
"log"
"os"
"sync"
nc "github.com/rthornton128/goncurses"
)
var (
win *nc.Window
ncquit = make(chan bool)
win *nc.Window
ncquit = make(chan bool)
winMutex = &sync.Mutex{}
)
func RenderFrame(f *Frame) {
win.Clear()
for k, _ := range f.Data {
win.MovePrint(k+1, 1, string(f.Data[k]))
winMutex.Lock()
if win != nil {
win.Clear()
for k, _ := range f.Data {
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 {
nc.End()
ncquit <- true
// }
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)