56typedef uint64_t xtc_tnt_handle_t;
58#define XTC_TNT_HANDLE_NONE ((xtc_tnt_handle_t)0)
60#define XTC_TNT_SHARD_BITS 8
61#define XTC_TNT_TYPE_BITS 8
62#define XTC_TNT_SLOT_BITS 20
63#define XTC_TNT_GEN_BITS 28
65#define XTC_TNT_SLOT_MAX ((1u << XTC_TNT_SLOT_BITS) - 1u)
66#define XTC_TNT_GEN_MASK ((1u << XTC_TNT_GEN_BITS) - 1u)
68static inline xtc_tnt_handle_t
69xtc_tnt_handle_make(uint8_t shard, uint8_t type, uint32_t slot, uint32_t gen)
71 return ((xtc_tnt_handle_t)shard << (XTC_TNT_TYPE_BITS + XTC_TNT_SLOT_BITS +
73 ((xtc_tnt_handle_t)type << (XTC_TNT_SLOT_BITS + XTC_TNT_GEN_BITS)) |
74 ((xtc_tnt_handle_t)(slot & XTC_TNT_SLOT_MAX) << XTC_TNT_GEN_BITS) |
75 ((xtc_tnt_handle_t)(gen & XTC_TNT_GEN_MASK));
79xtc_tnt_handle_shard(xtc_tnt_handle_t h)
81 return (uint8_t)(h >> (XTC_TNT_TYPE_BITS + XTC_TNT_SLOT_BITS + XTC_TNT_GEN_BITS));
85xtc_tnt_handle_type(xtc_tnt_handle_t h)
87 return (uint8_t)((h >> (XTC_TNT_SLOT_BITS + XTC_TNT_GEN_BITS)) & 0xffu);
91xtc_tnt_handle_slot(xtc_tnt_handle_t h)
93 return (uint32_t)((h >> XTC_TNT_GEN_BITS) & XTC_TNT_SLOT_MAX);
97xtc_tnt_handle_gen(xtc_tnt_handle_t h)
99 return (uint32_t)(h & XTC_TNT_GEN_MASK);
110typedef enum xtc_tnt_transition_kind {
113 XTC_TNT_WAIT_MESSAGE,
116} xtc_tnt_transition_kind_t;
119typedef enum xtc_tnt_fault_reason {
120 XTC_TNT_FAULT_NONE = 0,
121 XTC_TNT_FAULT_SPAWN_FAILED,
122 XTC_TNT_FAULT_UNIMPLEMENTED_TRANSITION,
123 XTC_TNT_FAULT_INIT_FAILED,
124 XTC_TNT_FAULT_CONTRACT_VIOLATION
125} xtc_tnt_fault_reason_t;
127typedef struct xtc_tnt_transition {
128 xtc_tnt_transition_kind_t kind;
129 xtc_tnt_fault_reason_t fault_reason;
132#define XTC_TNT_TRANSITION_DONE ((xtc_tnt_transition_t){ XTC_TNT_DONE, XTC_TNT_FAULT_NONE })
133#define XTC_TNT_TRANSITION_YIELD ((xtc_tnt_transition_t){ XTC_TNT_YIELD, XTC_TNT_FAULT_NONE })
134#define XTC_TNT_TRANSITION_WAIT_MESSAGE ((xtc_tnt_transition_t){ XTC_TNT_WAIT_MESSAGE, XTC_TNT_FAULT_NONE })
135#define XTC_TNT_TRANSITION_WAIT_IO ((xtc_tnt_transition_t){ XTC_TNT_WAIT_IO, XTC_TNT_FAULT_NONE })
138xtc_tnt_transition_to_crash(xtc_tnt_fault_reason_t reason)
153#define XTC_TNT_USER_TAG_BASE 0x0040u
157#define XTC_TNT_IO_TAG_ACCEPT_COMPLETE 0x0012u
158#define XTC_TNT_IO_TAG_CONNECT_COMPLETE 0x0013u
159#define XTC_TNT_IO_TAG_SEND_COMPLETE 0x0014u
160#define XTC_TNT_IO_TAG_RECV_COMPLETE 0x0015u
161#define XTC_TNT_IO_TAG_CLOSE_COMPLETE 0x0018u
164#define XTC_TNT_TAG_TIMER 0x0002u
165#define XTC_TNT_TAG_SHUTDOWN 0x0003u
167#define XTC_TNT_MAX_PAYLOAD_SIZE 96
168#define XTC_TNT_MAX_INIT_ARGS_SIZE 64
170typedef struct xtc_tnt_message {
178 xtc_tnt_handle_t source;
179 uint16_t payload_size;
181 uint8_t payload[XTC_TNT_MAX_PAYLOAD_SIZE];
206typedef struct xtc_tnt_type {
211 size_t working_memory_size;
212 uint32_t mailbox_capacity;
213 uint32_t budget_weight;
214 xtc_tnt_init_fn init_fn;
215 xtc_tnt_handler_fn handler_fn;
224typedef struct xtc_tnt_spec {
229 uint32_t scratch_size;
230 uint32_t recv_buf_size;
243typedef enum xtc_tnt_send_result {
245 XTC_TNT_SEND_MAILBOX_FULL,
246 XTC_TNT_SEND_POOL_EXHAUSTED,
247 XTC_TNT_SEND_STALE_HANDLE
248} xtc_tnt_send_result_t;
251typedef enum xtc_tnt_spawn_error {
252 XTC_TNT_SPAWN_OK = 0,
253 XTC_TNT_SPAWN_ARENA_FULL,
254 XTC_TNT_SPAWN_TYPE_NOT_ALLOCATED,
255 XTC_TNT_SPAWN_INIT_FAILED
256} xtc_tnt_spawn_error_t;
259typedef enum xtc_tnt_io_result {
263} xtc_tnt_io_result_t;
269xtc_tnt_send_result_t xtc_tnt_send(xtc_tnt_handle_t to, uint16_t tag,
270 const void *payload,
size_t payload_size);
275xtc_tnt_spawn_error_t xtc_tnt_spawn(uint8_t type_id,
const void *args,
276 size_t args_size, xtc_tnt_handle_t *out_handle);
284xtc_tnt_io_result_t xtc_tnt_submit_recv(
int fd);
289xtc_tnt_io_result_t xtc_tnt_io_send(
int fd,
const void *buffer,
size_t len);
303xtc_tnt_io_result_t xtc_tnt_io_send_owned(
int fd,
void *buffer,
size_t len);
307xtc_tnt_io_result_t xtc_tnt_submit_close(
int fd);
311void xtc_tnt_register_timer(uint64_t duration_ns, uint16_t tag);
314xtc_tnt_handle_t xtc_tnt_self(
void);
317uint8_t xtc_tnt_shard_id(
void);
322void *xtc_tnt_scratch_arena(
size_t size);
328#define xtc_tnt_self_as(T, self_raw) ((T *)(self_raw))
331#define xtc_tnt_payload_as(T, msg) ((T *)((msg)->body.user.payload))
344void xtc_tnt_stop(
void);
350xtc_tnt_spawn_error_t xtc_tnt_spawn_on(uint8_t shard, uint8_t type_id,
351 const void *args,
size_t args_size);