forked from smarty-prototypes/go-disruptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_writer_barrier.go
43 lines (36 loc) · 910 Bytes
/
shared_writer_barrier.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
package disruptor
import "math"
type SharedWriterBarrier struct {
written *Cursor
committed []int32
capacity int64
mask int64
shift uint8
}
func NewSharedWriterBarrier(written *Cursor, capacity int64) *SharedWriterBarrier {
assertPowerOfTwo(capacity)
return &SharedWriterBarrier{
written: written,
committed: prepareCommitBuffer(capacity),
capacity: capacity,
mask: capacity - 1,
shift: uint8(math.Log2(float64(capacity))),
}
}
func prepareCommitBuffer(capacity int64) []int32 {
buffer := make([]int32, capacity)
for i := range buffer {
buffer[i] = int32(InitialSequenceValue)
}
return buffer
}
func (this *SharedWriterBarrier) Read(lower int64) int64 {
shift, mask := this.shift, this.mask
upper := this.written.Load()
for ; lower <= upper; lower++ {
if this.committed[lower&mask] != int32(lower>>shift) {
return lower - 1
}
}
return upper
}