|
| 1 | +package routine |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +// LocalStorage provides goroutine-local variables. |
| 6 | +type LocalStorage interface { |
| 7 | + |
| 8 | + // Get returns the value in the current goroutine's local storage, if it was set before. |
| 9 | + Get() (value interface{}) |
| 10 | + |
| 11 | + // Set copy the value into the current goroutine's local storage, and return the old value. |
| 12 | + Set(value interface{}) (oldValue interface{}) |
| 13 | + |
| 14 | + // Del delete the value from the current goroutine's local storage, and return it. |
| 15 | + Del() (oldValue interface{}) |
| 16 | + |
| 17 | + // Clear delete values from all goroutine's local storages. |
| 18 | + Clear() |
| 19 | +} |
| 20 | + |
| 21 | +// ImmutableContext represents all local storages of one goroutine. |
| 22 | +type ImmutableContext struct { |
| 23 | + gid int64 |
| 24 | + values map[uintptr]interface{} |
| 25 | +} |
| 26 | + |
| 27 | +// Go start an new goroutine, and copy all local storages from current goroutine. |
| 28 | +func Go(f func()) { |
| 29 | + ic := BackupContext() |
| 30 | + go func() { |
| 31 | + InheritContext(ic) |
| 32 | + f() |
| 33 | + }() |
| 34 | +} |
| 35 | + |
| 36 | +// BackupContext copy all local storages into an ImmutableContext instance. |
| 37 | +func BackupContext() *ImmutableContext { |
| 38 | + s := loadCurrentStore() |
| 39 | + data := make(map[uintptr]interface{}, len(s.values)) |
| 40 | + for k, v := range s.values { |
| 41 | + data[k] = v |
| 42 | + } |
| 43 | + return &ImmutableContext{gid: s.gid, values: data} |
| 44 | +} |
| 45 | + |
| 46 | +// InheritContext load the specified ImmutableContext instance into the local storage of current goroutine. |
| 47 | +func InheritContext(ic *ImmutableContext) { |
| 48 | + if ic == nil || ic.values == nil { |
| 49 | + return |
| 50 | + } |
| 51 | + s := loadCurrentStore() |
| 52 | + for k, v := range ic.values { |
| 53 | + s.values[k] = v |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// NewLocalStorage create and return an new LocalStorage instance. |
| 58 | +func NewLocalStorage() LocalStorage { |
| 59 | + t := new(storage) |
| 60 | + t.Clear() |
| 61 | + return t |
| 62 | +} |
| 63 | + |
| 64 | +// Goid return the current goroutine's unique id. |
| 65 | +// It will try get gid by native cgo/asm for better performance, |
| 66 | +// and could parse gid from stack for failover supporting. |
| 67 | +func Goid() (id int64) { |
| 68 | + var succ bool |
| 69 | + if id, succ = getGoidByNative(); !succ { |
| 70 | + // no need to warning |
| 71 | + id = getGoidByStack() |
| 72 | + } |
| 73 | + return |
| 74 | +} |
| 75 | + |
| 76 | +// AllGoids return all goroutine's goid in the current golang process. |
| 77 | +// It will try load all goid from runtime natively for better performance, |
| 78 | +// and fallover to runtime.Stack, which is realy inefficient. |
| 79 | +func AllGoids() (ids []int64) { |
| 80 | + var err error |
| 81 | + if ids, err = getAllGoidByNative(); err != nil { |
| 82 | + fmt.Println("[WARNING] cannot get all goid from runtime natively, now fallover to stack info, this will be very inefficient!!!") |
| 83 | + ids = getAllGoidByStack() |
| 84 | + } |
| 85 | + return |
| 86 | +} |
0 commit comments