initial import
This commit is contained in:
31
core/threading/routines.go
Normal file
31
core/threading/routines.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package threading
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
"zero/core/rescue"
|
||||
)
|
||||
|
||||
func GoSafe(fn func()) {
|
||||
go RunSafe(fn)
|
||||
}
|
||||
|
||||
// Only for debug, never use it in production
|
||||
func RoutineId() uint64 {
|
||||
b := make([]byte, 64)
|
||||
b = b[:runtime.Stack(b, false)]
|
||||
b = bytes.TrimPrefix(b, []byte("goroutine "))
|
||||
b = b[:bytes.IndexByte(b, ' ')]
|
||||
// if error, just return 0
|
||||
n, _ := strconv.ParseUint(string(b), 10, 64)
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
func RunSafe(fn func()) {
|
||||
defer rescue.Recover()
|
||||
|
||||
fn()
|
||||
}
|
||||
Reference in New Issue
Block a user