46#ifndef BIO_DEFAULT_NUM_CLS_BUCKETS
47# define BIO_DEFAULT_NUM_CLS_BUCKETS 4
51#ifndef BIO_DEFAULT_THREAD_POOL_SIZE
52# define BIO_DEFAULT_THREAD_POOL_SIZE 2
56#ifndef BIO_DEFAULT_THREAD_POOL_QUEUE_SIZE
57# define BIO_DEFAULT_THREAD_POOL_QUEUE_SIZE 2
63# include "linux/platform.h"
65# include "windows/platform.h"
66#elif defined(__FreeBSD__)
67# include "freebsd/platform.h"
69# error "Unsupported platform"
77# define BIO_LIKELY(X) __builtin_expect(!!(X), 1)
79# define BIO_LIKELY(X) (X)
82#define BIO_DEFINE_LIST_LINK(NAME) \
83 typedef struct NAME##_s { \
84 struct NAME##_s* next; \
85 struct NAME##_s* prev; \
88#define BIO_LIST_APPEND(list, item) \
90 (item)->next = (list); \
91 (item)->prev = (list)->prev; \
92 (list)->prev->next = (item); \
93 (list)->prev = (item); \
96#define BIO_LIST_REMOVE(item) \
98 if ((item)->next != NULL) { \
99 (item)->next->prev = (item)->prev; \
100 (item)->prev->next = (item)->next; \
101 (item)->next = NULL; \
102 (item)->prev = NULL; \
106#define BIO_LIST_IS_EMPTY(list) \
107 ((list)->next == (list) || (list)->next == NULL)
109#define BIO_LIST_INIT(list) \
111 (list)->next = (list); \
112 (list)->prev = (list); \
115#define BIO_LIST_POP(list) \
116 BIO_LIST_REMOVE((list)->next)
118#define BIO_CONTAINER_OF(ptr, type, member) \
119 ((type *)((char *)(1 ? (ptr) : &((type *)0)->member) - offsetof(type, member)))
122# define BIO_ALIGN_TYPE long double
124# define BIO_ALIGN_TYPE max_align_t
132 int32_t next_handle_slot;
135typedef struct mco_coro mco_coro;
137BIO_DEFINE_LIST_LINK(bio_signal_link);
138BIO_DEFINE_LIST_LINK(bio_logger_link);
139BIO_DEFINE_LIST_LINK(bio_monitor_link);
140BIO_DEFINE_LIST_LINK(bio_cls_link);
145 bio_signal_link_t link;
157 _Alignas(BIO_ALIGN_TYPE)
char data[];
173 bio_cls_link_t* cls_buckets;
178 bio_signal_link_t pending_signals;
179 bio_monitor_link_t monitors;
181 int num_blocking_signals;
186 bio_monitor_link_t link;
191typedef struct bio_worker_thread_s bio_worker_thread_t;
210 int32_t handle_capacity;
211 int32_t next_handle_slot;
216 int32_t timer_capacity;
227 bio_logger_link_t loggers;
231 bio_worker_thread_t* thread_pool;
232 int32_t num_running_async_jobs;
239 BIO_PLATFORM_UPDATE_NO_WAIT,
240 BIO_PLATFORM_UPDATE_WAIT_INDEFINITELY,
241 BIO_PLATFORM_UPDATE_WAIT_NOTIFIABLE,
242} bio_platform_update_type_t;
247bio_realloc(
void* ptr,
size_t size) {
252bio_malloc(
size_t size) {
253 return bio_realloc(NULL, size);
261static inline uint32_t
262bio_next_pow2(uint32_t v) {
278 va_copy(args_copy, args);
279 int num_chars = vsnprintf(buf->ptr, (
size_t)buf->len, fmt, args_copy);
282 if (num_chars < 0) {
return num_chars; }
284 if (num_chars >= (
int)buf->len) {
286 buf->ptr = bio_malloc(num_chars + 1);
287 buf->len = num_chars + 1;
289 vsnprintf(buf->ptr, (
size_t)buf->len, fmt, args);
292 buf->ptr[num_chars] =
'\0';
296BIO_FORMAT_ATTRIBUTE(2, 3)
301 int num_chars = bio_vfmt(buf, fmt, args);
309#define bio_set_core_error(error, code) bio_set_core_error(error, code, __FILE__, __LINE__)
421bio_handle_table_init(
void);
424bio_handle_table_cleanup(
void);
432bio_timer_cleanup(
void);
435bio_timer_update(
void);
438bio_time_until_next_timer(
void);
443bio_scheduler_init(
void);
446bio_scheduler_cleanup(
void);
451bio_thread_init(
void);
454bio_thread_cleanup(
void);
457bio_thread_update(
void);
460bio_num_running_async_jobs(
void);
465bio_logging_init(
void);
468bio_logging_cleanup(
void);
void(* bio_entrypoint_t)(void *userdata)
Entrypoint for a coroutine.
Definition bio.h:120
bio_coro_state_t
State of a coroutine.
Definition bio.h:504
bio_core_error_code_t
Core error codes.
Definition bio.h:616
void bio_net_cleanup(void)
Cleanup the Network I/O subsystem.
void bio_platform_unblock_exit_signal(void)
Unblock exit signals from the OS.
void bio_net_init(void)
Initialize the Network I/O subsystem.
void bio_platform_end_create_thread_pool(void)
Called after the async thread pool is created.
void bio_platform_update(bio_time_t wait_timeout_ms, bool notifiable)
Check on I/O completion status.
void bio_platform_block_exit_signal(void)
Block exit signals from the OS.
void bio_handle_exit_signal(void)
Callback for a platform implementation to notify bio of exit signal.
bio_time_t bio_platform_current_time_ms(void)
Return the current time in milliseconds.
void bio_platform_cleanup(void)
Cleanup the platform layer.
void bio_platform_init(void)
Initialize the platform layer.
void bio_platform_begin_create_thread_pool(void)
Called before the async thread pool is created.
void bio_platform_notify(void)
Make bio_platform_update return.
void bio_fs_cleanup(void)
Cleanup the File I/O subsystem.
void bio_fs_init(void)
Initialize the File I/O subsystem.
bio_exit_reason_t
Result for bio_wait_for_exit.
Definition bio.h:520
int64_t bio_time_t
A timestamp.
Definition bio.h:213
void *(* realloc)(void *ptr, size_t size, void *ctx)
Allocation function.
Definition bio.h:308
void * ctx
Allocator context.
Definition bio.h:290
Definition internal.h:153
Coroutine-local storage (CLS) specification.
Definition bio.h:317
Definition internal.h:165
Definition internal.h:203
An error returned from a function.
Definition bio.h:468
Definition internal.h:160
Definition internal.h:198
Definition internal.h:127
Definition internal.h:185
Initialization options.
Definition bio.h:408
bio_allocator_t allocator
Custom allocator. Defaults to stdlib if not provided.
Definition bio.h:410
Definition internal.h:144
Handle to a signal.
Definition bio.h:147
A unique tag.
Definition bio.h:189
Definition internal.h:193