Skip to content

Commit

Permalink
driver/glfw: Merge waitForStart and done channels
Browse files Browse the repository at this point in the history
With the threading changes, we no longer need to have separate
channels for done and waitForStart given that showing a window no
longer is expected to happen on the wrong thread. This allows
allows us to first wait for starting and then reopen the channel
to wait for shutting down.
  • Loading branch information
Jacalz committed Dec 17, 2024
1 parent 5ee9979 commit ae2fe1b
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 18 deletions.
12 changes: 5 additions & 7 deletions internal/driver/glfw/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ var curWindow *window
var _ fyne.Driver = (*gLDriver)(nil)

type gLDriver struct {
windowLock sync.RWMutex
windows []fyne.Window
done chan struct{}
waitForStart chan struct{}
windowLock sync.RWMutex
windows []fyne.Window
waitForTransition chan struct{} // wait for transitioning first from starting and later on to quiting.

animation animation.Runner

Expand Down Expand Up @@ -99,7 +98,7 @@ func (d *gLDriver) Quit() {

// Only call close once to avoid panic.
if running.CompareAndSwap(true, false) {
close(d.done)
close(d.waitForTransition)
}
}

Expand Down Expand Up @@ -174,7 +173,6 @@ func NewGLDriver() *gLDriver {
repository.Register("file", intRepo.NewFileRepository())

return &gLDriver{
done: make(chan struct{}),
waitForStart: make(chan struct{}),
waitForTransition: make(chan struct{}),
}
}
4 changes: 0 additions & 4 deletions internal/driver/glfw/glfw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ func ensureCanvasSize(t *testing.T, w *window, size fyne.Size) {
}

func repaintWindow(w *window) {
// Wait for GLFW loop to be running.
// If we try to paint windows before the context is created, we will end up on the wrong thread.
<-w.driver.waitForStart

runOnMainWithContext(w, func() {
d.repaintWindow(w)
})
Expand Down
6 changes: 4 additions & 2 deletions internal/driver/glfw/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func (d *gLDriver) runGL() {
if !running.CompareAndSwap(false, true) {
return // Run was called twice.
}
close(d.waitForStart) // Signal that execution can continue.

close(d.waitForTransition) // Signal that execution can continue.
d.waitForTransition = make(chan struct{}) // Prepare for transitioning into done.

d.initGLFW()
if d.trayStart != nil {
Expand All @@ -109,7 +111,7 @@ func (d *gLDriver) runGL() {
eventTick := time.NewTicker(time.Second / 60)
for {
select {
case <-d.done:
case <-d.waitForTransition:
eventTick.Stop()
d.Terminate()
l := fyne.CurrentApp().Lifecycle().(*app.Lifecycle)
Expand Down
2 changes: 0 additions & 2 deletions internal/driver/glfw/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ func (w *window) doShow() {
return
}

<-w.driver.waitForStart

w.createLock.Do(w.create)
if w.view() == nil {
return
Expand Down
3 changes: 0 additions & 3 deletions internal/driver/glfw/window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ func init() {
func TestMain(m *testing.M) {
d.initGLFW()
go func() {
// Wait for GLFW loop to be running.
// If we try to create windows before the context is created, this will fail with an exception.
<-d.waitForStart

initMainMenu()
os.Exit(m.Run())
Expand Down

0 comments on commit ae2fe1b

Please sign in to comment.