-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapstring.go
174 lines (143 loc) · 2.85 KB
/
mapstring.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
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
// Copyright 2019 Kazuhisa TAKEI<xtakei@rytr.jp>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package loncha/list_head is like a kernel's LIST_HEAD
// list_head is used by loncha/gen/containers_list
package list_head
import (
"sync/atomic"
)
type MapString struct {
baseMap
dirty map[string]atomic.Value
}
type readMapStr struct {
m map[string]atomic.Value
amended bool // include dirty map
}
func (r readMapStr) store(k string, v *ListHead) (ok bool) {
ov, ok := r.m[k]
if !ok {
return ok
}
ohead := ov.Load().(*ListHead)
return ov.CompareAndSwap(ohead, v)
}
func (m *MapString) Set(k string, v *ListHead) bool {
read, succ := m.read.Load().(readMapStr)
if !succ {
m.read.Store(readMapStr{})
}
ensure := func(v *ListHead) {
for _, fn := range m.onNewStores {
fn(v)
}
}
if ok := read.store(k, v); ok {
defer ensure(v)
return true
}
m.Lock()
defer m.Unlock()
_, ok := read.m[k]
if !ok {
_, ok = m.getDirty(k)
}
var av atomic.Value
if ok {
av = m.dirty[k]
} else if !read.amended {
m.read.Store(readMapStr{m: read.m, amended: true})
}
av.Store(v)
if m.dirty == nil {
m.dirty = map[string]atomic.Value{}
}
m.dirty[k] = av
if !ok {
defer ensure(v)
}
return true
}
func (m *MapString) missLocked() {
m.misses++
if m.misses < len(m.dirty) {
return
}
m.read.Store(readMapStr{m: m.dirty})
m.dirty = nil
m.misses = 0
}
func (m *MapString) Get(k string) (v *ListHead, ok bool) {
read := m.read.Load().(readMapStr)
av, ok := read.m[k]
if ok {
v, ok = av.Load().(*ListHead)
}
if !ok && read.amended {
m.Lock()
defer m.Unlock()
read := m.read.Load().(readMapStr)
av, ok = read.m[k]
if !ok && read.amended {
v, ok = m.getDirty(k)
m.missLocked()
}
}
if !ok || v == nil {
return nil, false
}
return
}
func (m *MapString) getDirty(k string) (v *ListHead, ok bool) {
av, ok := m.dirty[k]
if ok {
v = av.Load().(*ListHead)
}
return
}
func (m *MapString) Delete(k string) bool {
read, _ := m.read.Load().(readMapStr)
av, ok := read.m[k]
if !ok && read.amended {
m.Lock()
defer m.Unlock()
read, _ := m.read.Load().(readMapStr)
if !ok && read.amended {
av, ok = m.dirty[k]
delete(m.dirty, k)
m.missLocked()
}
}
if ok {
ohead := av.Load().(*ListHead)
return av.CompareAndSwap(ohead, nil)
}
return false
}
func (m *MapString) Len() int {
return len(m.dirty)
}
func (m *MapString) Range(f func(key string, value *ListHead) bool) {
read, _ := m.read.Load().(readMapStr)
if read.amended {
m.Lock()
read, _ := m.read.Load().(readMapStr)
if read.amended {
read = readMapStr{m: m.dirty}
m.read.Store(read)
m.dirty = nil
m.misses = 0
}
m.Unlock()
}
for k, e := range read.m {
v, ok := e.Load().(*ListHead)
if !ok {
continue
}
if !f(k, v) {
break
}
}
}