Repository navigation
Expand file tree
/
Copy pathapi_thread_local.go
More file actions
45 lines (36 loc) · 1.76 KB
/
Copy pathapi_thread_local.go
File metadata and controls
45 lines (36 loc) · 1.76 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package routine
// ThreadLocal provides goroutine-local variables.
type ThreadLocal interface {
// Id returns the global id of instance.
Id() int
// Get returns the value in the current goroutine's local threadLocals or inheritableThreadLocals, if it was set before.
Get() Any
// Set copy the value into the current goroutine's local threadLocals or inheritableThreadLocals.
Set(value Any)
// Remove delete the value from the current goroutine's local threadLocals or inheritableThreadLocals.
Remove()
}
// Supplier provides a function which return Any type result.
type Supplier = func() Any
// NewThreadLocal create and return a new ThreadLocal instance.
// The initial value is nil.
func NewThreadLocal() ThreadLocal {
return &threadLocal{id: nextThreadLocalId()}
}
// NewThreadLocalWithInitial create and return a new ThreadLocal instance.
// The initial value is determined by invoking the supplier method.
func NewThreadLocalWithInitial(supplier Supplier) ThreadLocal {
return &threadLocal{id: nextThreadLocalId(), supplier: supplier}
}
// NewInheritableThreadLocal create and return a new ThreadLocal instance.
// The initial value is nil.
// The value can be inherited to sub goroutines witch started by Go, GoWait, GoWaitResult methods.
func NewInheritableThreadLocal() ThreadLocal {
return &inheritableThreadLocal{id: nextInheritableThreadLocalId()}
}
// NewInheritableThreadLocalWithInitial create and return a new ThreadLocal instance.
// The initial value is determined by invoking the supplier method.
// The value can be inherited to sub goroutines witch started by Go, GoWait, GoWaitResult methods.
func NewInheritableThreadLocalWithInitial(supplier Supplier) ThreadLocal {
return &inheritableThreadLocal{id: nextInheritableThreadLocalId(), supplier: supplier}
}