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 {
|
select {
|
||||||
case <-c:
|
case <-c:
|
||||||
// funzt noch nicht:
|
// funzt noch nicht:
|
||||||
|
log.Println("Got a KILL")
|
||||||
|
con.Close()
|
||||||
|
close(renderQueue)
|
||||||
run = false
|
run = false
|
||||||
default:
|
default:
|
||||||
if data.GetChar() != 0 {
|
if data.GetChar() != 0 {
|
||||||
|
|
|
@ -4,30 +4,38 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
nc "github.com/rthornton128/goncurses"
|
nc "github.com/rthornton128/goncurses"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
win *nc.Window
|
win *nc.Window
|
||||||
ncquit = make(chan bool)
|
ncquit = make(chan bool)
|
||||||
|
winMutex = &sync.Mutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func RenderFrame(f *Frame) {
|
func RenderFrame(f *Frame) {
|
||||||
win.Clear()
|
winMutex.Lock()
|
||||||
for k, _ := range f.Data {
|
if win != nil {
|
||||||
win.MovePrint(k+1, 1, string(f.Data[k]))
|
win.Clear()
|
||||||
|
for k, _ := range f.Data {
|
||||||
|
win.MovePrint(k, 0, string(f.Data[k]))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
winMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitNC(killchan chan<- os.Signal) {
|
func InitNC(killchan chan<- os.Signal) {
|
||||||
var err error
|
var err error
|
||||||
|
winMutex.Lock()
|
||||||
win, err = nc.Init()
|
win, err = nc.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
win.Timeout(1)
|
win.Timeout(1)
|
||||||
|
winMutex.Unlock()
|
||||||
go func() {
|
go func() {
|
||||||
select {
|
select {
|
||||||
case <-ncquit:
|
case <-ncquit:
|
||||||
|
@ -38,13 +46,18 @@ func InitNC(killchan chan<- os.Signal) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExitNC() {
|
func ExitNC() {
|
||||||
//if win != nil {
|
winMutex.Lock()
|
||||||
nc.End()
|
if win != nil {
|
||||||
ncquit <- true
|
win = nil
|
||||||
// }
|
nc.End()
|
||||||
|
ncquit <- true
|
||||||
|
}
|
||||||
|
winMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetChar() int {
|
func GetChar() int {
|
||||||
|
winMutex.Lock()
|
||||||
|
defer winMutex.Unlock()
|
||||||
if win != nil {
|
if win != nil {
|
||||||
k := win.GetChar()
|
k := win.GetChar()
|
||||||
return int(k)
|
return int(k)
|
||||||
|
@ -52,7 +65,7 @@ func GetChar() int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNC() (int, int) {
|
func testNC() (int, int) {
|
||||||
win, err := nc.Init()
|
win, err := nc.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user