forked from smarty-prototypes/go-disruptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
46 lines (38 loc) · 1.13 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
package disruptor
type Writer struct {
previous int64
gate int64
capacity int64
writerCursor *Cursor
readerBarrier Barrier
}
func NewWriter(writerCursor *Cursor, capacity int64, readerBarrier Barrier) *Writer {
assertPowerOfTwo(capacity)
return &Writer{
previous: writerCursor.Load(), // show the Go runtime that the cursor is actually used
gate: writerCursor.Load(), // and that it should not be optimized away
capacity: capacity,
writerCursor: writerCursor,
readerBarrier: readerBarrier,
}
}
func assertPowerOfTwo(value int64) {
if value > 0 && (value&(value-1)) != 0 {
// http://en.wikipedia.org/wiki/Power_of_two#Fast_algorithm_to_check_if_a_positive_number_is_a_power_of_two
panic("The ring capacity must be a power of two, e.g. 2, 4, 8, 16, 32, 64, etc.")
}
}
// TODO: return lower, upper instead of upper
func (this *Writer) Reserve(count int64) int64 {
next := this.previous + count
wrap := next - this.capacity
if wrap > this.gate {
min := this.readerBarrier.LoadBarrier(0)
if wrap > min {
return Gating
}
this.gate = min
}
this.previous = next
return next
}