Fix numa001 failure with "too many NUMA nodes"
[ghc.git] / rts / Pool.h
1 #include "Rts.h"
2
3 /*
4 * Resource pools
5 *
6 * This module provides an implementation of a simple thread-safe resource pool.
7 * A pool is a shared set of resources, the size of which is bounded by a
8 * maximum size (0 indicates unbounded). Consumers can request a resource from
9 * the pool with pool_take and, when finished can return it to the pool with
10 * pool_release. Resources will be lazily allocated with alloc_fn as necessary.
11 * If the pool is already at its maximum size when a request is made, pool_take
12 * will block until a resource is freed.
13 *
14 * The pool will free resources such that there are at most desired_size
15 * resources in the pool when all resources have been released.
16 *
17 * invariant: desired_size <= max_size
18 *
19 */
20
21 typedef void *(*alloc_thing_fn)(void);
22 typedef void (*free_thing_fn)(void *);
23 typedef struct Pool_ Pool;
24
25 /* Create a pool of things. */
26 Pool *poolInit(uint32_t max_size, uint32_t desired_size,
27 alloc_thing_fn alloc_fn, free_thing_fn free_fn);
28
29 /* Free a pool. Returns 0 on success or 1 on failure due to things
30 * belonging to the pool currently being claimed. */
31 int poolFree(Pool *pool);
32
33 /* Set the maximum size of a pool (0 indicates unbounded). desired_size will be
34 * lowered if necessary. */
35 void poolSetMaxSize(Pool *pool, uint32_t size);
36
37 /* Get the maximum size of a pool */
38 uint32_t poolGetMaxSize(Pool *pool);
39
40 /* Set the desired size of a pool */
41 void poolSetDesiredSize(Pool *pool, uint32_t size);
42
43 /* Get the desired size of a pool */
44 uint32_t poolGetDesiredSize(Pool *pool);
45
46 /* Try to grab an available thing from a pool, returning NULL if no things
47 * are available.
48 */
49 void *poolTryTake(Pool *pool);
50
51 /* Grab an available thing from a pool. This will block if no elements are
52 * available in the case of a threaded runtime or abort in a single-threaded
53 * environment.
54 */
55 void *poolTake(Pool *pool);
56
57 /* Release a thing back to the pool from which it was taken */
58 void poolRelease(Pool *pool, void *thing);
59
60 /* Invalidate all currently allocated resources. Things which are currently
61 * taken will be freed upon release instead of being returned to the pool. */
62 void poolFlush(Pool *pool);