forked from jtolio/gls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gid.go
31 lines (27 loc) · 775 Bytes
/
gid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package gls
import (
"golang.design/x/lockfree"
)
var (
stackTagPool = &idPool{
queue: lockfree.NewQueue(),
}
)
// Will return this goroutine's identifier if set. If you always need a
// goroutine identifier, you should use EnsureGoroutineId which will make one
// if there isn't one already.
func GetGoroutineId() (gid uint32, ok bool) {
return readStackTag()
}
// Will call cb with the current goroutine identifier. If one hasn't already
// been generated, one will be created and set first. The goroutine identifier
// might be invalid after cb returns.
func EnsureGoroutineId(cb func(gid uint32)) {
if gid, ok := readStackTag(); ok {
cb(gid)
return
}
gid := stackTagPool.Acquire()
defer stackTagPool.Release(gid)
addStackTag(gid, func() { cb(gid) })
}