-
Notifications
You must be signed in to change notification settings - Fork 0
/
leftright.go
64 lines (56 loc) · 1.69 KB
/
leftright.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
package leftright
import (
"sync"
"github.com/pourplusquoi/slabmap"
)
// New creates a pair of `ReadHandle` and `WriteHandle`, based on a concurrency
// model called "left-right".
//
// Basically, there are two copies of the same map in the concurrency model,
// where the writer writes to the one map and readers read from the other map.
// Only after the writer explicitly publishes changes can readers see the
// modifications until then.
//
// The benefit of "left-right" concurrency model is that reads are entirely
// lock-free and can be very fast, and that the writer can decide when to
// publish changes to readers. However, the cost is that the model consumes
// extra memory, and that the model only supports single writer, and that the
// writer has to do extra work while writing.
//
// To summarize, this concurrency model is suitable for use cases where reads
// are more frequent than writes and strong consistency is not required.
func New() (*ReadHandle, *WriteHandle) {
slab := newSyncSlabMap()
epoch := uint64(0)
index := slab.inner.Insert(&epoch)
parent := &leftrightMap{
maps: [2]map[interface{}]interface{}{
make(map[interface{}]interface{}),
make(map[interface{}]interface{}),
},
current: 0,
epochs: slab,
}
return newReadHandle(parent, &epoch, index), newWriteHandle(parent)
}
type leftrightMap struct {
maps [2]map[interface{}]interface{}
current uint32
epochs *syncSlabMap
}
type syncSlabMap struct {
inner *slabmap.SlabMap
mu sync.Mutex
}
func newSyncSlabMap() *syncSlabMap {
return &syncSlabMap{
inner: slabmap.NewSlabMap(),
}
}
func (s *syncSlabMap) Lock() *slabmap.SlabMap {
s.mu.Lock()
return s.inner
}
func (s *syncSlabMap) Unlock() {
s.mu.Unlock()
}