Class ScyllaGenericObjectPool<T>.SynchronizedScyllaGenericObjectPool
Thread-safe wrapper around ScyllaGenericObjectPool<T> that synchronizes all operations using a lock.
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public sealed class ScyllaGenericObjectPool<T>.SynchronizedScyllaGenericObjectPool : IScyllaCollection<T>, IScyllaCollection
Constructors
SynchronizedScyllaGenericObjectPool(ScyllaGenericObjectPool<T>, object)
Initializes a new instance of the synchronized wrapper around the specified pool.
Declaration
public SynchronizedScyllaGenericObjectPool(ScyllaGenericObjectPool<T> inner, object syncRoot)
Parameters
| Type | Name | Description |
|---|---|---|
| ScyllaGenericObjectPool<T> | inner | The pool to wrap. Must not be null. |
| object | syncRoot | The lock object to use for synchronization. If null, a new private lock is created. |
Properties
AllowCreate
Gets whether the pool creates new instances when empty.
Declaration
public bool AllowCreate { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Capabilities
Gets the capability flags supported by this pool instance.
Declaration
public ScyllaCollectionCapabilities Capabilities { get; }
Property Value
| Type | Description |
|---|---|
| ScyllaCollectionCapabilities |
Capacity
Gets the current free-list capacity.
Declaration
public int Capacity { get; }
Property Value
| Type | Description |
|---|---|
| int |
Count
Gets the number of available instances currently stored in the pool.
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| int |
CountAll
Gets the total number of instances created by the pool.
Declaration
public int CountAll { get; }
Property Value
| Type | Description |
|---|---|
| int |
DisposeOnDestroy
Gets whether instances implementing IDisposable are disposed when destroyed/forgotten.
Declaration
public bool DisposeOnDestroy { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsEmpty
Gets whether the pool is empty.
Declaration
public bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
MaxSize
Gets the maximum number of available instances that can be stored.
Declaration
public int MaxSize { get; }
Property Value
| Type | Description |
|---|---|
| int |
SyncRoot
Gets the synchronization root object used for coordinating operations.
Declaration
public object SyncRoot { get; }
Property Value
| Type | Description |
|---|---|
| object |
ThreadSafety
Gets the thread-safety guarantees provided by this wrapper.
Declaration
public ScyllaCollectionThreadSafety ThreadSafety { get; }
Property Value
| Type | Description |
|---|---|
| ScyllaCollectionThreadSafety |
Methods
Clear()
Clears the pool and destroys/disposes available instances.
Declaration
public void Clear()
Clear(bool)
Removes all available instances from the pool. When destroy is true,
each instance has the onDestroy callback invoked, and if DisposeOnDestroy is
enabled and the instance implements IDisposable, Dispose()
is called; when false, instances are simply forgotten without any callbacks.
All operations are performed under the synchronized lock.
Declaration
public void Clear(bool destroy)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | destroy | If true, invoke destroy callbacks and dispose instances; if false, just remove them from the pool. |
Contains(T, IEqualityComparer<T>)
Determines whether the pool currently contains the specified available instance.
For reference types, uses reference identity by default; for value types, uses
Default unless a custom comparer is supplied.
The check is O(n).
Declaration
public bool Contains(T instance, IEqualityComparer<T> comparer = null)
Parameters
| Type | Name | Description |
|---|---|---|
| T | instance | The instance to search for among available pool entries. |
| IEqualityComparer<T> | comparer | Optional equality comparer. If null, defaults to reference identity for reference types or Default for value types. |
Returns
| Type | Description |
|---|---|
| bool | True if found among available instances; false otherwise. |
CopyTo(Span<T>)
Copies available instances from the pool into the provided span in LIFO order (most recently released first). The pool state is not modified.
Declaration
public int CopyTo(Span<T> destination)
Parameters
| Type | Name | Description |
|---|---|---|
| Span<T> | destination | The destination span. Receives up to |
Returns
| Type | Description |
|---|---|
| int | The number of instances actually copied. |
CopyTo(T[], int)
Copies available instances from the pool into the provided array in LIFO order
(most recently released first), beginning at destinationIndex.
The pool state is not modified.
Declaration
public int CopyTo(T[] destination, int destinationIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | destination | The destination array. Must not be null. |
| int | destinationIndex | The zero-based index in |
Returns
| Type | Description |
|---|---|
| int | The number of instances actually copied. |
Get()
Retrieves an instance from the pool and invokes the onGet callback on success.
If the pool is empty and AllowCreate is true, a new instance is created.
Declaration
public T Get()
Returns
| Type | Description |
|---|---|
| T | An instance ready for use. Never returns |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when the pool is empty and AllowCreate is false. |
Prewarm(int)
Pre-creates and stores up to count available instances in the pool.
Stops early if the pool reaches MaxSize or AllowCreate is false.
Declaration
public int Prewarm(int count)
Parameters
| Type | Name | Description |
|---|---|---|
| int | count | The desired number of instances to create. Values of zero or less return 0 immediately. |
Returns
| Type | Description |
|---|---|
| int | The number of instances actually created and stored. |
Release(T)
Returns a previously retrieved instance back to the pool. Invokes the onRelease callback.
If the pool is already at MaxSize, the instance is destroyed or forgotten instead,
invoking the onDestroy callback and optionally calling Dispose()
when DisposeOnDestroy is enabled.
Declaration
public bool Release(T instance)
Parameters
| Type | Name | Description |
|---|---|---|
| T | instance | The instance to return. |
Returns
| Type | Description |
|---|---|
| bool | True if the instance was stored in the pool; false if it was destroyed or forgotten due to capacity constraints. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when |
TryAdd(T)
Attempts to add an instance to the pool by releasing it back into the available free-list. This is equivalent to calling Release(T). If the pool is at MaxSize, the instance is destroyed or forgotten (with optional Dispose() if DisposeOnDestroy is true) and false is returned.
Declaration
public bool TryAdd(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The instance to return to the pool. |
Returns
| Type | Description |
|---|---|
| bool | True if stored in the pool; false if destroyed or forgotten due to capacity constraints. |
TryGet(out T)
Attempts to retrieve an instance from the pool and invokes the onGet callback on success.
If the pool is empty and AllowCreate is true, a new instance is created.
Declaration
public bool TryGet(out T instance)
Parameters
| Type | Name | Description |
|---|---|---|
| T | instance | When this method returns, contains the retrieved or created instance if successful;
otherwise |
Returns
| Type | Description |
|---|---|
| bool | True if an instance was retrieved or created; false if the pool is empty and AllowCreate is false. |
TryPeek(out T)
Attempts to peek at the next available instance at the top of the free-list without removing it. The pool state is not modified by this operation.
Declaration
public bool TryPeek(out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | When this method returns, contains the next available instance if one is present;
otherwise |
Returns
| Type | Description |
|---|---|
| bool | True if an available instance is present; false if the pool is empty. |
TryRemove(out T)
Attempts to remove an instance from the pool by acquiring it from the available free-list. This is equivalent to calling TryGet(out T). If the pool is empty and AllowCreate is true, a new instance is created.
Declaration
public bool TryRemove(out T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | When this method returns, contains the retrieved or created instance if successful;
otherwise |
Returns
| Type | Description |
|---|---|
| bool | True if an instance was retrieved or created; false if the pool is empty and AllowCreate is false. |