ocean.util.container.pool.AcquiredResources

Helper structs for a specific execution context to acquire and relinquish resources from a shared pool.

(Examples of an "execution context" include Tasks or connection handlers.)

Several utilities are provided in this module, all of which build on top of FreeList: * Acquired: Tracks instances of a specific type acquired from the shared resources and automatically relinquishes them when the execution context exits. * AcquiredArraysOf: Tracks arrays of a specific type acquired from the shared resources and automatically relinquishes them when the execution context exits. * AcquiredSingleton: Tracks a singleton instance acquired from the shared resources and automatically relinquishes it when the execution context exits.

The normal approach to using these utilities is as follows: 1. Create a class to store the shared resources pools. Let's call it SharedResources. 2. Add your resource pools to SharedResources. A FreeList!(ubyte[]) is required, but you should add pools of other types you need as well. 3. New the resource pools in the constructor. 4. Create a nested class inside SharedResources. An instance of this class will be newed at scope inside each execution context, and will track the shared resources that the execution context has acquired. Let's call it AcquiredResources. 5. Add Acquired* private members to AcquiredResources, as required. There should be one member per type of resource that the execution context might need to acquire. 6. Initialise the acquired members in the constructor, and relinquish them in the destructor. 7. Add public getters for each type of resource that can be acquired. These should call the acquire method of the appropriate acquired member, and return the newly acquired instance to the user.

Usage examples: See documented unittests of Acquired, AcquiredArraysOf, and AcquiredSingleton.

Members

Structs

Acquired
struct Acquired(T)

Set of resources of the templated type acquired by an execution context. An external source of elements of this type -- a FreeList!(T) -- as well as a source of untyped buffers -- a FreeList!(ubyte[]) -- is required. When resources are acquired (via the acquire() method), they are requested from the free list and stored internally in an array. When the resources are no longer required, the relinquishAll() method will return them to the free list. Note that the array used to store the acquired resources is itself acquired from the free list of untyped buffers and relinquished by relinquishAll().

AcquiredArraysOf
struct AcquiredArraysOf(T)

Set of acquired arrays of the templated type acquired by an execution context. An external source of untyped arrays -- a FreeList!(ubyte[]) -- is required. When arrays are acquired (via the acquire() method), they are requested from the free list and stored internally in a container array. When the arrays are no longer required, the relinquishAll() method will return them to the free list. Note that the container array used to store the acquired arrays is also itself acquired from the free list and relinquished by relinquishAll().

AcquiredSingleton
struct AcquiredSingleton(T)

Singleton (per-execution context) acquired resource of the templated type. An external source of elements of this type -- a FreeList!(T) -- is required. When the singleton resource is acquired (via the acquire() method), it is requested from the free list and stored internally. All subsequent calls to acquire() return the same instance. When the resource is no longer required, the relinquish() method will return it to the free list.

Meta

License

Boost Software License Version 1.0. See LICENSE.txt for details.