-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.go
58 lines (46 loc) · 1.32 KB
/
dict.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
package adhoctx
import (
"unsafe"
)
type dictType map[string]string
type DictView struct {
dictsByID PointerStackView
currentDictID atomicCommitter
}
func NewDictView() *DictView {
dv := new(DictView)
dict := make(dictType)
dPtr := unsafe.Pointer(&dict)
currVer := dv.dictsByID.AllocateID(dPtr)
dv.currentDictID.unsafe_race_condition_commit('d', unsafe.Pointer(&currVer))
return dv
}
func (d *DictView) Reader() uint32 {
return *(*uint32)(d.currentDictID.getCurrent('d'))
}
func (d *DictView) ReadWriter(u uint32) uint32 {
currDict := *(*dictType)(d.dictsByID.GetPointer(u))
newDict := make(dictType, len(currDict))
for k, v := range currDict {
newDict[k] = v
}
return d.dictsByID.AllocateID(unsafe.Pointer(&newDict))
}
func (d *DictView) Commit(old, new uint32) bool {
if !d.currentDictID.tryCommit('d', d.currentDictID.getCurrent('d'), unsafe.Pointer(&new)) {
return false
}
d.dictsByID.RemoveID(old)
return true
}
func (d *DictView) SetKey(u uint32, key, val string) {
dict := *(*dictType)(d.dictsByID.GetPointer(u))
dict[key] = val
}
func (d *DictView) GetKey(u uint32, key string) string {
dict := *(*dictType)(d.dictsByID.GetPointer(u))
return dict[key]
}
func (d *DictView) String() string {
return "dictview[currentDictID:" + d.currentDictID.String() + ",dictsByID:" + d.dictsByID.String() + "]"
}