forked from timandy/routine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_local_map.go
More file actions
118 lines (104 loc) · 2.49 KB
/
thread_local_map.go
File metadata and controls
118 lines (104 loc) · 2.49 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package routine
import _ "unsafe"
var unset entry = &object{}
type object struct {
none bool //nolint:unused
}
type threadLocalMap struct {
table []entry
}
func (mp *threadLocalMap) get(index int) entry {
lookup := mp.table
if index < len(lookup) {
return lookup[index]
}
return unset
}
func (mp *threadLocalMap) set(index int, value entry) {
lookup := mp.table
if index < len(lookup) {
lookup[index] = value
return
}
mp.expandAndSet(index, value)
}
func (mp *threadLocalMap) remove(index int) {
lookup := mp.table
if index < len(lookup) {
lookup[index] = unset
}
}
func (mp *threadLocalMap) expandAndSet(index int, value entry) {
oldArray := mp.table
oldCapacity := len(oldArray)
newCapacity := index
newCapacity |= newCapacity >> 1
newCapacity |= newCapacity >> 2
newCapacity |= newCapacity >> 4
newCapacity |= newCapacity >> 8
newCapacity |= newCapacity >> 16
newCapacity++
newArray := make([]entry, newCapacity)
copy(newArray, oldArray)
fill(newArray, oldCapacity, newCapacity, unset)
newArray[index] = value
mp.table = newArray
}
//go:norace
//go:linkname createInheritedMap routine.createInheritedMap
func createInheritedMap() *threadLocalMap {
parent := currentThread(false)
if parent == nil {
return nil
}
parentMap := parent.inheritableThreadLocals
if parentMap == nil {
return nil
}
lookup := parentMap.table
if lookup == nil {
return nil
}
table := make([]entry, len(lookup))
copy(table, lookup)
for i := 0; i < len(table); i++ {
if c, ok := entryAssert[Cloneable](table[i]); ok && !isNil(c) {
table[i] = entry(c.Clone())
}
}
return &threadLocalMap{table: table}
}
//go:norace
//go:linkname restoreInheritedMap routine.restoreInheritedMap
func restoreInheritedMap(mp *threadLocalMap) func() {
t := currentThread(mp != nil)
if t == nil {
// mp and t are nil
return clearThread
}
threadLocalsBackup := t.threadLocals
inheritableThreadLocalsBackup := t.inheritableThreadLocals
t.threadLocals = nil
t.inheritableThreadLocals = mp
return func() {
resetThread(t, threadLocalsBackup, inheritableThreadLocalsBackup)
}
}
//go:norace
func clearThread() {
t := currentThread(false)
if t != nil {
t.threadLocals = nil
t.inheritableThreadLocals = nil
}
}
//go:norace
func resetThread(t *thread, threadLocals, inheritableThreadLocals *threadLocalMap) {
t.threadLocals = threadLocals
t.inheritableThreadLocals = inheritableThreadLocals
}
func fill[T any](a []T, fromIndex int, toIndex int, val T) {
for i := fromIndex; i < toIndex; i++ {
a[i] = val
}
}