forked from smarty-prototypes/go-disruptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
52 lines (41 loc) · 1.12 KB
/
writer.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
package disruptor
import "runtime"
type Writer struct {
written *Cursor
upstream Barrier
capacity int64
previous int64
gate int64
}
func NewWriter(written *Cursor, upstream Barrier, capacity int64) *Writer {
assertPowerOfTwo(capacity)
return &Writer{
upstream: upstream,
written: written,
capacity: capacity,
previous: InitialSequenceValue,
gate: InitialSequenceValue,
}
}
func assertPowerOfTwo(value int64) {
if value > 0 && (value&(value-1)) != 0 {
// Wikipedia entry: http://bit.ly/1krhaSB
panic("The ring capacity must be a power of two, e.g. 2, 4, 8, 16, 32, 64, etc.")
}
}
func (this *Writer) Reserve(count int64) int64 {
this.previous += count
for spin := int64(0); this.previous-this.capacity > this.gate; spin++ {
if spin&SpinMask == 0 {
runtime.Gosched() // LockSupport.parkNanos(1L); http://bit.ly/1xiDINZ
}
this.gate = this.upstream.Read(0)
}
return this.previous
}
func (this *Writer) Await(next int64) {
for next-this.capacity > this.gate {
this.gate = this.upstream.Read(0)
}
}
const SpinMask = 1024*16 - 1 // arbitrary; we'll want to experiment with different values