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 <stddef.h>
24
25#include "xtc.h"
26#include "xtc_proc.h"
27#include "xtc_reg.h"
28
29/*
30 * PUBLIC: int xtc_pg_join __P((xtc_reg_t *, const char *, xtc_pid_t));
31 * PUBLIC: int xtc_pg_leave __P((xtc_reg_t *, const char *, xtc_pid_t));
32 * PUBLIC: int xtc_pg_members __P((xtc_reg_t *, const char *, int (*)(xtc_pid_t, void *), void *));
33 * PUBLIC: int xtc_pg_send __P((xtc_reg_t *, const char *, const void *, size_t));
34 */
35
36/* Add `pid` to group `group` (idempotent). Typically the caller joins
37 * itself with xtc_self(). */
38int xtc_pg_join(xtc_reg_t *reg, const char *group, xtc_pid_t pid);
39
40/* Remove `pid` from `group`. XTC_E_INVAL if not a member. */
41int xtc_pg_leave(xtc_reg_t *reg, const char *group, xtc_pid_t pid);
42
43/* Visit every member pid of `group` (callback runs under the registry
44 * lock -- keep it brief; do not re-enter the registry). A nonzero
45 * callback return stops the walk. Returns the member count. */
46int xtc_pg_members(xtc_reg_t *reg, const char *group,
47 int (*fn)(xtc_pid_t pid, void *user), void *user);
48
49/* Broadcast `msg` (a copy) to every current member of `group`. Returns
50 * the number of members the message was sent to. A send to an
51 * already-dead member is a harmless no-op (its DOWN reaping is the
52 * embedder's responsibility until the registry monitor lands). */
53int xtc_pg_send(xtc_reg_t *reg, const char *group,
54 const void *msg, size_t size);
55
56#endif /* XTC_PG_H */