libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_pool.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * @stability stable
4 * Use of this source code is governed by the ISC License.
5 *
6 * src/inc/xtc_pool.h
7 * A bounded resource pool: a fixed set of caller-owned resources
8 * (connections, buffers, handles) that fibers check out and return.
9 * Checkout blocks (up to a timeout) when all resources are busy, so
10 * the pool doubles as a concurrency limiter. This is the checkout /
11 * return half of the connection-pool pattern; the supervisor's
12 * bounded dynamic pool (xtc_sup_opts_t.max_children) is the
13 * spawn-workers half.
14 *
15 * The pool does not create or own the resources: the caller adds
16 * them with xtc_pool_add before use and is responsible for freeing
17 * them after xtc_pool_destroy. The pool only tracks which are free
18 * vs checked out, and blocks a checkout until one is free.
19 */
20
21#ifndef XTC_POOL_H
22#define XTC_POOL_H
23
24#include "xtc_export.h"
25
26#include <stddef.h>
27#include <stdint.h>
28
29#include "xtc.h"
30
31typedef struct xtc_pool xtc_pool_t;
32
33/*
34 * PUBLIC: int xtc_pool_create __P((size_t, xtc_pool_t **));
35 * PUBLIC: void xtc_pool_destroy __P((xtc_pool_t *));
36 * PUBLIC: int xtc_pool_add __P((xtc_pool_t *, void *));
37 * PUBLIC: int xtc_pool_checkout __P((xtc_pool_t *, int64_t, void **));
38 * PUBLIC: int xtc_pool_checkin __P((xtc_pool_t *, void *));
39 * PUBLIC: size_t xtc_pool_available __P((const xtc_pool_t *));
40 * PUBLIC: size_t xtc_pool_capacity __P((const xtc_pool_t *));
41 */
42
43/* Create a pool sized for `capacity` resources. Add resources with
44 * xtc_pool_add before checking any out. */
45XTC_API int xtc_pool_create(size_t capacity, xtc_pool_t **out);
46
47/* Destroy the pool. Does NOT free the resources -- the caller owns
48 * them. It is the caller's responsibility not to destroy a pool with
49 * resources still checked out. */
50XTC_API void xtc_pool_destroy(xtc_pool_t *p);
51
52/* Add a resource to the pool (as free/available). Returns XTC_E_RESOURCE
53 * if the pool is already at capacity. */
54XTC_API int xtc_pool_add(xtc_pool_t *p, void *resource);
55
56/* Check out a free resource into *out. Blocks the calling fiber until
57 * one is free or `timeout_ns` elapses (XTC_E_AGAIN). timeout_ns == 0
58 * means try-once (XTC_E_AGAIN immediately if none free); a negative
59 * timeout blocks indefinitely. */
60XTC_API int xtc_pool_checkout(xtc_pool_t *p, int64_t timeout_ns, void **out);
61
62/* Return a previously checked-out resource, unblocking one waiter.
63 * Returns XTC_E_INVAL if `resource` was not checked out from this pool. */
64XTC_API int xtc_pool_checkin(xtc_pool_t *p, void *resource);
65
66/* Number of resources currently free (available for checkout). */
67XTC_API size_t xtc_pool_available(const xtc_pool_t *p);
68
69/* Total resources added to the pool (free + checked out). */
70XTC_API size_t xtc_pool_capacity(const xtc_pool_t *p);
71
72#endif /* XTC_POOL_H */