Skip to content

Block main event loop with mutex during Suspend() call#135

Merged
rivo merged 2 commits intorivo:masterfrom
kvj:suspend-in-goroutine
Jun 28, 2018
Merged

Block main event loop with mutex during Suspend() call#135
rivo merged 2 commits intorivo:masterfrom
kvj:suspend-in-goroutine

Conversation

@kvj
Copy link
Copy Markdown
Contributor

@kvj kvj commented Jun 20, 2018

Fixes #134

Please take a look on it.
This replaces suspended bool flag with dedicated mutex, which simplifies code and fixes above mentioned bug

@kvj kvj force-pushed the suspend-in-goroutine branch from dcfc6de to 2ac0e9e Compare June 20, 2018 15:04
Copy link
Copy Markdown
Owner

@rivo rivo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I gave a few comments and requested a few changes.

Comment thread application.go Outdated

// If this value is true, the application has entered suspended mode.
suspended bool
// Used for events loop lock during Suspend()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a comment such as Halts the loop during suspended mode . is better here.

Comment thread application.go

// Wait for next event.
event := a.screen.PollEvent()
a.suspendMutex.Unlock()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it make sense to wrap the entire for block with the lock? It seems to me that the code after this Unlock() could also potentially interfere with suspended mode.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, in this case there will be a deadlock, if Suspend() will be called from inputCapture() handler (same goroutine)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. That's true.

Comment thread application.go Outdated

if a.suspended || a.screen == nil {
if a.screen == nil {
// Application is already suspended.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is now not correct anymore. It should probably read Screen has not yet been initialized..

Comment thread application.go Outdated
var err error
a.screen, err = tcell.NewScreen()
if err != nil {
a.suspendMutex.Unlock()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of putting Unlock()'s everywhere, a defer Unlock() at the top is probably better.

@kvj
Copy link
Copy Markdown
Contributor Author

kvj commented Jun 20, 2018

@rivo thanks for the feedback. I've updated PR

@rivo rivo merged commit 6b270eb into rivo:master Jun 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Suspend() doesn't work from goroutine

2 participants