|
Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
|
#include "serene/rt/reactor.h"#include <assert.h>#include <limits.h>#include "backend.h"#include "internal.h"#include "serene/rt/engine.h"#include "serene/rt/impl/spsc_ring.h"#include "serene/rt/mm/interface.h"#include "serene/utils.h"Go to the source code of this file.
Data Structures | |
| struct | srn_timer_t |
Typedefs | |
| typedef struct srn_timer_t | srn_timer_t |
Functions | |
| srn_reactor_t * | srn_reactor_init (srn_engine_t *engine) |
| Allocate the IO reactor from the engine. | |
| void | srn_reactor_shutdown (srn_reactor_t *reactor) |
| Tear the reactor down, stop and join the reactor thread and release its channels. | |
| srn_error_t * | srn_reactor_errors_static (srn_error_tag_t tag) |
| Return the shared, immutable error for an IO failure tag. | |
| void | srn_reactor_post_completion (srn_reactor_t *reactor, size_t channel, srn_reactor_io_completion_t completion) |
| Post a finished operation to channel's completion queue and rouse the channel's worker through the notify mechanism. | |
| void | srn_reactor_op_done (srn_reactor_t *reactor, size_t channel) |
| Drop channel's and the reactor's in-flight counts by one. | |
| bool | srn_reactor_idle (srn_reactor_t *reactor) |
| Whether the reactor has no operations in flight. | |
| bool | srn_reactor_submit (srn_reactor_t *reactor, size_t channel, const srn_reactor_io_request_t *request) |
| Place a request on channel's submission queue and rouse the reactor. | |
| size_t | srn_reactor_reap (srn_reactor_t *reactor, size_t channel, srn_reactor_io_completion_t *out, size_t max) |
| Drain up to max completions from channel's completion queue into out, returning how many were taken. | |
| bool | srn_reactor_channel_has_completions (srn_reactor_t *reactor, size_t channel) |
| Whether channel's completion queue has unconsumed completions. | |
| static bool | timer_less (srn_context_t *ctx, const void *a, const void *b) |
| Order timers by deadline so the soonest sits at the heap root. | |
| static void | timer_add (srn_reactor_t *r, size_t channel, const srn_reactor_io_request_t *req) |
| Register a SLEEP: allocate a timer entry and push it onto the heap. | |
| static int | timer_next_timeout (srn_reactor_t *r) |
| Milliseconds until the soonest deadline (rounded up), clamped to a valid epoll_wait timeout, 0 if a timer is already due, INT_MAX for a far-off deadline, and -1 (block indefinitely) when no timers are pending. | |
| static void | timer_fire_expired (srn_reactor_t *r) |
| Fire every timer whose deadline has passed, pop it (in deadline order), post a zero result completion to its channel, and free the entry. | |
| static void | process_request (srn_reactor_t *r, size_t channel, const srn_reactor_io_request_t *req) |
| static void | drain_submissions (srn_reactor_t *r) |
| static void | reactor_thread_main (void *arg) |
| void | srn_reactor_activate (srn_reactor_t *reactor, size_t nchannels, srn_reactor_notify_fn notify) |
| Bring the reactor up, allocate nchannels channels (one per worker) and start the reactor thread. | |
Variables | |
| static srn_error_t | io_errors [] |
| static const pqmh_control_t | timer_ctl = {.less = timer_less} |
| typedef struct srn_timer_t srn_timer_t |
|
static |
Definition at line 367 of file reactor.c.
|
static |
Definition at line 336 of file reactor.c.
|
static |
Definition at line 378 of file reactor.c.
| void srn_reactor_activate | ( | srn_reactor_t * | reactor, |
| size_t | nchannels, | ||
| srn_reactor_notify_fn | notify ) |
Bring the reactor up, allocate nchannels channels (one per worker) and start the reactor thread.
notify is called with the scheduler and the channel id whenever a channel gains completions.
Definition at line 390 of file reactor.c.
|
nodiscard |
Whether channel's completion queue has unconsumed completions.
The worker checks this under the scheduler lock before parking, so a completion landing just before it sleeps is not missed.
Definition at line 237 of file reactor.c.
| srn_error_t * srn_reactor_errors_static | ( | srn_error_tag_t | tag | ) |
Return the shared, immutable error for an IO failure tag.
These live in static storage, not a context, so a completion or result can point one at maybe_error with no allocation and no lifetime concern, and it is safe to share across threads. A backend maps its native code (via srn_errno_to_tag on POSIX) to a tag, then to one of these. Any tag without a dedicated entry yields the UNKNOWN_IO_ERROR error.
Definition at line 125 of file reactor.c.
|
nodiscard |
Whether the reactor has no operations in flight.
The scheduler folds this into quiescence, a pool with parked workers and an empty run queue is only truly done when the reactor is idle too.
Definition at line 169 of file reactor.c.
|
nodiscard |
Allocate the IO reactor from the engine.
Definition at line 34 of file reactor.c.
| void srn_reactor_op_done | ( | srn_reactor_t * | reactor, |
| size_t | channel ) |
Drop channel's and the reactor's in-flight counts by one.
The consumer of a completion calls this after it has finished acting on it (for fibers, after the waiting fiber is readied), so that "in flight" reaches zero only once every result has been both produced and handled. Dropping the channel count is also what re-opens the channel's admission window (see srn_reactor_submit).
Definition at line 162 of file reactor.c.
| void srn_reactor_post_completion | ( | srn_reactor_t * | reactor, |
| size_t | channel, | ||
| srn_reactor_io_completion_t | completion ) |
Post a finished operation to channel's completion queue and rouse the channel's worker through the notify mechanism.
Runs on the reactor thread, called by the core for ops it completes itself (such as NOP) and by a backend when a descriptor operation finishes. It does NOT drop the in-flight count: that is the consumer's job via srn_reactor_op_done, so an op counts as gone only once the waiting fiber has been readied, never in the gap between.
Definition at line 141 of file reactor.c.
| size_t srn_reactor_reap | ( | srn_reactor_t * | reactor, |
| size_t | channel, | ||
| srn_reactor_io_completion_t * | out, | ||
| size_t | max ) |
Drain up to max completions from channel's completion queue into out, returning how many were taken.
Definition at line 220 of file reactor.c.
| void srn_reactor_shutdown | ( | srn_reactor_t * | reactor | ) |
Tear the reactor down, stop and join the reactor thread and release its channels.
Definition at line 66 of file reactor.c.
|
nodiscard |
Place a request on channel's submission queue and rouse the reactor.
Returns false without submitting when the channel already has a full ring's worth of operations in flight. The condition is transient, it clears as the channel's worker consumes completions, so the caller can fail the operation back with CHANNEL_BUSY and let the fiber retry.
Definition at line 174 of file reactor.c.
|
static |
Register a SLEEP: allocate a timer entry and push it onto the heap.
Definition at line 274 of file reactor.c.
|
static |
Fire every timer whose deadline has passed, pop it (in deadline order), post a zero result completion to its channel, and free the entry.
Definition at line 316 of file reactor.c.
|
static |
|
static |
Milliseconds until the soonest deadline (rounded up), clamped to a valid epoll_wait timeout, 0 if a timer is already due, INT_MAX for a far-off deadline, and -1 (block indefinitely) when no timers are pending.
Definition at line 291 of file reactor.c.
|
static |
Definition at line 99 of file reactor.c.
|
static |
Definition at line 269 of file reactor.c.