Add MainRunnable interface for locking to the main thread

This commit is contained in:
2025-11-24 10:21:41 -05:00
parent f0611e53ce
commit 32c8e7f7c3
4 changed files with 57 additions and 1 deletions

View File

@@ -228,6 +228,41 @@ func (this *environment) phase60Initialization() bool {
}
func (this *environment) phase70Running() bool {
for actor := range this.All() {
if actor, ok := actor.(MainRunnable); ok {
this.main = actor
}
}
result := make(chan bool)
go func() {
result <- this.phase70RunningBody()
if this.main != nil {
shutdownCtx, done := context.WithTimeout(
context.Background(),
defaul(this.timing.shutdownTimeout.Load(),
defaultShutdownTimeout))
defer done()
this.main.ShutdownMain(shutdownCtx)
}
}()
if this.main != nil {
mainActor := this.main.(Actor)
if this.Verb() { log.Printf("(i) (70) binding %s to main thread", mainActor.Type()) }
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := this.main.RunMain()
if err != nil {
log.Printf("XXX [%s] main thread failed: %v", mainActor.Type(), err)
}
if this.Verb() { log.Printf("(i) (70) main thread exited") }
}
return <- result
}
func (this *environment) phase70RunningBody() bool {
defer this.Done(nil)
if this.Verb() { log.Println("... (70) starting up") }
this.running.Store(true)