libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_pg.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_pg.h
7 * Process groups: named, membership sets of pids for pub/sub and
8 * fan-out (the Erlang :pg / Phoenix.PubSub / Discord-presence
9 * pattern). A group is a duplicate-key registry key; join adds the
10 * caller's pid, leave removes it, and xtc_pg_send broadcasts a
11 * message to every current member.
12 *
13 * Single-node only. Cross-node process groups need the (unbuilt)
14 * distributed module; this is the local substrate.
15 *
16 * A group set is backed by an xtc_reg_t (see xtc_reg_register_dup),
17 * so it composes with the app registry: pass xtc_app_registry(app)
18 * to share one namespace, or a dedicated xtc_reg_t for isolation.
19 */
20
21#ifndef XTC_PG_H
22#define XTC_PG_H
23
24#include "xtc_export.h"
25
26#include <stddef.h>
27
28#include "xtc.h"
29#include "xtc_proc.h"
30#include "xtc_reg.h"
31
32/*
33 * PUBLIC: int xtc_pg_join __P((xtc_reg_t *, const char *, xtc_pid_t));
34 * PUBLIC: int xtc_pg_leave __P((xtc_reg_t *, const char *, xtc_pid_t));
35 * PUBLIC: int xtc_pg_members __P((xtc_reg_t *, const char *, int (*)(xtc_pid_t, void *), void *));
36 * PUBLIC: int xtc_pg_send __P((xtc_reg_t *, const char *, const void *, size_t));
37 * PUBLIC: int xtc_pg_send_owned __P((xtc_reg_t *, const char *, void *, size_t));
38 */
39
40/* Add `pid` to group `group` (idempotent). Typically the caller joins
41 * itself with xtc_self(). */
42XTC_API int xtc_pg_join(xtc_reg_t *reg, const char *group, xtc_pid_t pid);
43
44/* Remove `pid` from `group`. XTC_E_INVAL if not a member. */
45XTC_API int xtc_pg_leave(xtc_reg_t *reg, const char *group, xtc_pid_t pid);
46
47/* Visit every member pid of `group` (callback runs under the registry
48 * lock -- keep it brief; do not re-enter the registry). A nonzero
49 * callback return stops the walk. Returns the member count. */
50XTC_API int xtc_pg_members(xtc_reg_t *reg, const char *group,
51 int (*fn)(xtc_pid_t pid, void *user), void *user);
52
53/* Broadcast `msg` (a copy) to every current member of `group`. Returns
54 * the number of members the message was sent to. A send to an
55 * already-dead member is a harmless no-op (its DOWN reaping is the
56 * embedder's responsibility until the registry monitor lands). */
57XTC_API int xtc_pg_send(xtc_reg_t *reg, const char *group,
58 const void *msg, size_t size);
59
60/* xtc_pg_send of an xtc_malloc'd buffer the caller gives away: every
61 * member but the last gets a copy, the last gets `buf` itself
62 * (xtc_send_owned), so a group of one member costs no copy and a group
63 * of N costs N - 1. `buf` is the library's on return whatever happens
64 * (moved, or freed when no member took it); do not touch it after the
65 * call. Returns the number of members the message was sent to. */
66XTC_API int xtc_pg_send_owned(xtc_reg_t *reg, const char *group,
67 void *buf, size_t size);
68
69#endif /* XTC_PG_H */