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