Class ScyllaRingBuffer<T>.SynchronizedScyllaRingBuffer
Thread-safe wrapper around ScyllaRingBuffer<T> that synchronizes all operations using a lock.
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public sealed class ScyllaRingBuffer<T>.SynchronizedScyllaRingBuffer : IScyllaCollection<T>, IScyllaCollection
Remarks
Every method and property accessor in this wrapper acquires SyncRoot before delegating to the inner ScyllaRingBuffer<T>. This provides mutual exclusion for all state-mutating and state-reading operations, making the wrapper safe to use from multiple threads simultaneously.
Compound operations (check-then-act) are still not atomic unless the caller holds SyncRoot for the duration of the compound operation.
Obtain a synchronized wrapper by calling AsSynchronized(object) or by using the fluent Synchronized(object) method on a ScyllaRingBuffer<T>.Builder.
Constructors
SynchronizedScyllaRingBuffer(ScyllaRingBuffer<T>, object)
Initializes a new instance of the synchronized wrapper around the specified ring buffer.
Declaration
public SynchronizedScyllaRingBuffer(ScyllaRingBuffer<T> inner, object syncRoot)
Parameters
| Type | Name | Description |
|---|---|---|
| ScyllaRingBuffer<T> | inner | The ring buffer to wrap. Must not be |
| object | syncRoot | The object to use as the exclusive lock. If |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
Properties
Capabilities
Gets the capability flags supported by this ring buffer instance.
Declaration
public ScyllaCollectionCapabilities Capabilities { get; }
Property Value
| Type | Description |
|---|---|
| ScyllaCollectionCapabilities | A combination of ScyllaCollectionCapabilities flags indicating which optional operations are available. This value is identical to the wrapped buffer's capabilities. |
Capacity
Gets the fixed maximum number of elements this ring buffer can hold.
Declaration
public int Capacity { get; }
Property Value
| Type | Description |
|---|---|
| int | The capacity set at construction time. This value never changes over the lifetime of the buffer. |
Count
Gets the current number of elements stored in the ring buffer.
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| int | A value in the range |
FreeCount
Gets the number of additional elements that can be written before the buffer is full.
Declaration
public int FreeCount { get; }
Property Value
| Type | Description |
|---|---|
| int | Equivalent to |
IsEmpty
Gets whether the ring buffer contains no elements.
Declaration
public bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
IsFull
Gets whether the ring buffer is currently at full capacity.
Declaration
public bool IsFull { get; }
Property Value
| Type | Description |
|---|---|
| bool |
OverwriteOnFull
Gets whether writing to a full buffer automatically overwrites the oldest element.
Declaration
public bool OverwriteOnFull { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
SyncRoot
Gets the object used as the exclusive lock for all synchronized operations.
Declaration
public object SyncRoot { get; }
Property Value
| Type | Description |
|---|---|
| object | The lock object provided at construction or, if |
ThreadSafety
Gets the thread-safety level provided by this wrapper.
Declaration
public ScyllaCollectionThreadSafety ThreadSafety { get; }
Property Value
| Type | Description |
|---|---|
| ScyllaCollectionThreadSafety | Always Synchronized, indicating that all operations are protected by SyncRoot. |
Methods
Clear()
Removes all elements from the ring buffer and resets it to an empty state. For reference types, clears backing-array slots to release GC references.
Declaration
public void Clear()
Contains(T, IEqualityComparer<T>)
Determines whether the ring buffer contains the specified item using a linear scan. O(n).
Declaration
public bool Contains(T item, IEqualityComparer<T> comparer = null)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to locate in the buffer. |
| IEqualityComparer<T> | comparer | Optional equality comparer to use for element comparisons. If |
Returns
| Type | Description |
|---|---|
| bool |
|
CopyTo(Span<T>)
Copies elements from the ring buffer into the provided span in oldest-to-newest order.
Declaration
public int CopyTo(Span<T> destination)
Parameters
| Type | Name | Description |
|---|---|---|
| Span<T> | destination | The span to copy elements into. Copying stops when either all elements have been copied or the span is full, whichever comes first. |
Returns
| Type | Description |
|---|---|
| int | The number of elements copied. |
CopyTo(T[], int)
Copies elements from the ring buffer into the provided array starting at the specified index, in oldest-to-newest order.
Declaration
public int CopyTo(T[] destination, int destinationIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | destination | The array to copy elements into. Must not be |
| int | destinationIndex | The zero-based index in |
Returns
| Type | Description |
|---|---|
| int | The number of elements copied. |
PeekAt(int)
Returns the item at the specified logical index without removing it.
Declaration
public T PeekAt(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | The zero-based logical index into the buffer. Must be in the range |
Returns
| Type | Description |
|---|---|
| T | The item stored at logical position |
Remarks
Logical index 0 corresponds to the oldest element (head of the FIFO queue),
and logical index Count - 1 corresponds to the newest element (most recently written).
This method maps the logical index to the correct physical position in the circular backing array.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when |
RemoveWhere(Predicate<T>)
Removes all items that satisfy the specified predicate, preserving the FIFO order of the remaining items. Performs a single O(n) pass over all live elements.
Declaration
public int RemoveWhere(Predicate<T> match)
Parameters
| Type | Name | Description |
|---|---|---|
| Predicate<T> | match | A predicate that returns |
Returns
| Type | Description |
|---|---|
| int | The number of elements removed. |
Remarks
After removal, the ring buffer is compacted in-place so that the surviving elements occupy contiguous logical positions starting from the current head. For reference types, vacated backing-array slots are cleared to release GC references.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
TryAdd(T)
Attempts to add an item to the ring buffer. Equivalent to TryWrite(T). Implements TryAdd(T) for polymorphic usage.
Declaration
public bool TryAdd(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to add. |
Returns
| Type | Description |
|---|---|
| bool |
|
TryPeek(out T)
Attempts to peek at the oldest item without removing it. Equivalent to TryPeekOldest(out T). Implements TryPeek(out T) for polymorphic usage.
Declaration
public bool TryPeek(out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | When this method returns, contains the oldest item if available, or |
Returns
| Type | Description |
|---|---|
| bool |
|
TryPeekAt(int, out T)
Attempts to return the item at the specified logical index without removing it.
Declaration
public bool TryPeekAt(int index, out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | The zero-based logical index into the buffer. Values outside |
| T | item | When this method returns, contains the item at |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
Logical index 0 corresponds to the oldest element (head of the FIFO queue),
and logical index Count - 1 corresponds to the newest element. Unlike PeekAt(int),
this method returns false instead of throwing when the index is out of range.
TryPeekNewest(out T)
Attempts to peek at the most recently written item without removing it.
Declaration
public bool TryPeekNewest(out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | When this method returns, contains the newest item if available, or |
Returns
| Type | Description |
|---|---|
| bool |
|
TryPeekOldest(out T)
Attempts to peek at the oldest item in the buffer without removing it.
Declaration
public bool TryPeekOldest(out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | When this method returns, contains the oldest item if available, or |
Returns
| Type | Description |
|---|---|
| bool |
|
TryRead(out T)
Attempts to read (remove) the oldest item from the ring buffer.
Declaration
public bool TryRead(out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | When this method returns, contains the oldest item if the operation succeeded, or
|
Returns
| Type | Description |
|---|---|
| bool |
|
TryRemove(out T)
Attempts to remove the oldest item from the ring buffer. Equivalent to TryRead(out T). Implements TryRemove(out T) for polymorphic usage.
Declaration
public bool TryRemove(out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | When this method returns, contains the oldest item if the operation succeeded, or
|
Returns
| Type | Description |
|---|---|
| bool |
|
TryWrite(T)
Attempts to write an item to the ring buffer.
Declaration
public bool TryWrite(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to write. |
Returns
| Type | Description |
|---|---|
| bool |
|