libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_reg.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_reg.h
6 * Process registry: name -> xtc_pid_t lookup. M10.5.
7 *
8 * Erlang's gen_server-via-name pattern: spawn a service, register
9 * it under a stable name, and the rest of the system finds it by
10 * name rather than by passing pids around. We provide this as a
11 * per-application table guarded by a mutex; entries are
12 * registered/unregistered explicitly.
13 */
14
15#ifndef XTC_REG_H
16#define XTC_REG_H
17
18#include <stddef.h>
19
20#include "xtc.h"
21#include "xtc_proc.h"
22
23typedef struct xtc_reg xtc_reg_t;
24
25/*
26 * PUBLIC: int xtc_reg_create __P((xtc_reg_t **));
27 * PUBLIC: void xtc_reg_destroy __P((xtc_reg_t *));
28 * PUBLIC: int xtc_reg_register __P((xtc_reg_t *, const char *, xtc_pid_t));
29 * PUBLIC: int xtc_reg_unregister __P((xtc_reg_t *, const char *));
30 * PUBLIC: int xtc_reg_whereis __P((xtc_reg_t *, const char *, xtc_pid_t *));
31 * PUBLIC: int xtc_reg_count __P((const xtc_reg_t *));
32 * PUBLIC: int xtc_reg_register_dup __P((xtc_reg_t *, const char *, xtc_pid_t));
33 * PUBLIC: int xtc_reg_unregister_pid __P((xtc_reg_t *, const char *, xtc_pid_t));
34 * PUBLIC: int xtc_reg_drop_pid __P((xtc_reg_t *, xtc_pid_t));
35 * PUBLIC: void xtc_reg_reaper __P((void *));
36 * PUBLIC: int xtc_reg_register_mon __P((xtc_reg_t *, const char *, xtc_pid_t));
37 * PUBLIC: int xtc_svr_call_name __P((xtc_reg_t *, const char *, const void *, size_t, void **, size_t *, int64_t));
38 * PUBLIC: int xtc_reg_members __P((xtc_reg_t *, const char *, int (*)(xtc_pid_t, void *), void *));
39 */
40int xtc_reg_create(xtc_reg_t **out);
41void xtc_reg_destroy(xtc_reg_t *r);
42
43/* Register name -> pid. Fails with XTC_E_INVAL if name already taken. */
44int xtc_reg_register(xtc_reg_t *r, const char *name, xtc_pid_t pid);
45
46/* Remove a name. Returns XTC_E_INVAL if not registered. */
47int xtc_reg_unregister(xtc_reg_t *r, const char *name);
48
49/* Look up a pid by name. Writes to *out_pid on success. */
50int xtc_reg_whereis(xtc_reg_t *r, const char *name, xtc_pid_t *out_pid);
51
52int xtc_reg_count(const xtc_reg_t *r);
53
54/* Duplicate-key (pub/sub, group-membership) registration: many pids may
55 * share one key. The substrate for process groups. Registering the
56 * same (key, pid) twice is idempotent. */
57int xtc_reg_register_dup(xtc_reg_t *r, const char *key, xtc_pid_t pid);
58
59/* Remove one (key, pid) duplicate-key entry (a group leave). */
60int xtc_reg_unregister_pid(xtc_reg_t *r, const char *key, xtc_pid_t pid);
61
62/* Remove `pid` from EVERY key it is registered under (unique names and
63 * all duplicate-key groups). The "process left everything" cleanup an
64 * embedder calls when a process exits or a connection closes, until the
65 * registry gains an automatic monitor-on-DOWN. Returns the number of
66 * entries removed. */
67int xtc_reg_drop_pid(xtc_reg_t *r, xtc_pid_t pid);
68
69/* The crash-aware registry. Spawn ONE proc with this body and the
70 * registry as its argument (xtc_proc_spawn(loop, xtc_reg_reaper, reg,
71 * ...)). It registers itself, then auto-drops any pid registered via
72 * xtc_reg_register_mon when that pid goes DOWN -- the automatic form of
73 * xtc_reg_drop_pid. Runs until its loop is torn down. */
74void xtc_reg_reaper(void *reg);
75
76/* Register `name` -> `pid` (like xtc_reg_register) AND, if a reaper proc
77 * is running, arrange for the entry to be auto-dropped when `pid` goes
78 * DOWN. With no reaper it is exactly xtc_reg_register (the caller may
79 * still xtc_reg_drop_pid manually). */
80int xtc_reg_register_mon(xtc_reg_t *r, const char *name, xtc_pid_t pid);
81
82/* Via-dispatch: look up `name` -> pid, then xtc_svr_call it. Returns
83 * XTC_E_NOTFOUND if the name is not registered, otherwise the result of
84 * xtc_svr_call. Lets a client address a gen_server by registered name
85 * instead of holding its pid (the Erlang {via, ...} / global name
86 * pattern). */
87int xtc_svr_call_name(xtc_reg_t *r, const char *name,
88 const void *req, size_t req_size,
89 void **out_reply, size_t *out_size,
90 int64_t timeout_ns);
91
92/* Visit every pid registered under `key`. The callback runs under the
93 * registry lock (keep it brief; do not re-enter the registry); a nonzero
94 * return stops the walk. Returns the number of members visited. */
95int xtc_reg_members(xtc_reg_t *r, const char *key,
96 int (*fn)(xtc_pid_t pid, void *user), void *user);
97
98#endif /* XTC_REG_H */