Skip to content

Commit

Permalink
Refactor the goroutine code to internal helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jan 6, 2025
1 parent 220f6c6 commit a35749c
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 64 deletions.
22 changes: 2 additions & 20 deletions data/binding/queue.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
package binding

import (
"runtime"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/async"
)

var mainGoroutineID uint64

func init() {
runtime.LockOSThread()
mainGoroutineID = goroutineID()
}

func goroutineID() (id uint64) {
var buf [30]byte
runtime.Stack(buf[:], false)
for i := 10; buf[i] != ' '; i++ {
id = id*10 + uint64(buf[i]&15)
}

return id
}

func queueItem(f func()) {
if goroutineID() == mainGoroutineID {
if async.IsMainGoroutine() {
f()
return
}
Expand Down
11 changes: 11 additions & 0 deletions internal/async/goroutine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package async

var mainGoroutineID uint64

func init() {
mainGoroutineID = goroutineID()
}

func IsMainGoroutine() bool {
return goroutineID() == mainGoroutineID
}
15 changes: 15 additions & 0 deletions internal/async/goroutine_desktop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !wasm

package async

import "runtime"

func goroutineID() (id uint64) {
var buf [30]byte
runtime.Stack(buf[:], false)
for i := 10; buf[i] != ' '; i++ {
id = id*10 + uint64(buf[i]&15)
}

return id
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build wasm

package glfw
package async

func goroutineID() uint64 {
return mainGoroutineID
Expand Down
39 changes: 39 additions & 0 deletions internal/async/goroutine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package async

import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

var mainRoutineID uint64

func init() {
mainRoutineID = goroutineID()
}

func TestGoroutineID(t *testing.T) {
assert.Equal(t, uint64(1), mainRoutineID)

var childID1, childID2 uint64
testID1 := goroutineID()
var wg sync.WaitGroup
wg.Add(2)
go func() {
childID1 = goroutineID()
wg.Done()
}()
go func() {
childID2 = goroutineID()
wg.Done()
}()
wg.Wait()
testID2 := goroutineID()

assert.Equal(t, testID1, testID2)
assert.Greater(t, childID1, uint64(0))
assert.NotEqual(t, testID1, childID1)
assert.Greater(t, childID2, uint64(0))
assert.NotEqual(t, childID1, childID2)
}
3 changes: 2 additions & 1 deletion internal/driver/glfw/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"sync"

"fyne.io/fyne/v2/internal/async"
"github.com/fyne-io/image/ico"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -156,7 +157,7 @@ func (d *gLDriver) initFailed(msg string, err error) {
}

func (d *gLDriver) Run() {
if goroutineID() != mainGoroutineID {
if !async.IsMainGoroutine() {
panic("Run() or ShowAndRun() must be called from main goroutine")
}

Expand Down
9 changes: 0 additions & 9 deletions internal/driver/glfw/driver_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ var (
setup sync.Once
)

func goroutineID() (id uint64) {
var buf [30]byte
runtime.Stack(buf[:], false)
for i := 10; buf[i] != ' '; i++ {
id = id*10 + uint64(buf[i]&15)
}
return id
}

func (d *gLDriver) SetSystemTrayMenu(m *fyne.Menu) {
setup.Do(func() {
d.trayStart, d.trayStop = systray.RunWithExternalLoop(func() {
Expand Down
32 changes: 0 additions & 32 deletions internal/driver/glfw/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package glfw

import (
"sync"
"testing"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -122,34 +121,3 @@ func Test_gLDriver_AbsolutePositionForObject(t *testing.T) {
})
}
}

var mainRoutineID uint64

func init() {
mainRoutineID = goroutineID()
}

func TestGoroutineID(t *testing.T) {
assert.Equal(t, uint64(1), mainRoutineID)

var childID1, childID2 uint64
testID1 := goroutineID()
var wg sync.WaitGroup
wg.Add(2)
go func() {
childID1 = goroutineID()
wg.Done()
}()
go func() {
childID2 = goroutineID()
wg.Done()
}()
wg.Wait()
testID2 := goroutineID()

assert.Equal(t, testID1, testID2)
assert.Greater(t, childID1, uint64(0))
assert.NotEqual(t, testID1, childID1)
assert.Greater(t, childID2, uint64(0))
assert.NotEqual(t, childID1, childID2)
}
1 change: 0 additions & 1 deletion internal/driver/glfw/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ var initOnce = &sync.Once{}
// Arrange that main.main runs on main thread.
func init() {
runtime.LockOSThread()
mainGoroutineID = goroutineID()
}

// force a function f to run on the main thread
Expand Down

0 comments on commit a35749c

Please sign in to comment.