-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added overlapped ring buffer impl, see the #5
An overlapped ring buffer will overwrite the existing head element in putting new elem but the ring buffer is full. The original ring buffer of ourselves is to return a wrong state and stop putting action, see the `ErrQueueFull`. The new overlapped ring buffer will be created by calling `NewOverlappedRingBuffer()`. Any issues are welcome.
- Loading branch information
Showing
5 changed files
with
286 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package mpmc | ||
|
||
import ( | ||
"runtime" | ||
"sync/atomic" | ||
|
||
"github.com/hedzr/go-ringbuf/v2/mpmc/state" | ||
) | ||
|
||
type orbuf[T any] struct { | ||
ringBuf[T] | ||
} | ||
|
||
func (rb *orbuf[T]) Put(item T) (err error) { return rb.Enqueue(item) } //nolint:revive | ||
|
||
func (rb *orbuf[T]) Enqueue(item T) (err error) { //nolint:revive | ||
var tail, head, nt, nh uint32 | ||
var holder *rbItem[T] | ||
for { | ||
head = atomic.LoadUint32(&rb.head) | ||
tail = atomic.LoadUint32(&rb.tail) | ||
nt = (tail + 1) & rb.capModMask | ||
|
||
isEmpty := head == tail | ||
if isEmpty && head == MaxUint32 { | ||
err = ErrQueueNotReady | ||
return | ||
} | ||
|
||
isFull := nt == head | ||
if isFull { | ||
nh = (head + 1) & rb.capModMask | ||
atomic.CompareAndSwapUint32(&rb.head, head, nh) | ||
} | ||
|
||
holder = &rb.data[tail] | ||
atomic.CompareAndSwapUint32(&rb.tail, tail, nt) | ||
retry: | ||
if !atomic.CompareAndSwapUint64(&holder.readWrite, 0, 2) { //nolint:gomnd | ||
if !atomic.CompareAndSwapUint64(&holder.readWrite, 1, 2) { //nolint:gomnd | ||
if atomic.LoadUint64(&holder.readWrite) == 0 { | ||
goto retry // sometimes, short circuit | ||
} | ||
runtime.Gosched() // time to time | ||
continue | ||
} | ||
} | ||
|
||
if rb.initializer != nil { | ||
rb.initializer.CloneIn(item, &holder.value) | ||
} else { | ||
holder.value = item | ||
} | ||
if !atomic.CompareAndSwapUint64(&holder.readWrite, 2, 1) { //nolint:gomnd | ||
err = ErrRaced // runtime.Gosched() // never happens | ||
} | ||
|
||
if state.VerboseEnabled { | ||
state.Verbose("[W] enqueued", | ||
"tail", tail, "new-tail", nt, "head", head, "value", toString(holder.value), | ||
"value(rb.data[0])", toString(rb.data[0].value), | ||
"value(rb.data[1])", toString(rb.data[1].value)) | ||
} | ||
return | ||
} | ||
} | ||
|
||
func (rb *orbuf[T]) Get() (item T, err error) { return rb.Dequeue() } //nolint:revive | ||
|
||
func (rb *orbuf[T]) Dequeue() (item T, err error) { //nolint:revive | ||
var tail, head, nh uint32 | ||
var holder *rbItem[T] | ||
for { | ||
// var quad uint64 | ||
// quad = atomic.LoadUint64((*uint64)(unsafe.Pointer(&rb.head))) | ||
// head = (uint32)(quad & MaxUint32_64) | ||
// tail = (uint32)(quad >> 32) | ||
head = atomic.LoadUint32(&rb.head) | ||
tail = atomic.LoadUint32(&rb.tail) | ||
|
||
isEmpty := head == tail | ||
if isEmpty { | ||
if head == MaxUint32 { | ||
err = ErrQueueNotReady | ||
return | ||
} | ||
err = ErrQueueEmpty | ||
return | ||
} | ||
|
||
holder = &rb.data[head] | ||
|
||
nh = (head + 1) & rb.capModMask | ||
atomic.CompareAndSwapUint32(&rb.head, head, nh) | ||
retry: | ||
if !atomic.CompareAndSwapUint64(&holder.readWrite, 1, 3) { //nolint:gomnd | ||
if atomic.LoadUint64(&holder.readWrite) == 1 { | ||
goto retry // sometimes, short circuit | ||
} | ||
runtime.Gosched() // time to time | ||
continue | ||
} | ||
|
||
if rb.initializer != nil { | ||
item = rb.initializer.CloneOut(&holder.value) | ||
} else { | ||
item = holder.value | ||
// holder.value = zero | ||
} | ||
if !atomic.CompareAndSwapUint64(&holder.readWrite, 3, 0) { //nolint:gomnd | ||
err = ErrRaced // runtime.Gosched() // never happens | ||
} | ||
|
||
if state.VerboseEnabled { | ||
state.Verbose("[ringbuf][GET] states are:", | ||
"cap", rb.Cap(), "qty", rb.qty(head, tail), "tail", tail, "head", head, "new-head", nh, "item", toString(item)) | ||
} | ||
|
||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package mpmc | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestOverlappedRingBuf_Put_OneByOne(t *testing.T) { //nolint:revive | ||
var err error | ||
rb := NewOverlappedRingBuffer(NLtd, WithDebugMode[uint32](true)) | ||
size := rb.Cap() - 1 | ||
// t.Logf("Ring Buffer created, capacity = %v, real size: %v", size+1, size) | ||
defer rb.Close() | ||
|
||
for i := uint32(0); i < size; i++ { | ||
err = rb.Enqueue(i) | ||
if err != nil { | ||
t.Fatalf("faild on i=%v. err: %+v", i, err) | ||
// } else { | ||
// t.Logf("%5d. '%v' put, quantity = %v.", i, i, fast.Quantity()) | ||
} | ||
t.Logf(" %3d. ringbuf elements -> %v", i, rb) | ||
} | ||
|
||
for i := size; i < size+size; i++ { | ||
err = rb.Put(i) | ||
if errors.Is(err, ErrQueueFull) { | ||
t.Fatalf("> %3d. expect ErrQueueFull but no error raised. err: %+v", i, err) | ||
} | ||
t.Logf(" %3d. ringbuf elements -> %v", i, rb) | ||
} | ||
|
||
var it any | ||
it, err = rb.Dequeue() | ||
_, _ = it, err | ||
t.Logf(" %3d. ringbuf elements -> %v", -1, rb) | ||
it, err = rb.Dequeue() | ||
_, _ = it, err | ||
t.Logf(" %3d. ringbuf elements -> %v", -1, rb) | ||
if fmt.Sprintf("%v", rb) != "[17,18,19,20,21,22,23,24,25,26,27,28,29,]/13" { | ||
t.Fatalf("faild: expecting elements are: [17,18,19,20,21,22,23,24,25,26,27,28,29,]/13") | ||
} | ||
|
||
for i := size * 2; i < size*3+2; i++ { | ||
err = rb.Put(i) | ||
if errors.Is(err, ErrQueueFull) { | ||
t.Fatalf("> %3d. expect ErrQueueFull but no error raised. err: %+v", i, err) | ||
} | ||
t.Logf(" %3d. ringbuf elements -> %v", i, rb) | ||
} | ||
if fmt.Sprintf("%v", rb) != "[32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,]/15" { | ||
t.Fatalf("faild: expecting elements are: [32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,]/15") | ||
} | ||
|
||
for i := 0; ; i++ { | ||
it, err = rb.Dequeue() | ||
if err != nil { | ||
if errors.Is(err, ErrQueueEmpty) { | ||
break | ||
} | ||
t.Fatalf("faild on i=%v. err: %+v. item: %v", i, err, it) | ||
// } else { | ||
// t.Logf("< %3d. '%v' GOT, quantity = %v.", i, it, fast.Quantity()) | ||
} | ||
if rb.Size() == 0 { | ||
// t.Log("empty ring buffer elements") | ||
if fmt.Sprintf("%v", rb) != "[]/0" { | ||
t.Fatalf("faild: expecting elements are: []/0") | ||
} | ||
} | ||
t.Logf(" %3d. ringbuf elements -> %v", i, rb) | ||
} | ||
|
||
it, err = rb.Dequeue() | ||
if err == nil { | ||
t.Fatalf("faild: Dequeue on an empty ringbuf should return an ErrQueueEmpty object.") | ||
} | ||
} |
Oops, something went wrong.