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