cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
reactor.c File Reference

Readiness multiplexer (io_uring / epoll / kqueue) driving CWIST's synchronous callback model. More...

#include <cwist/sys/io/reactor.h>
#include "reactor_rx.h"
#include "async_debug.h"
#include <cwist/core/mem/alloc.h>
#include <cwist/sys/app/shutdown.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdatomic.h>
#include <fcntl.h>
#include <limits.h>
#include <time.h>
Include dependency graph for reactor.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 
#define REACTOR_CHUNK_EVENTS   512
 
#define REACTOR_IDLE_WAIT_NS   100000000ull
 

Typedefs

typedef struct reactor_slot_chunk reactor_slot_chunk_t
 

Functions

bool cwist_reactor_post (cwist_reactor_t *r, cwist_reactor_post_t *node)
 
void cwist_reactor_timer_init (cwist_reactor_timer_t *timer, void(*cb)(void *ctx), void *ctx)
 
bool cwist_reactor_timer_arm (cwist_reactor_t *r, cwist_reactor_timer_t *timer, uint64_t delay_us)
 
void cwist_reactor_timer_cancel (cwist_reactor_t *r, cwist_reactor_timer_t *timer)
 
bool cwist_reactor_timer_armed (const cwist_reactor_timer_t *timer)
 
cwist_reactor_t * cwist_reactor_create (void)
 
void cwist_reactor_destroy (cwist_reactor_t *reactor)
 
bool cwist_reactor_add (cwist_reactor_t *reactor, int fd, cwist_reactor_cb_t cb, const void *payload, size_t payload_size)
 
bool cwist_reactor_add_out (cwist_reactor_t *reactor, int fd, cwist_reactor_cb_t cb, const void *payload, size_t payload_size)
 
bool cwist_reactor_mod (cwist_reactor_t *reactor, int fd, cwist_reactor_cb_t cb, const void *payload, size_t payload_size)
 
bool cwist_reactor_del (cwist_reactor_t *reactor, int fd)
 
void cwist_reactor_run (cwist_reactor_t *reactor)
 
void cwist_reactor_stop (cwist_reactor_t *reactor)
 

Detailed Description

Readiness multiplexer (io_uring / epoll / kqueue) driving CWIST's synchronous callback model.

Design note - why readiness notification + synchronous completion is kept:

  1. No queues: a request passes through no queue at all; the woken worker finishes it immediately. This is the source of the 0.0x ms latency. A completion model pushes each request through a ring 3-4 times and binds progress to loop ticks, landing at 2-3ms (Axum/Tokio level).
  2. Structural backpressure: callbacks block, so unfinished work cannot pile up in kernel or userland. A single in-flight cap per thread (32 in http.c) is all the flow control the server needs.
  3. Cache locality: the whole request lifetime runs on one thread's contiguous stack, reusing L1/L2. A completion model splits the handler into fragments and lifts per-stage state onto the heap.
  4. No state machines: handlers are straight-line code; a stack trace is the request's execution history.
  5. Deterministic tail: with no queue waiting, p99/p999 converge on the mean. The cost: per-connection concurrency is bounded by worker count (cores*8), covered by multi-process scaling (fork + SO_REUSEPORT).

Definition in file reactor.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 25 of file reactor.c.

◆ REACTOR_CHUNK_EVENTS

#define REACTOR_CHUNK_EVENTS   512

Definition at line 241 of file reactor.c.

◆ REACTOR_IDLE_WAIT_NS

#define REACTOR_IDLE_WAIT_NS   100000000ull

Definition at line 878 of file reactor.c.

Typedef Documentation

◆ reactor_slot_chunk_t

typedef struct reactor_slot_chunk reactor_slot_chunk_t

Function Documentation

◆ cwist_reactor_add()

bool cwist_reactor_add ( cwist_reactor_t *  reactor,
int  fd,
cwist_reactor_cb_t  cb,
const void *  payload,
size_t  payload_size 
)

Definition at line 1357 of file reactor.c.

References cap.

Referenced by cwist_async_server_loop(), cwist_reactor_create(), and cwist_websocket_async_attach_ex().

◆ cwist_reactor_add_out()

bool cwist_reactor_add_out ( cwist_reactor_t *  reactor,
int  fd,
cwist_reactor_cb_t  cb,
const void *  payload,
size_t  payload_size 
)

◆ cwist_reactor_create()

cwist_reactor_t * cwist_reactor_create ( void  )

◆ cwist_reactor_del()

bool cwist_reactor_del ( cwist_reactor_t *  reactor,
int  fd 
)

Definition at line 1410 of file reactor.c.

References cap.

◆ cwist_reactor_destroy()

void cwist_reactor_destroy ( cwist_reactor_t *  reactor)

◆ cwist_reactor_mod()

bool cwist_reactor_mod ( cwist_reactor_t *  reactor,
int  fd,
cwist_reactor_cb_t  cb,
const void *  payload,
size_t  payload_size 
)

Definition at line 1367 of file reactor.c.

References cap.

◆ cwist_reactor_post()

◆ cwist_reactor_run()

void cwist_reactor_run ( cwist_reactor_t *  reactor)

Definition at line 1579 of file reactor.c.

References cap, cwist_free(), and head.

Referenced by cwist_async_server_loop().

◆ cwist_reactor_stop()

void cwist_reactor_stop ( cwist_reactor_t *  reactor)

Definition at line 1904 of file reactor.c.

References cap.

Referenced by cwist_http_pool_destroy().

◆ cwist_reactor_timer_arm()

bool cwist_reactor_timer_arm ( cwist_reactor_t *  r,
cwist_reactor_timer_t *  timer,
uint64_t  delay_us 
)

◆ cwist_reactor_timer_armed()

bool cwist_reactor_timer_armed ( const cwist_reactor_timer_t *  timer)

Definition at line 961 of file reactor.c.

References cwist_reactor_timer::heap_slot.

◆ cwist_reactor_timer_cancel()

void cwist_reactor_timer_cancel ( cwist_reactor_t *  r,
cwist_reactor_timer_t *  timer 
)

Definition at line 956 of file reactor.c.

References cwist_reactor_timer::heap_slot.

◆ cwist_reactor_timer_init()

void cwist_reactor_timer_init ( cwist_reactor_timer_t *  timer,
void(*)(void *ctx)  cb,
void *  ctx 
)