Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
04_suspend_resume.c
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 program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU 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 program 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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19// Example 04 -- suspend and resume, the building block for blocking.
20//
21// `srn_fiber_yield` keeps a fiber runnable. `srn_fiber_suspend` parks it off
22// every queue until some party calls `srn_fiber_ready` on it. This is the
23// mechanism a lock, a channel, or an IO reactor uses to block a fiber on an
24// event and wake it when the event arrives.
25//
26// Suspending is race free by construction. The fiber switches off its stack
27// FIRST, and only then does the scheduler run the commit callback on its own
28// side. The commit registers the fiber with whatever will wake it and re-checks
29// the condition. A waker can never see a half parked fiber, because the commit
30// runs after the park is complete. The commit returns false to decline parking
31// when the condition already holds, so the fiber resumes at once.
32//
33// A one slot mailbox shows the handoff. The consumer runs first and finds the
34// slot empty, so it parks and registers itself as the waiter. The producer
35// fills the slot and readies the consumer, which resumes and reads the value.
36
37#include <stdio.h>
38
39#include <serene/runtime.h>
40
41typedef struct {
42 int value;
44 // The fiber waiting for a value, or null if none is waiting.
46} mailbox_t;
47
49
50// Park commit. It runs on the worker after the consumer has parked. If a value
51// is already there it declines to park, so the consumer resumes immediately.
52// Otherwise it records the consumer as the waiter and stays parked.
53static bool park_on_empty(srn_fiber_t *self, void *arg) {
54 mailbox_t *mb = arg;
55 if (mb->has_value) {
56 return false;
57 }
58 mb->waiter = self;
59 return true;
60}
61
62static srn_fiber_result_t consumer(srn_context_t *ctx, void *arg) {
63 (void)ctx;
64 (void)arg;
65 printf("consumer: waiting for a value\n");
67 printf("consumer: received %d\n", mailbox.value);
68 return nullptr;
69}
70
71static srn_fiber_result_t producer(srn_context_t *ctx, void *arg) {
72 (void)ctx;
73 (void)arg;
74 mailbox.value = 42;
75 mailbox.has_value = true;
76 printf("producer: put %d in the mailbox\n", mailbox.value);
77 // Wake the consumer if one is parked on the mailbox.
78 if (mailbox.waiter != nullptr) {
79 srn_fiber_t *w = mailbox.waiter;
80 mailbox.waiter = nullptr;
82 }
83 return nullptr;
84}
85
86int main(void) {
87 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
88 srn_context_t *ctx = srn_context_make(engine);
89
90 // The consumer is created first so it runs and parks before the producer
91 // fills the slot.
92 (void)srn_fiber_spawn(ctx, consumer, nullptr);
93 (void)srn_fiber_spawn(ctx, producer, nullptr);
94
95 srn_sched_run(sched, 1);
96
99 return 0;
100}
static srn_fiber_result_t producer(srn_context_t *ctx, void *arg)
int main(void)
static srn_fiber_result_t consumer(srn_context_t *ctx, void *arg)
static bool park_on_empty(srn_fiber_t *self, void *arg)
static mailbox_t mailbox
srn_context_t * srn_context_make(srn_engine_t *engine)
Make an empty context, by allocating a new memory block.
Definition context.c:39
int srn_context_release(srn_context_t *ctx)
Definition context.c:64
srn_fiber_t * srn_fiber_spawn(srn_context_t *ctx, srn_fiber_entry_t entry, void *arg)
Make and schedule a fiber with every default, the engine's scheduler, the configured stack size,...
Definition fiber.c:247
void * srn_fiber_result_t
What a fiber's entry produces, type-erased.
Definition fiber.h:157
#define SERENE_RUNTIME_SHUTDOWN(engine)
Tear down what SERENE_RUNTIME_INIT brought up, in reverse order, the engine (reactor,...
Definition runtime.h:58
#define SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, config)
SERENE_RUNTIME_INIT plus a sched variable bound to the engine's scheduler, which every run and fiber ...
Definition runtime.h:46
void srn_fiber_ready(srn_fiber_t *fiber)
Mark a suspended fiber runnable again, waking it when the event it awaited occurs.
Definition scheduler.c:1044
void srn_sched_run(srn_scheduler_t *sched, size_t nworkers)
Run the scheduler with nworkers os threads draining it, returning once the pool goes quiescent (every...
Definition scheduler.c:843
void srn_fiber_suspend(srn_fiber_park_fn commit, void *arg)
A suspended fiber is on no scheduler queue, and the scheduler does not track what it waits on – whoev...
Definition scheduler.c:1024
srn_fiber_t * waiter