-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidmap_ops.go
More file actions
312 lines (290 loc) · 8.84 KB
/
Copy pathidmap_ops.go
File metadata and controls
312 lines (290 loc) · 8.84 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package memory
import (
"errors"
"unsafe"
)
var errIDMapRetry = errors.New("id map retry")
type idMapPutResult struct {
value unsafe.Pointer
inserted bool
}
func (m *IDMap) put(key idMapKey, value unsafe.Pointer, onlyAbsent bool, result *idMapPutResult) error {
for {
if m.closed.Load() {
return ErrIDMapFreed
}
s := m.state.Load()
if s == nil {
return ErrIDMapFreed
}
if s.next != nil {
m.helpMigrateAll(s)
continue
}
existing, inserted, err := m.putInner(s, key, value, onlyAbsent, true)
if err == nil {
if result != nil {
result.value = existing
result.inserted = inserted
}
return nil
}
if err == errIDMapRetry {
Spin()
continue
}
if err != ErrNeedsResize {
return err
}
if current := m.state.Load(); current == s && current.next == nil {
if err := m.triggerResize(s); err != nil {
return err
}
}
}
}
func (m *IDMap) putInner(s *idMapState, key idMapKey, value unsafe.Pointer, onlyAbsent, copyKey bool) (unsafe.Pointer, bool, error) {
fingerprint := idMapFingerprint(key.hash)
start := key.hash & (s.size - 1)
return m.putInnerReserved(s, key, value, onlyAbsent, copyKey, fingerprint, start)
}
func (m *IDMap) putInnerReserved(s *idMapState, key idMapKey, value unsafe.Pointer, onlyAbsent, copyKey bool, fingerprint, start uint64) (unsafe.Pointer, bool, error) {
var candidate *IDBucket
var candidateSlot uint32
var candidateTomb bool
for probe := uint64(0); probe < s.size; probe++ {
bucket := idMapBucket(s, (start+probe)&(s.size-1))
meta := bucket.Metadata.Load()
if meta&(idMapMigratingBit|idMapMigratedBit) != 0 {
return nil, false, ErrNeedsResize
}
if (meta>>idMapReservedShift)&idMapSlotMask != 0 {
return nil, false, errIDMapRetry
}
matches := idMapFingerprintMask(meta, fingerprint)
for matches != 0 {
slot := idMapFirst(matches)
if bucket.Hashes[slot].Load() == key.hash && idMapKeysEqual(bucket.KeyPtrs[slot].Load(), bucket.KeyLens[slot].Load(), key) {
existing := unsafe.Pointer(bucket.Vals[slot].Load())
if onlyAbsent {
return existing, false, nil
}
reserveBit := uint64(1) << (idMapReservedShift + uint64(slot))
if !bucket.Metadata.CompareAndSwap(meta, meta|reserveBit) {
return nil, false, errIDMapRetry
}
bucket.Vals[slot].Store(uintptr(value))
for {
current := bucket.Metadata.Load()
if current&(idMapMigratingBit|idMapMigratedBit) != 0 ||
current&(uint64(1)<<(idMapOccupiedShift+uint64(slot))) == 0 {
idMapClearReservation(bucket, reserveBit)
return nil, false, errIDMapRetry
}
if bucket.Metadata.CompareAndSwap(current, current&^reserveBit) {
break
}
}
if bucket.Hashes[slot].Load() == key.hash {
return value, false, nil
}
return nil, false, errIDMapRetry
}
matches &= matches - 1
}
occupied := (meta >> idMapOccupiedShift) & idMapSlotMask
reserved := (meta >> idMapReservedShift) & idMapSlotMask
tombs := (meta >> idMapTombShift) & idMapSlotMask
availableTomb := tombs &^ occupied &^ reserved
if candidate == nil && availableTomb != 0 {
candidate = bucket
candidateSlot = idMapFirst(availableTomb)
candidateTomb = true
}
trueEmpty := idMapSlotMask &^ (occupied | reserved | tombs)
if trueEmpty == 0 {
continue
}
if candidate == nil {
candidate = bucket
candidateSlot = idMapFirst(trueEmpty)
candidateTomb = false
}
return m.reserveAndPublish(candidate, candidateSlot, candidateTomb, key, value, copyKey)
}
if candidate != nil {
return m.reserveAndPublish(candidate, candidateSlot, candidateTomb, key, value, copyKey)
}
return nil, false, ErrNeedsResize
}
func idMapClearReservation(bucket *IDBucket, reserveBit uint64) {
for {
meta := bucket.Metadata.Load()
if meta&reserveBit == 0 || bucket.Metadata.CompareAndSwap(meta, meta&^reserveBit) {
return
}
}
}
func (m *IDMap) reserveAndPublish(bucket *IDBucket, slot uint32, tomb bool, key idMapKey, value unsafe.Pointer, copyKey bool) (unsafe.Pointer, bool, error) {
reserveBit := uint64(1) << (idMapReservedShift + uint64(slot))
occupiedBit := uint64(1) << (idMapOccupiedShift + uint64(slot))
tombBit := uint64(1) << (idMapTombShift + uint64(slot))
for {
meta := bucket.Metadata.Load()
if meta&(idMapMigratingBit|idMapMigratedBit) != 0 || meta&reserveBit != 0 || meta&occupiedBit != 0 {
return nil, false, errIDMapRetry
}
if tomb && meta&tombBit == 0 {
return nil, false, errIDMapRetry
}
if !tomb && meta&tombBit != 0 {
return nil, false, errIDMapRetry
}
if bucket.Metadata.CompareAndSwap(meta, meta|reserveBit) {
break
}
}
storedKey := key.ptr
if copyKey && key.len != 0 {
var err error
storedKey, err = m.keys.Alloc(uint64(key.len))
if err != nil {
m.releaseReservation(bucket, slot, tomb)
return nil, false, err
}
copy(unsafe.Slice((*byte)(storedKey), int(key.len)), unsafe.Slice((*byte)(key.ptr), int(key.len)))
}
bucket.Hashes[slot].Store(key.hash)
bucket.KeyPtrs[slot].Store(uintptr(storedKey))
bucket.KeyLens[slot].Store(key.len)
bucket.Vals[slot].Store(uintptr(value))
fingerprintShift := idMapFingerprintAt + uint64(slot)*8
for {
meta := bucket.Metadata.Load()
if meta&reserveBit == 0 {
return nil, false, errIDMapRetry
}
published := meta | occupiedBit
published &^= reserveBit | tombBit | (uint64(0xff) << fingerprintShift)
published |= idMapFingerprint(key.hash) << fingerprintShift
if bucket.Metadata.CompareAndSwap(meta, published) {
return value, true, nil
}
}
}
func (m *IDMap) releaseReservation(bucket *IDBucket, slot uint32, tomb bool) {
reserveBit := uint64(1) << (idMapReservedShift + uint64(slot))
tombBit := uint64(1) << (idMapTombShift + uint64(slot))
for {
meta := bucket.Metadata.Load()
next := meta &^ reserveBit
if tomb {
next |= tombBit
}
if bucket.Metadata.CompareAndSwap(meta, next) {
return
}
}
}
func (m *IDMap) get(key idMapKey) (unsafe.Pointer, bool) {
for {
s := m.state.Load()
if s == nil {
return nil, false
}
if s.next != nil {
m.helpMigrateAll(s)
continue
}
return m.getInner(s, key)
}
}
func (m *IDMap) getInner(s *idMapState, key idMapKey) (unsafe.Pointer, bool) {
fingerprint := idMapFingerprint(key.hash)
start := key.hash & (s.size - 1)
for probe := uint64(0); probe < s.size; probe++ {
bucket := idMapBucket(s, (start+probe)&(s.size-1))
meta := bucket.Metadata.Load()
matches := idMapFingerprintMask(meta, fingerprint)
for matches != 0 {
slot := idMapFirst(matches)
if bucket.Hashes[slot].Load() == key.hash && idMapKeysEqual(bucket.KeyPtrs[slot].Load(), bucket.KeyLens[slot].Load(), key) {
return unsafe.Pointer(bucket.Vals[slot].Load()), true
}
matches &= matches - 1
}
occupied := (meta >> idMapOccupiedShift) & idMapSlotMask
reserved := (meta >> idMapReservedShift) & idMapSlotMask
tombs := (meta >> idMapTombShift) & idMapSlotMask
if occupied|reserved|tombs != idMapSlotMask {
return nil, false
}
}
return nil, false
}
func (m *IDMap) delete(key idMapKey) bool {
for {
s := m.state.Load()
if s == nil {
return false
}
if s.next != nil {
m.helpMigrateAll(s)
continue
}
deleted, retry := m.deleteInner(s, key)
if retry {
Spin()
continue
}
return deleted
}
}
func (m *IDMap) deleteInner(s *idMapState, key idMapKey) (bool, bool) {
fingerprint := idMapFingerprint(key.hash)
start := key.hash & (s.size - 1)
return m.deleteInnerReserved(s, key, fingerprint, start)
}
func (m *IDMap) deleteInnerReserved(s *idMapState, key idMapKey, fingerprint, start uint64) (bool, bool) {
for probe := uint64(0); probe < s.size; probe++ {
bucket := idMapBucket(s, (start+probe)&(s.size-1))
for {
meta := bucket.Metadata.Load()
if meta&(idMapMigratingBit|idMapMigratedBit) != 0 || (meta>>idMapReservedShift)&idMapSlotMask != 0 {
return false, true
}
matches := idMapFingerprintMask(meta, fingerprint)
var matched bool
for matches != 0 {
slot := idMapFirst(matches)
if bucket.Hashes[slot].Load() == key.hash && idMapKeysEqual(bucket.KeyPtrs[slot].Load(), bucket.KeyLens[slot].Load(), key) {
matched = true
occupiedBit := uint64(1) << (idMapOccupiedShift + uint64(slot))
reserveBit := uint64(1) << (idMapReservedShift + uint64(slot))
tombBit := uint64(1) << (idMapTombShift + uint64(slot))
if !bucket.Metadata.CompareAndSwap(meta, meta|reserveBit) {
break
}
reserved := meta | reserveBit
if bucket.Metadata.CompareAndSwap(reserved, ((reserved&^occupiedBit)&^reserveBit)|tombBit) {
return true, false
}
idMapClearReservation(bucket, reserveBit)
break
}
matches &= matches - 1
}
if matched {
continue
}
occupied := (meta >> idMapOccupiedShift) & idMapSlotMask
reserved := (meta >> idMapReservedShift) & idMapSlotMask
tombs := (meta >> idMapTombShift) & idMapSlotMask
if occupied|reserved|tombs != idMapSlotMask {
return false, false
}
break
}
}
return false, false
}