Problem Description

In application servers some resources are expensive and they must be shared. This is the case for a database connection, a frame buffer used for image processing, a connection to a remote server, and so on. The problem is to make available these scarce resources in such a way that:

a resource is used by only one thread at a time,

we can control the maximum number of resources used at a time,

we have some flexibility to define such maximum when configuring the application server,

and of course the final solution is thread safe.

The common pattern used in such situation is to use a thread-safe pool of objects. Objects are picked from the pool when needed and restored back to the pool when they are no longer used.

Java thread safe object pool

Let's see how to implement our object pool in Java. We will use a generic class declaration into which we define a fixed array of objects. The pool array is allocated by the constructor and we will assume it will never change (hence the final keyword).

public class Pool<T> { private final T[] objects; public Pool<T>(int size) { objects = new T[size]; } ... }

First, we need a getInstance method that picks an object from the pool. The method must be thread safe and it is protected by the synchronized keyword. It there is no object, it has to wait until an object is available. For this, the method invokes wait to sleep until another thread releases an object. To keep track of the number of available objects, we will use an available counter that is decremented each time an object is used.

private int available = 0; private int waiting = 0; public synchronized T getInstance() { while (available == 0) { waiting++; wait(); waiting--; } available--; return objects[available]; }

To know when to wakeup a thread, we keep track of the number of waiters in the waiting counter. A loop is also necessary to make sure we have an available object after being wakeup. Indeed, there is no guarantee that after being notified, we have an available object to return. The call to wait will release the lock on the pool and puts the thread is wait mode.

Releasing the object is provided by release . The object is put backed in the pool array and the available counter incremented. If some threads are waiting, one of them is awaken by calling notify .

public synchronized void release(T obj) { objects[available] = obj; available++; if (waiting) { notify(); } }

When the application is started, the pool is initialized and some pre-defined objects are inserted.

class Item { ... }; ... Pool<Item> pool = new Pool<Item>(10); for (int i = 0; i < 10; i++) { pool.release(new Item()); }

Ada thread safe pool

The Ada object pool will be defined in a generic package and we will use a protected type. The protected type will guarantee the thread safe behavior of the implementation by making sure that only one thread executes the procedures.

generic type Element_Type is private; package Util.Concurrent.Pools is type Element_Array_Access is private; Null_Element_Array : constant Element_Array_Access; ... private type Element_Array is array (Positive range <>) of Element_Type; type Element_Array_Access is access all Element_Array; Null_Element_Array : constant Element_Array_Access := null; end Util.Concurrent.Pools;

The Ada protected type is simple with three procedures, we get the Get_Instance and Release as in the Java implementation. The Set_Size will take care of allocating the pool array (a job done by the Java pool constructor).

protected type Pool is entry Get_Instance (Item : out Element_Type); procedure Release (Item : in Element_Type); procedure Set_Size (Capacity : in Positive); private Available : Natural := 0; Elements : Element_Array_Access := Null_Element_Array; end Pool;

First, the Get_Instance procedure is defined as an entry so that we can define a condition to enter in it. Indeed, we need at least one object in the pool. Since we keep track of the number of available objects, we will use it as the entry condition. Thanks to this entry condition, the Ada implementation is a lot easier.

protected body Pool is entry Get_Instance (Item : out Element_Type) when Available > 0 is begin Item := Elements (Available); Available := Available - 1; end Get_Instance; ... end Pool;

The Release operation is also easier as there is no need to wakeup any thread: the Ada runtime will do that for us.

protected body Pool is procedure Release (Item : in Element_Type) is begin Available := Available + 1; Elements (Available) := Item; end Release; end Pool;

The pool is instantiated:

type Connection is ...; package Connection_Pool is new Util.Concurrent.Pools (Connection);

And a pool object can be declared and initialized with some default object:

P : Connection_Pool.Pool; C : Connection; ... P.Set_Size (Capacity => 10); for I in 1 .. 10 loop ... P.Release (C); end loop;

References

util-concurrent-pools.ads

util-concurrent-pools.adb