55typedef uint64_t xtc_tnt_handle_t;
57#define XTC_TNT_HANDLE_NONE ((xtc_tnt_handle_t)0)
59#define XTC_TNT_SHARD_BITS 8
60#define XTC_TNT_TYPE_BITS 8
61#define XTC_TNT_SLOT_BITS 20
62#define XTC_TNT_GEN_BITS 28
64#define XTC_TNT_SLOT_MAX ((1u << XTC_TNT_SLOT_BITS) - 1u)
65#define XTC_TNT_GEN_MASK ((1u << XTC_TNT_GEN_BITS) - 1u)
67static inline xtc_tnt_handle_t
68xtc_tnt_handle_make(uint8_t shard, uint8_t type, uint32_t slot, uint32_t gen)
70 return ((xtc_tnt_handle_t)shard << (XTC_TNT_TYPE_BITS + XTC_TNT_SLOT_BITS +
72 ((xtc_tnt_handle_t)type << (XTC_TNT_SLOT_BITS + XTC_TNT_GEN_BITS)) |
73 ((xtc_tnt_handle_t)(slot & XTC_TNT_SLOT_MAX) << XTC_TNT_GEN_BITS) |
74 ((xtc_tnt_handle_t)(gen & XTC_TNT_GEN_MASK));
78xtc_tnt_handle_shard(xtc_tnt_handle_t h)
80 return (uint8_t)(h >> (XTC_TNT_TYPE_BITS + XTC_TNT_SLOT_BITS + XTC_TNT_GEN_BITS));
84xtc_tnt_handle_type(xtc_tnt_handle_t h)
86 return (uint8_t)((h >> (XTC_TNT_SLOT_BITS + XTC_TNT_GEN_BITS)) & 0xffu);
90xtc_tnt_handle_slot(xtc_tnt_handle_t h)
92 return (uint32_t)((h >> XTC_TNT_GEN_BITS) & XTC_TNT_SLOT_MAX);
96xtc_tnt_handle_gen(xtc_tnt_handle_t h)
98 return (uint32_t)(h & XTC_TNT_GEN_MASK);
109typedef enum xtc_tnt_transition_kind {
112 XTC_TNT_WAIT_MESSAGE,
115} xtc_tnt_transition_kind_t;
118typedef enum xtc_tnt_fault_reason {
119 XTC_TNT_FAULT_NONE = 0,
120 XTC_TNT_FAULT_SPAWN_FAILED,
121 XTC_TNT_FAULT_UNIMPLEMENTED_TRANSITION,
122 XTC_TNT_FAULT_INIT_FAILED,
123 XTC_TNT_FAULT_CONTRACT_VIOLATION
124} xtc_tnt_fault_reason_t;
126typedef struct xtc_tnt_transition {
127 xtc_tnt_transition_kind_t kind;
128 xtc_tnt_fault_reason_t fault_reason;
131#define XTC_TNT_TRANSITION_DONE ((xtc_tnt_transition_t){ XTC_TNT_DONE, XTC_TNT_FAULT_NONE })
132#define XTC_TNT_TRANSITION_YIELD ((xtc_tnt_transition_t){ XTC_TNT_YIELD, XTC_TNT_FAULT_NONE })
133#define XTC_TNT_TRANSITION_WAIT_MESSAGE ((xtc_tnt_transition_t){ XTC_TNT_WAIT_MESSAGE, XTC_TNT_FAULT_NONE })
134#define XTC_TNT_TRANSITION_WAIT_IO ((xtc_tnt_transition_t){ XTC_TNT_WAIT_IO, XTC_TNT_FAULT_NONE })
137xtc_tnt_transition_to_crash(xtc_tnt_fault_reason_t reason)
152#define XTC_TNT_USER_TAG_BASE 0x0040u
156#define XTC_TNT_IO_TAG_ACCEPT_COMPLETE 0x0012u
157#define XTC_TNT_IO_TAG_CONNECT_COMPLETE 0x0013u
158#define XTC_TNT_IO_TAG_SEND_COMPLETE 0x0014u
159#define XTC_TNT_IO_TAG_RECV_COMPLETE 0x0015u
160#define XTC_TNT_IO_TAG_CLOSE_COMPLETE 0x0018u
163#define XTC_TNT_TAG_TIMER 0x0002u
164#define XTC_TNT_TAG_SHUTDOWN 0x0003u
166#define XTC_TNT_MAX_PAYLOAD_SIZE 96
167#define XTC_TNT_MAX_INIT_ARGS_SIZE 64
169typedef struct xtc_tnt_message {
177 xtc_tnt_handle_t source;
178 uint16_t payload_size;
180 uint8_t payload[XTC_TNT_MAX_PAYLOAD_SIZE];
205typedef struct xtc_tnt_type {
210 size_t working_memory_size;
211 uint32_t mailbox_capacity;
212 uint32_t budget_weight;
213 xtc_tnt_init_fn init_fn;
214 xtc_tnt_handler_fn handler_fn;
223typedef struct xtc_tnt_spec {
228 uint32_t scratch_size;
229 uint32_t recv_buf_size;
242typedef enum xtc_tnt_send_result {
244 XTC_TNT_SEND_MAILBOX_FULL,
245 XTC_TNT_SEND_POOL_EXHAUSTED,
246 XTC_TNT_SEND_STALE_HANDLE
247} xtc_tnt_send_result_t;
250typedef enum xtc_tnt_spawn_error {
251 XTC_TNT_SPAWN_OK = 0,
252 XTC_TNT_SPAWN_ARENA_FULL,
253 XTC_TNT_SPAWN_TYPE_NOT_ALLOCATED,
254 XTC_TNT_SPAWN_INIT_FAILED
255} xtc_tnt_spawn_error_t;
258typedef enum xtc_tnt_io_result {
262} xtc_tnt_io_result_t;
268xtc_tnt_send_result_t xtc_tnt_send(xtc_tnt_handle_t to, uint16_t tag,
269 const void *payload,
size_t payload_size);
274xtc_tnt_spawn_error_t xtc_tnt_spawn(uint8_t type_id,
const void *args,
275 size_t args_size, xtc_tnt_handle_t *out_handle);
283xtc_tnt_io_result_t xtc_tnt_submit_recv(
int fd);
288xtc_tnt_io_result_t xtc_tnt_io_send(
int fd,
const void *buffer,
size_t len);
291xtc_tnt_io_result_t xtc_tnt_submit_close(
int fd);
295void xtc_tnt_register_timer(uint64_t duration_ns, uint16_t tag);
298xtc_tnt_handle_t xtc_tnt_self(
void);
301uint8_t xtc_tnt_shard_id(
void);
306void *xtc_tnt_scratch_arena(
size_t size);
312#define xtc_tnt_self_as(T, self_raw) ((T *)(self_raw))
315#define xtc_tnt_payload_as(T, msg) ((T *)((msg)->body.user.payload))
328void xtc_tnt_stop(
void);
334xtc_tnt_spawn_error_t xtc_tnt_spawn_on(uint8_t shard, uint8_t type_id,
335 const void *args,
size_t args_size);