Skip to content

Commit 048891b

Browse files
authored
Merge pull request #751 from JiepengTan/pr_add_spx_coror_abort_detection
Add spx coro termination detection
2 parents dee631f + 7e4f30a commit 048891b

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

internal/coroutine/coro.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type Coroutines struct {
8686
waitMutex sync.Mutex
8787
waitCond sync.Cond
8888
debug bool
89-
89+
9090
// goroutineIDs tracks all goroutine IDs created by CreateAndStart
9191
goroutineIDs sync.Map // map[int64]bool
9292
}
@@ -202,7 +202,7 @@ func (p *Coroutines) CreateAndStart(start bool, tobj ThreadObj, fn func(me Threa
202202
// Track this goroutine ID
203203
gid := goid.Get()
204204
p.goroutineIDs.Store(gid, true)
205-
205+
206206
p.sema.Lock()
207207
p.setCurrent(id)
208208
defer func() {
@@ -211,10 +211,10 @@ func (p *Coroutines) CreateAndStart(start bool, tobj ThreadObj, fn func(me Threa
211211
p.mutex.Unlock()
212212
p.setWaitStatus(id, waitStatusDelete)
213213
p.sema.Unlock()
214-
214+
215215
// Remove goroutine ID from tracking
216216
p.goroutineIDs.Delete(gid)
217-
217+
218218
if e := recover(); e != nil {
219219
if e != ErrAbortThread {
220220
if p.onPanic != nil {
@@ -575,3 +575,7 @@ func (p *Coroutines) IsInCoroutine() bool {
575575
_, exists := p.goroutineIDs.Load(currentGID)
576576
return exists
577577
}
578+
579+
func IsAbortThreadError(err any) bool {
580+
return err == ErrAbortThread
581+
}

internal/engine/coro.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func IsInCoroutine() bool {
2626
return gco.IsInCoroutine()
2727
}
2828

29+
func IsAbortThreadError(err any) bool {
30+
return coroutine.IsAbortThreadError(err)
31+
}
32+
2933
func GetCoroutineOwner() any {
3034
if IsInCoroutine() {
3135
return gco.Current().Obj

pkg/spx/api.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@ func isSpxEnv() bool {
1111
return engine.GetGame() != nil
1212
}
1313

14+
// IsAbortThreadError checks whether the given error is an SPX coroutine termination error.
15+
// This function is used to determine if an error was thrown due to SPX coroutine termination.
16+
//
17+
// Parameters:
18+
//
19+
// err - The error to check
20+
//
21+
// Returns:
22+
//
23+
// true if the error is an SPX coroutine termination error, false otherwise
24+
//
25+
// Example:
26+
//
27+
// defer func() {
28+
// if r := recover(); r != nil {
29+
// if spx.IsAbortThreadError(r) {
30+
// // Handle SPX coroutine termination
31+
// return
32+
// }
33+
// // Handle other panics
34+
// panic(r)
35+
// }
36+
// }()
37+
func IsAbortThreadError(err any) bool {
38+
return engine.IsAbortThreadError(err)
39+
}
40+
1441
// IsInCoroutine checks whether the current execution context is within an SPX coroutine.
1542
// Returns true if running inside an SPX coroutine, false if running in a regular Go goroutine
1643
// or the main thread.

0 commit comments

Comments
 (0)