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 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_pg.h
6 * Process groups: named, membership sets of pids for pub/sub and
7 * fan-out (the Erlang :pg / Phoenix.PubSub / Discord-presence
8 * pattern). A group is a duplicate-key registry key; join adds the
9 * caller's pid, leave removes it, and xtc_pg_send broadcasts a
10 * message to every current member.
11 *
12 * Single-node only. Cross-node process groups need the (unbuilt)
13 * distributed module; this is the local substrate.
14 *
15 * A group set is backed by an xtc_reg_t (see xtc_reg_register_dup),
16 * so it composes with the app registry: pass xtc_app_registry(app)
17 * to share one namespace, or a dedicated xtc_reg_t for isolation.
18 */
19
20#ifndef XTC_PG_H
21#define XTC_PG_H
22
23#include "xtc_export.h"
24
25#include <stddef.h>
26
27#include "xtc.h"
28#include "xtc_proc.h"
29#include "xtc_reg.h"
30
31/*
32 * PUBLIC: int xtc_pg_join __P((xtc_reg_t *, const char *, xtc_pid_t));
33 * PUBLIC: int xtc_pg_leave __P((xtc_reg_t *, const char *, xtc_pid_t));
34 * PUBLIC: int xtc_pg_members __P((xtc_reg_t *, const char *, int (*)(xtc_pid_t, void *), void *));
35 * PUBLIC: int xtc_pg_send __P((xtc_reg_t *, const char *, const void *, size_t));
36 */
37
38/* Add `pid` to group `group` (idempotent). Typically the caller joins
39 * itself with xtc_self(). */
40XTC_API int xtc_pg_join(xtc_reg_t *reg, const char *group, xtc_pid_t pid);
41
42/* Remove `pid` from `group`. XTC_E_INVAL if not a member. */
43XTC_API int xtc_pg_leave(xtc_reg_t *reg, const char *group, xtc_pid_t pid);
44
45/* Visit every member pid of `group` (callback runs under the registry
46 * lock -- keep it brief; do not re-enter the registry). A nonzero
47 * callback return stops the walk. Returns the member count. */
48XTC_API int xtc_pg_members(xtc_reg_t *reg, const char *group,
49 int (*fn)(xtc_pid_t pid, void *user), void *user);
50
51/* Broadcast `msg` (a copy) to every current member of `group`. Returns
52 * the number of members the message was sent to. A send to an
53 * already-dead member is a harmless no-op (its DOWN reaping is the
54 * embedder's responsibility until the registry monitor lands). */
55XTC_API int xtc_pg_send(xtc_reg_t *reg, const char *group,
56 const void *msg, size_t size);
57
58#endif /* XTC_PG_H */