diff options
author | Ben Gamari <ben@smart-cactus.org> | 2015-10-22 22:16:46 +0200 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2015-11-23 15:40:44 +0100 |
commit | a3a8ce6e60466cb3742506c7d7bfa1a5b1012728 (patch) | |
tree | c56c2c1a02cae391f6bb332a99c0e5284daade8c /rts/Pool.h | |
parent | 36b213903db2363c2153f93c78bce079083f3d68 (diff) | |
download | haskell-a3a8ce6e60466cb3742506c7d7bfa1a5b1012728.tar.gz |
rts: Add simple resource pool
Diffstat (limited to 'rts/Pool.h')
-rw-r--r-- | rts/Pool.h | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/rts/Pool.h b/rts/Pool.h new file mode 100644 index 0000000000..d1aeab5661 --- /dev/null +++ b/rts/Pool.h @@ -0,0 +1,54 @@ +#include "Rts.h" + +/* + * Resource pools + * + * This module provides an implementation of a simple thread-safe resource pool. + * A pool is a shared set of resources, the size of which is bounded by a + * maximum size (0 indicates unbounded). Consumers can request a resource from + * the pool with pool_take and, when finished can return it to the pool with + * pool_release. Resources will be lazily allocated with alloc_fn as necessary. + * If the pool is already at its maximum size when a request is made, pool_take + * will block until a resource is freed. + * + * The pool will free resources such that there are at most desired_size + * resources in the pool when all resources have been released. + * + * invariant: desired_size <= max_size + * + */ + +typedef void *(*alloc_thing_fn)(void); +typedef void (*free_thing_fn)(void *); +typedef struct Pool_ Pool; + +/* Create a pool of things. */ +Pool *poolInit(nat max_size, nat desired_size, + alloc_thing_fn alloc_fn, free_thing_fn free_fn); + +/* Free a pool. Returns 0 on success or 1 on failure due to things + * belonging to the pool currently being claimed. */ +int poolFree(Pool *pool); + +/* Set the maximum size of a pool (0 indicates unbounded). desired_size will be + * lowered if necessary. */ +void poolSetMaxSize(Pool *pool, nat size); + +/* Get the maximum size of a pool */ +nat poolGetMaxSize(Pool *pool); + +/* Set the desired size of a pool */ +void poolSetDesiredSize(Pool *pool, nat size); + +/* Get the desired size of a pool */ +nat poolGetDesiredSize(Pool *pool); + +/* Grab an available thing from a pool */ +void *poolTake(Pool *pool); + +/* Release a thing back to the pool from which it was taken */ +void poolRelease(Pool *pool, void *thing); + +/* Invalidate all currently allocated resources. Things which are currently + * taken will be freed upon release instead of being returned to the pool. */ +void poolFlush(Pool *pool); |