Main-locked threads are panic wrapped

This commit is contained in:
Sasha Koshka 2025-11-24 19:43:25 -05:00
parent f97f5010e2
commit 5d25b3fb9a
2 changed files with 45 additions and 19 deletions

View File

@ -234,9 +234,9 @@ func (this *environment) phase70Running() bool {
} }
} }
result := make(chan bool) bodyReturn := make(chan bool)
go func() { go func() {
result <- this.phase70RunningBody() bodyReturn <- this.phase70RunningBody()
if this.main != nil { if this.main != nil {
shutdownCtx, done := context.WithTimeout( shutdownCtx, done := context.WithTimeout(
context.Background(), context.Background(),
@ -247,19 +247,12 @@ func (this *environment) phase70Running() bool {
} }
}() }()
mainReturn := false
if this.main != nil { if this.main != nil {
mainActor := this.main.(Actor) mainReturn = this.phase70_2MainBind()
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 return <- bodyReturn && mainReturn
} }
func (this *environment) phase70RunningBody() bool { func (this *environment) phase70RunningBody() bool {
@ -294,6 +287,21 @@ func (this *environment) phase70RunningBody() bool {
return true return true
} }
func (this *environment) phase70_2MainBind() bool{
mainActor := this.main.(Actor)
if this.Verb() { log.Printf("... (70.2) binding %s to main thread", mainActor.Type()) }
runtime.LockOSThread()
if this.Verb() { log.Printf(".// (70.2) main thread bind complete") }
defer runtime.UnlockOSThread()
err := panicWrap(this.main.RunMain)
if err != nil {
log.Printf("XXX [%s] main thread failed: %v", mainActor.Type(), err)
return false
}
if this.Verb() { log.Printf("(i) (70.2) main thread exited") }
return true
}
func (this *environment) phase70_5Trimming() bool { func (this *environment) phase70_5Trimming() bool {
if this.Verb() { log.Println("... (70.5) trimming") } if this.Verb() { log.Println("... (70.5) trimming") }
var trimmable []Trimmable var trimmable []Trimmable

32
util.go
View File

@ -14,16 +14,34 @@ import "sync/atomic"
import "unicode/utf8" import "unicode/utf8"
import "runtime/debug" import "runtime/debug"
func panicErr(message any, stack []byte) (err error) { type panicError struct {
if panErr, ok := message.(error); ok { wrapped error
err = panErr stack []byte
}
func (this panicError) Error() string {
if this.stack == nil {
return this.wrapped.Error()
} else { } else {
err = errors.New(fmt.Sprint(message)) return fmt.Sprintf("%v: %s", this.wrapped, this.stack)
} }
if stack != nil {
err = fmt.Errorf("%w: %s", err, stack)
} }
return err
func (this panicError) Unwrap() error {
return this.wrapped
}
func panicErr(message any, stack []byte) (err error) {
var wrapped error
if panErr, ok := message.(error); ok {
wrapped = panErr
} else {
wrapped = errors.New(fmt.Sprint(message))
}
return panicError {
wrapped: wrapped,
stack: stack,
}
} }
func defaul[T comparable](value, def T) T { func defaul[T comparable](value, def T) T {