Acquired

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().

Members

Aliases

Elem
alias Elem = T*

Type of a new resource. (Differs for reference / value types.)

Elem
alias Elem = T
Undocumented in source.

Functions

acquire
Elem acquire(Elem new_t)

Gets a new T.

initialise
void initialise(FreeList!(ubyte[]) buffer_pool, FreeList!(T) t_pool)

Initialises this instance. (No other methods may be called before calling this method.)

relinquishAll
void relinquishAll()

Relinquishes all shared resources acquired by this instance.

Parameters

T

type of resource

Examples

1 // Type of a specialised resource which may be required by an execution
2 // context.
3 struct MyResource
4 {
5 }
6 
7 // Demonstrates how a typical global shared resources container should look.
8 // A single instance of this would be owned at the top level of the app.
9 class SharedResources
10 {
11     import ocean.util.container.pool.FreeList;
12 
13     // The pool of untyped buffers required by Acquired.
14     private FreeList!(ubyte[]) buffers;
15 
16     // The pool of specialised resources required by Acquired.
17     private FreeList!(MyResource) myresources;
18 
19     this ( )
20     {
21         this.buffers = new FreeList!(ubyte[]);
22         this.myresources = new FreeList!(MyResource);
23     }
24 
25     // Objects of this class will be newed at scope and passed to execution
26     // contexts. This allows the context to acquire various shared resources
27     // and have them automatically relinquished when it exits.
28     class ContextResources
29     {
30         // Tracker of resources acquired by the context.
31         private Acquired!(MyResource) acquired_myresources;
32 
33         // Initialise the tracker in the ctor.
34         this ( )
35         {
36             this.acquired_myresources.initialise(this.outer.buffers,
37                 this.outer.myresources);
38         }
39 
40         // ...and be sure to relinquish all the acquired resources in the
41         // dtor.
42         ~this ( )
43         {
44             this.acquired_myresources.relinquishAll();
45         }
46 
47         // Public method to get a new resource, managed by the tracker.
48         public MyResource* getMyResource ( )
49         {
50             return this.acquired_myresources.acquire(new MyResource);
51         }
52     }
53 }
54 
55 // Demonstrates the usage of the shared resources and the context resources.
56 class Context
57 {
58     SharedResources resources;
59 
60     void entryPoint ( )
61     {
62         // New a ContextResources as scope, so that its dtor will be called
63         // at scope exit and all acquired resources relinquished.
64         scope acquired = this.resources.new ContextResources;
65 
66         // Acquire some resources.
67         auto r1 = acquired.getMyResource();
68         auto r2 = acquired.getMyResource();
69         auto r3 = acquired.getMyResource();
70     }
71 }

Meta