Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
internal.h
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <stdatomic.h>
22
23#include "serene/rt/context.h"
25#include "serene/rt/impl/pqmh.h"
27#include "serene/rt/reactor.h"
28
29/// A channel, the per-worker submission and completion ring pair.
31 // requests: worker -> reactor
33 // completions: reactor -> worker
35 /// Operations submitted on this channel and not yet consumed. Touched only
36 /// by the channel's owning worker os thread (the submit path bumps it, the
37 /// consume path drops it), so it needs no atomics. Admission control caps
38 /// it at the ring capacity, which is what makes a full SQ or CQ
39 /// impossible.
40 size_t inflight;
42
45
46 /// The chosen kernel mechanism and its private state, set at activation.
49
50 /// Wakes the worker that owns a channel a completion landed on. Set at
51 /// activation.
53
54 /// One channel per worker, there is a 1to1 mapping between worker ids and
55 /// channel ids. Basically their index in their respective array should match
57 size_t nchannels;
58
59 /// Capacity of every ring (1 << ring_magnitude), fixed at activation. Also
60 /// each channel's in-flight cap; see srn_reactor_submit.
62
63 /// The reactor thread and its run flag. The thread loops until `running` is
64 /// cleared and it is roused.
66 _Atomic bool running;
67
68 /// Operations submitted but not yet consumed, across all channels.
69 /// Quiescence needs this to reach zero. Bumped on submit, dropped by the
70 /// consumer through srn_reactor_op_done once the waiting fiber is readied.
71 _Atomic size_t inflight;
72
73 /// Pending timers (like SLEEP, and IO deadlines) as a min-heap keyed by
74 /// absolute monotonic deadline. Created at activation, owned and touched only
75 /// by the reactor thread (THIS IS NOT THREAD SAFE, so only the reactor should
76 /// manage it). See the timer helpers in reactor.c.
78};
79
80/**
81 * Post a finished operation to `channel`'s completion queue and rouse the
82 * channel's worker through the notify mechanism. Runs on the reactor thread,
83 * called by the core for ops it completes itself (such as NOP) and by a backend
84 * when a descriptor operation finishes. It does NOT drop the in-flight count:
85 * that is the consumer's job via `srn_reactor_op_done`, so an op counts as gone
86 * only once the waiting fiber has been readied, never in the gap between.
87 */
88[[gnu::nonnull(1)]]
90 srn_reactor_t *reactor, size_t channel, srn_reactor_io_completion_t completion
91);
92
93/**
94 * Return the shared, immutable error for an IO failure `tag`. These live in
95 * static storage, not a context, so a completion or result can point one at
96 * `maybe_error` with no allocation and no lifetime concern, and it is safe to
97 * share across threads. A backend maps its native code (via srn_errno_to_tag
98 * on POSIX) to a tag, then to one of these. Any tag without a dedicated entry
99 * yields the UNKNOWN_IO_ERROR error.
100 */
srn_error_tag_t
The kind of a failure.
Definition errors.h:75
srn_error_t * srn_reactor_errors_static(srn_error_tag_t tag)
Return the shared, immutable error for an IO failure tag.
Definition reactor.c:125
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 no...
Definition reactor.c:141
A generic priority queue backed by a binary min-heap.
Reactor overview.
void(* srn_reactor_notify_fn)(srn_scheduler_t *sched, size_t channel)
The interface that the scheduler supplies so the reactor can announce that a channel has completions,...
Definition reactor.h:177
A mutable single producer, single consumer ring of fixed size elements.
A priority queue.
Definition pqmh.h:66
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
One async system behind the reactor contract.
Definition backend.h:55
A channel, the per-worker submission and completion ring pair.
Definition internal.h:30
srn_spsc_ring_t * cq
Definition internal.h:34
size_t inflight
Operations submitted on this channel and not yet consumed.
Definition internal.h:40
srn_spsc_ring_t * sq
Definition internal.h:32
A completion, the result of one submission, echoed back on the channel.
Definition reactor.h:156
srn_thread_t thread
The reactor thread and its run flag.
Definition internal.h:65
pqmh_t timers
Pending timers (like SLEEP, and IO deadlines) as a min-heap keyed by absolute monotonic deadline.
Definition internal.h:77
srn_reactor_io_channel_t * channels
One channel per worker, there is a 1to1 mapping between worker ids and channel ids.
Definition internal.h:56
srn_reactor_notify_fn notify
Wakes the worker that owns a channel a completion landed on.
Definition internal.h:52
srn_context_t * ctx
Definition internal.h:44
size_t ring_capacity
Capacity of every ring (1 << ring_magnitude), fixed at activation.
Definition internal.h:61
const srn_reactor_backend_t * backend
The chosen kernel mechanism and its private state, set at activation.
Definition internal.h:47
size_t nchannels
Definition internal.h:57
_Atomic bool running
Definition internal.h:66
void * backend_state
Definition internal.h:48
_Atomic size_t inflight
Operations submitted but not yet consumed, across all channels.
Definition internal.h:71
srn_thread_t, srn_mutex_t, and srn_cond_t model the thread-level operations the runtime needs,...