Class ScyllaObjectPool<T>.SynchronizedScyllaObjectPool
Thread-safe wrapper around ScyllaObjectPool<T> that synchronizes all operations using a lock.
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public sealed class ScyllaObjectPool<T>.SynchronizedScyllaObjectPool : IScyllaCollection<T>, IScyllaCollection
Constructors
SynchronizedScyllaObjectPool(ScyllaObjectPool<T>, object)
Initializes a new instance of the synchronized wrapper around the specified pool.
Declaration
public SynchronizedScyllaObjectPool(ScyllaObjectPool<T> inner, object syncRoot)
Parameters
| Type | Name | Description |
|---|---|---|
| ScyllaObjectPool<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 inactive 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 |
IsEmpty
Gets whether the pool is empty.
Declaration
public bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
MaxSize
Gets the maximum number of inactive 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()
Removes all inactive instances from the pool and destroys them using
UnityEngine.Object.Destroy(UnityEngine.Object) or UnityEngine.Object.DestroyImmediate(UnityEngine.Object).
The onDestroy callback is invoked for each instance before destruction.
All operations are performed under the synchronized lock.
Declaration
public void Clear()
Clear(bool)
Removes all inactive instances from the pool. When destroy is true,
each instance is destroyed via UnityEngine.Object.Destroy(UnityEngine.Object) or
UnityEngine.Object.DestroyImmediate(UnityEngine.Object) and the onDestroy callback is invoked;
when false, the instances are simply forgotten without destruction.
All operations are performed under the synchronized lock.
Declaration
public void Clear(bool destroy)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | destroy | If true, destroy inactive instances; if false, just remove them from the pool. |
Contains(T)
Determines whether the pool currently contains the specified inactive instance. Uses reference identity to compare instances (O(n) scan).
Declaration
public bool Contains(T instance)
Parameters
| Type | Name | Description |
|---|---|---|
| T | instance | The instance to search for among inactive pool entries. |
Returns
| Type | Description |
|---|---|
| bool | True if found among inactive instances; false otherwise. |
CopyTo(Span<T>)
Copies inactive 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 inactive 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. Activates the returned Unity object by toggling
its underlying GameObject active (if applicable), then invokes the onGet callback.
If the pool is empty and AllowCreate is true, a new instance is created.
Declaration
public T Get()
Returns
| Type | Description |
|---|---|
| T | An active instance ready for use. Never returns null when AllowCreate is true. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when the pool is empty and AllowCreate is false. |
Prewarm(int)
Pre-creates and stores up to count inactive instances in the pool.
Stops early if the pool reaches MaxSize or AllowCreate is false.
Each created instance is deactivated before being stored.
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,
then deactivates the Unity object. If the pool is already at MaxSize,
the instance is destroyed instead and the onDestroy callback is invoked.
Declaration
public bool Release(T instance)
Parameters
| Type | Name | Description |
|---|---|---|
| T | instance | The instance to return. Must not be null. |
Returns
| Type | Description |
|---|---|
| bool | True if the instance was stored in the pool; false if it was destroyed due to capacity constraints. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
| InvalidOperationException | Thrown when |
TryAdd(T)
Attempts to add an instance to the pool by releasing it back into the inactive free-list. This is equivalent to calling Release(T). If the pool is at MaxSize, the instance is destroyed and false is returned.
Declaration
public bool TryAdd(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The instance to return to the pool. Must not be null. |
Returns
| Type | Description |
|---|---|
| bool | True if stored in the pool; false if destroyed due to capacity constraints. |
TryGet(out T)
Attempts to retrieve an instance from the pool. Activates the returned Unity object
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 null. |
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 inactive 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 inactive instance if one is available; otherwise null. |
Returns
| Type | Description |
|---|---|
| bool | True if an inactive instance is available; false if the pool is empty. |
TryRemove(out T)
Attempts to remove an instance from the pool by acquiring it from the inactive 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 null. |
Returns
| Type | Description |
|---|---|
| bool | True if an instance was retrieved or created; false if the pool is empty and AllowCreate is false. |