Skip to content

Commit

Permalink
Fixup getting the right main thread ID for mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jan 24, 2025
1 parent 22234be commit b450374
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 3 additions & 4 deletions internal/async/goroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
)

// mainGoroutineID stores the main goroutine ID.
// This ID must be initialized in main.init because
// a main goroutine may not equal to 1 due to the
// influence of a garbage collector.
// This ID must be initialized during setup by calling `SetMainGoroutine` because
// a main goroutine may not equal to 1 due to the influence of a garbage collector.
var mainGoroutineID uint64

func init() {
func SetMainGoroutine() {
mainGoroutineID = goroutineID()
}

Expand Down
1 change: 1 addition & 0 deletions internal/driver/glfw/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var initOnce = &sync.Once{}
// Arrange that main.main runs on main thread.
func init() {
runtime.LockOSThread()
async.SetMainGoroutine()
}

// force a function f to run on the main thread
Expand Down
5 changes: 5 additions & 0 deletions internal/driver/mobile/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func init() {

func (d *driver) DoFromGoroutine(fn func(), wait bool) {
async.EnsureNotMain(func() {
if d.queuedFuncs == nil {
fn() // before the app actually starts
return
}
var done chan struct{}
if wait {
done = common.DonePool.Get()
Expand Down Expand Up @@ -167,6 +171,7 @@ func (d *driver) Run() {
d.running = true

app.Main(func(a app.App) {
async.SetMainGoroutine()
d.app = a
settingsChange := make(chan fyne.Settings)
d.queuedFuncs = async.NewUnboundedChan[func()]()
Expand Down

0 comments on commit b450374

Please sign in to comment.