-
Notifications
You must be signed in to change notification settings - Fork 136
feat(audiotrack): lockless buffer #656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
anunaym14
wants to merge
2
commits into
main
Choose a base branch
from
am/lockless-queue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,331 @@ | ||
| package lockless_circular_buffer | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "time" | ||
| "unsafe" | ||
|
|
||
| "go.uber.org/atomic" | ||
| ) | ||
|
|
||
| type CircularBuffer[T any] struct { | ||
| buffer []T | ||
| head *atomic.Uint32 | ||
| tail *atomic.Uint32 | ||
| mask uint32 | ||
| size uint32 | ||
|
|
||
| // todo(anunaym14): arch-aware padding to avoid false sharing | ||
| // _padding [x]byte | ||
| } | ||
|
|
||
| func NewCircularBuffer[T any](capacity uint32) *CircularBuffer[T] { | ||
| // Ensure capacity is a power of 2 and at least 1 | ||
| // todo(anunaym14): cleanup | ||
| if capacity == 0 { | ||
| capacity = 1 | ||
| } else if (capacity & (capacity - 1)) != 0 { | ||
| capacity-- | ||
| capacity |= capacity >> 1 | ||
| capacity |= capacity >> 2 | ||
| capacity |= capacity >> 4 | ||
| capacity |= capacity >> 8 | ||
| capacity |= capacity >> 16 | ||
| capacity++ | ||
|
Comment on lines
+28
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bits.Len32 |
||
| } | ||
|
|
||
| return &CircularBuffer[T]{ | ||
| buffer: make([]T, capacity), | ||
| head: atomic.NewUint32(0), | ||
| tail: atomic.NewUint32(0), | ||
| mask: capacity - 1, | ||
| size: capacity, | ||
| } | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) Push(item T) { | ||
| backoffCounter := 0 | ||
| backoffMax := 32 | ||
|
|
||
| for { | ||
| tail := cb.tail.Load() | ||
| head := cb.head.Load() | ||
|
|
||
| nextTail := (tail + 1) & cb.mask | ||
| if nextTail == head { | ||
| if backoffCounter < backoffMax { | ||
| backoffCounter++ | ||
| continue | ||
| } | ||
| runtime.Gosched() | ||
| continue | ||
| } | ||
|
|
||
| if cb.tail.CompareAndSwap(tail, nextTail) { | ||
| cb.buffer[tail] = item | ||
| return | ||
| } | ||
|
|
||
| if backoffCounter > 0 { | ||
| backoffCounter-- | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) TryPush(item T) bool { | ||
| const maxAttempts = 5 | ||
|
|
||
| for i := 0; i < maxAttempts; i++ { | ||
| tail := cb.tail.Load() | ||
| head := cb.head.Load() | ||
|
|
||
| nextTail := (tail + 1) & cb.mask | ||
| if nextTail == head { | ||
| return false | ||
| } | ||
|
|
||
| if cb.tail.CompareAndSwap(tail, nextTail) { | ||
| cb.buffer[tail] = item | ||
| return true | ||
| } | ||
|
|
||
| if i > 1 { | ||
| runtime.Gosched() | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) PushTimeout(item T, timeout time.Duration) bool { | ||
| deadline := time.Now().Add(timeout) | ||
|
|
||
| for time.Now().Before(deadline) { | ||
| tail := cb.tail.Load() | ||
| head := cb.head.Load() | ||
|
|
||
| nextTail := (tail + 1) & cb.mask | ||
| if nextTail == head { | ||
| runtime.Gosched() | ||
| continue | ||
| } | ||
|
|
||
| if cb.tail.CompareAndSwap(tail, nextTail) { | ||
| cb.buffer[tail] = item | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) Pop() (T, bool) { | ||
| var zero T | ||
| const maxAttempts = 5 | ||
|
|
||
| for i := 0; i < maxAttempts; i++ { | ||
| head := cb.head.Load() | ||
| tail := cb.tail.Load() | ||
|
|
||
| if head == tail { | ||
| return zero, false | ||
| } | ||
|
|
||
| nextHead := (head + 1) & cb.mask | ||
| if cb.head.CompareAndSwap(head, nextHead) { | ||
| item := cb.buffer[head] | ||
| return item, true | ||
| } | ||
|
|
||
| if i > 1 { | ||
| runtime.Gosched() | ||
| } | ||
| } | ||
| return zero, false | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) PushBatch(items []T) int { | ||
| if len(items) == 0 { | ||
| return 0 | ||
| } | ||
|
|
||
| head := cb.head.Load() | ||
| tail := cb.tail.Load() | ||
|
|
||
| var availableSpace uint32 | ||
| if head <= tail { | ||
| availableSpace = cb.size - (tail - head) - 1 | ||
| } else { | ||
| availableSpace = head - tail - 1 | ||
| } | ||
|
|
||
| batchSize := uint32(len(items)) | ||
| if batchSize > availableSpace { | ||
| batchSize = availableSpace | ||
| } | ||
|
|
||
| if batchSize == 0 { | ||
| return 0 | ||
| } | ||
|
|
||
| pushed := uint32(0) | ||
| for pushed < batchSize { | ||
| tail = cb.tail.Load() | ||
| head = cb.head.Load() | ||
|
|
||
| if head <= tail { | ||
| availableSpace = cb.size - (tail - head) - 1 | ||
| } else { | ||
| availableSpace = head - tail - 1 | ||
| } | ||
|
|
||
| if availableSpace == 0 { | ||
| break | ||
| } | ||
|
|
||
| currentBatchSize := batchSize - pushed | ||
| if currentBatchSize > availableSpace { | ||
| currentBatchSize = availableSpace | ||
| } | ||
|
|
||
| newTail := (tail + currentBatchSize) & cb.mask | ||
|
|
||
| if cb.tail.CompareAndSwap(tail, newTail) { | ||
| for i := uint32(0); i < currentBatchSize; i++ { | ||
| slotIndex := (tail + i) & cb.mask | ||
| cb.buffer[slotIndex] = items[pushed+i] | ||
| } | ||
| pushed += currentBatchSize | ||
| } | ||
| } | ||
|
|
||
| return int(pushed) | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) PushBatchBlocking(items []T) { | ||
| remaining := items | ||
| for len(remaining) > 0 { | ||
| pushed := cb.PushBatch(remaining) | ||
| if pushed == 0 { | ||
| runtime.Gosched() | ||
| continue | ||
| } | ||
| remaining = remaining[pushed:] | ||
| } | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) PopBatch(maxItems int) (int, []T) { | ||
| if maxItems <= 0 { | ||
| return 0, nil | ||
| } | ||
|
|
||
| head := cb.head.Load() | ||
| tail := cb.tail.Load() | ||
|
|
||
| var availableItems uint32 | ||
| if tail >= head { | ||
| availableItems = tail - head | ||
| } else { | ||
| availableItems = cb.size - (head - tail) | ||
| } | ||
|
|
||
| batchSize := uint32(maxItems) | ||
| if batchSize > availableItems { | ||
| batchSize = availableItems | ||
| } | ||
|
|
||
| if batchSize == 0 { | ||
| return 0, nil | ||
| } | ||
|
|
||
| result := make([]T, 0, batchSize) | ||
| popped := uint32(0) | ||
|
|
||
| for popped < batchSize { | ||
| head = cb.head.Load() | ||
| tail = cb.tail.Load() | ||
|
|
||
| if tail >= head { | ||
| availableItems = tail - head | ||
| } else { | ||
| availableItems = cb.size - (head - tail) | ||
| } | ||
|
|
||
| if availableItems == 0 { | ||
| break | ||
| } | ||
|
|
||
| currentBatchSize := batchSize - popped | ||
| if currentBatchSize > availableItems { | ||
| currentBatchSize = availableItems | ||
| } | ||
|
|
||
| newHead := (head + currentBatchSize) & cb.mask | ||
|
|
||
| if cb.head.CompareAndSwap(head, newHead) { | ||
| for i := uint32(0); i < currentBatchSize; i++ { | ||
| slotIndex := (head + i) & cb.mask | ||
| result = append(result, cb.buffer[slotIndex]) | ||
| } | ||
|
Comment on lines
+265
to
+268
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be implemented in at most 2 copies? after head has been updated is the read range safe from concurrent writes? |
||
| popped += currentBatchSize | ||
| } | ||
| } | ||
|
|
||
| return int(popped), result | ||
| } | ||
|
|
||
| //go:noinline | ||
| func prefetch(addr unsafe.Pointer) { | ||
| _ = addr | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) AddPreloaded(items []T) int { | ||
| if len(items) == 0 { | ||
| return 0 | ||
| } | ||
|
|
||
| prefetch(unsafe.Pointer(cb.head)) | ||
| prefetch(unsafe.Pointer(cb.tail)) | ||
|
|
||
| return cb.PushBatch(items) | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) Size() uint32 { | ||
| head := cb.head.Load() | ||
| tail := cb.tail.Load() | ||
|
|
||
| if head == tail { | ||
| return 0 | ||
| } | ||
|
|
||
| if tail >= head { | ||
| return tail - head | ||
| } | ||
|
|
||
| return cb.size - (head - tail) | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) Capacity() uint32 { | ||
| return cb.size | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) IsEmpty() bool { | ||
| return cb.head.Load() == cb.tail.Load() | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) IsFull() bool { | ||
| head := cb.head.Load() | ||
| tail := cb.tail.Load() | ||
|
|
||
| return ((tail + 1) & cb.mask) == head | ||
| } | ||
|
|
||
| func (cb *CircularBuffer[T]) Clear() { | ||
| tail := cb.tail.Load() | ||
| for { | ||
| head := cb.head.Load() | ||
| if head == tail || cb.head.CompareAndSwap(head, tail) { | ||
| break | ||
| } | ||
| runtime.Gosched() | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are zero initialized automatically so you can avoid the extra indirection and skip initializing with New(0) if these are
atomic.Uint32instead of*atomic.Uint32