summaryrefslogtreecommitdiff
path: root/rts/Pool.h
blob: 5190ce8bccf05103feed78eb785255e287d7d362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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(uint32_t max_size, uint32_t 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, uint32_t size);

/* Get the maximum size of a pool */
uint32_t poolGetMaxSize(Pool *pool);

/* Set the desired size of a pool */
void poolSetDesiredSize(Pool *pool, uint32_t size);

/* Get the desired size of a pool */
uint32_t poolGetDesiredSize(Pool *pool);

/* Try to grab an available thing from a pool, returning NULL if no things
 * are available.
 */
void *poolTryTake(Pool *pool);

/* Grab an available thing from a pool. This will block if no elements are
 * available in the case of a threaded runtime or abort in a single-threaded
 * environment.
 */
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);