-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the goroutine code to internal helpers
- Loading branch information
1 parent
220f6c6
commit a35749c
Showing
9 changed files
with
70 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
2 changes: 1 addition & 1 deletion
2
internal/driver/glfw/driver_goxjs.go → internal/async/goroutine_goxjs.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters