Using a cursor for safe cross-thread writes when checking gates across CPU cores.
diff --git a/example/main.go b/example/main.go
index be6eec3..5428882 100644
--- a/example/main.go
+++ b/example/main.go
@@ -9,7 +9,7 @@
const (
MaxConsumersPerGroup = 1
MaxConsumerGroups = 2
- MaxProducers = 1
+ MaxProducers = 2
ItemsToPublish = 4
ReportingFrequency = 1000000 * 10 // 1 million * N
RingSize = 1024 * 16
diff --git a/shared_writer.go b/shared_writer.go
index 871f699..3d4cfc8 100644
--- a/shared_writer.go
+++ b/shared_writer.go
@@ -4,7 +4,7 @@
type SharedWriter struct {
capacity int64
- gate int64 // TODO: determine if this should be a *Cursor
+ gate *Cursor
mask int64
shift uint8
committed []int32
@@ -15,7 +15,7 @@
func NewSharedWriter(write *SharedWriterBarrier, upstream Barrier) *SharedWriter {
return &SharedWriter{
capacity: write.capacity,
- gate: InitialSequenceValue,
+ gate: NewCursor(),
mask: write.mask,
shift: write.shift,
committed: write.committed,
@@ -30,13 +30,13 @@
upper := previous + count
wrap := upper - this.capacity
- if wrap > this.gate {
+ if wrap > this.gate.Load() {
min := this.upstream.LoadBarrier(0)
if wrap > min {
return InitialSequenceValue, Gating
}
- this.gate = min // doesn't matter which write wins, BUT will most likely need to be a Cursor
+ this.gate.Store(min)
}
if atomic.CompareAndSwapInt64(&this.written.sequence, previous, upper) {