Merge pull request #135 from kvj/suspend-in-goroutine - fixes #134

Block main event loop with mutex during Suspend() call
pull/139/head
rivo 6 years ago committed by GitHub
commit 6b270eb8e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -41,8 +41,8 @@ type Application struct {
// was drawn. // was drawn.
afterDraw func(screen tcell.Screen) afterDraw func(screen tcell.Screen)
// If this value is true, the application has entered suspended mode. // Halts the event loop during suspended mode
suspended bool suspendMutex sync.Mutex
} }
// NewApplication creates and returns a new application. // NewApplication creates and returns a new application.
@ -103,28 +103,20 @@ func (a *Application) Run() error {
// Start event loop. // Start event loop.
for { for {
// Do not poll events during suspend mode
a.suspendMutex.Lock()
a.Lock() a.Lock()
screen := a.screen screen := a.screen
if a.suspended {
a.suspended = false // Clear previous suspended flag.
}
a.Unlock() a.Unlock()
if screen == nil { if screen == nil {
a.suspendMutex.Unlock()
break break
} }
// Wait for next event. // Wait for next event.
event := a.screen.PollEvent() event := a.screen.PollEvent()
a.suspendMutex.Unlock()
if event == nil { if event == nil {
a.Lock()
if a.suspended {
// This screen was renewed due to suspended mode.
a.suspended = false
a.Unlock()
continue // Resume.
}
a.Unlock()
// The screen was finalized. Exit the loop. // The screen was finalized. Exit the loop.
break break
} }
@ -190,14 +182,15 @@ func (a *Application) Stop() {
func (a *Application) Suspend(f func()) bool { func (a *Application) Suspend(f func()) bool {
a.Lock() a.Lock()
if a.suspended || a.screen == nil { if a.screen == nil {
// Application is already suspended. // Screen has not yet been initialized
a.Unlock() a.Unlock()
return false return false
} }
// Enter suspended mode. // Enter suspended mode.
a.suspended = true a.suspendMutex.Lock()
defer a.suspendMutex.Unlock()
a.Unlock() a.Unlock()
a.Stop() a.Stop()

Loading…
Cancel
Save