-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
115 lines (101 loc) · 2.32 KB
/
store.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
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
package threadlocal
const (
INITIALIZE_THREADLOCALMAP_SIZE = 16
INITIALIZE_THREADLOCALSTORE_SIZE = 128
)
var (
store = make(threadLocalStore, INITIALIZE_THREADLOCALSTORE_SIZE)
expungeEntity = NewEntity(nil, nil)
)
type threadLocalStore map[uint32]*ThreadlocalMap
func NewThreadlocalMap(capacity int) *ThreadlocalMap {
return &ThreadlocalMap{
size: 0,
capacity: capacity,
entities: make([]*Entity, capacity),
}
}
type ThreadlocalMap struct {
size int
capacity int
entities []*Entity
}
func (tlm *ThreadlocalMap) Size() int {
return tlm.size
}
func (tlm *ThreadlocalMap) Set(key *Threadlocal, val interface{}) {
i := key.HashCode & (tlm.capacity - 1)
for e := tlm.entities[i]; e != nil && e != expungeEntity; i = tlm.nextIndex(i, tlm.capacity) {
if e.key == key {
e.val = val
return
}
e = tlm.entities[i]
}
tlm.entities[i] = NewEntity(key, val)
tlm.size++
tlm.rehash()
}
func (tlm *ThreadlocalMap) Get(key *Threadlocal) interface{} {
i := key.HashCode & (tlm.capacity - 1)
for e := tlm.entities[i]; e != nil && e != expungeEntity; i = tlm.nextIndex(i, tlm.capacity) {
if e.key == key {
return e.val
}
e = tlm.entities[i]
}
return nil
}
func (tlm *ThreadlocalMap) Remove(key *Threadlocal) {
i := key.HashCode & (tlm.capacity - 1)
for e := tlm.entities[i]; e != nil && e != expungeEntity; i = tlm.nextIndex(i, tlm.capacity) {
if e.key == key {
tlm.entities[i] = expungeEntity // mark deleted
tlm.size--
break
}
e = tlm.entities[i]
}
}
func (tlm *ThreadlocalMap) nextIndex(i, cap int) int {
if i+1 < cap {
return i + 1
}
return 0
}
func (tlm *ThreadlocalMap) rehash() {
if tlm.size < tlm.capacity*3/4 {
return
}
newLen := tlm.capacity << 1
newTab := make([]*Entity, newLen)
for _, e := range tlm.entities {
if e == nil || e == expungeEntity {
continue
}
i := e.key.HashCode & (newLen - 1)
for newTab[i] != nil {
i = tlm.nextIndex(i, newLen)
}
newTab[i] = e
}
tlm.capacity = newLen
tlm.entities = newTab
}
type Entity struct {
key *Threadlocal
val interface{}
}
func NewEntity(tl *Threadlocal, val interface{}) *Entity {
return &Entity{
key: tl,
val: val,
}
}
func currentThreadLocalMap() *ThreadlocalMap {
var tid = ThreadId()
if store[tid] == nil {
store[tid] = NewThreadlocalMap(INITIALIZE_THREADLOCALMAP_SIZE)
}
return store[tid]
}