Serene Runtime 1.0.0
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 seam
23// a lock, a channel, or an IO reactor uses to block a fiber on an event and
24// 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/rt/context.h"
40#include "serene/rt/engine.h"
41#include "serene/rt/fiber.h"
43
44static int ok;
45
46typedef struct {
47 int value;
49 // The fiber waiting for a value, or null if none is waiting.
51} mailbox_t;
52
54
55// Park commit. It runs on the worker after the consumer has parked. If a value
56// is already there it declines to park, so the consumer resumes immediately.
57// Otherwise it records the consumer as the waiter and stays parked.
58static bool park_on_empty(srn_fiber_t *self, void *arg) {
59 mailbox_t *mb = arg;
60 if (mb->has_value) {
61 return false;
62 }
63 mb->waiter = self;
64 return true;
65}
66
67static srn_fiber_result_t consumer(srn_context_t *ctx, void *arg) {
68 (void)ctx;
69 (void)arg;
70 printf("consumer: waiting for a value\n");
72 printf("consumer: received %d\n", mailbox.value);
73 return &ok;
74}
75
76static srn_fiber_result_t producer(srn_context_t *ctx, void *arg) {
77 (void)ctx;
78 (void)arg;
79 mailbox.value = 42;
80 mailbox.has_value = true;
81 printf("producer: put %d in the mailbox\n", mailbox.value);
82 // Wake the consumer if one is parked on the mailbox.
83 if (mailbox.waiter != nullptr) {
84 srn_fiber_t *w = mailbox.waiter;
85 mailbox.waiter = nullptr;
87 }
88 return &ok;
89}
90
91int main(void) {
92 srn_mm_t *mm = srn_mm_init();
93 srn_engine_t *engine = srn_engine_make(mm);
94 srn_context_t *ctx = srn_context_make(engine);
95 srn_scheduler_t *sched = engine->scheduler;
96
97 // The consumer is created first so it runs and parks before the producer
98 // fills the slot.
99 (void)srn_fiber_make(ctx, sched, consumer, nullptr, 0);
100 (void)srn_fiber_make(ctx, sched, producer, nullptr, 0);
101
102 srn_sched_run(sched, 1);
103
105 srn_engine_shutdown(engine);
106 srn_mm_shutdown(mm);
107 return 0;
108}
static int ok
Definition 01_hello.c:36
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:38
int srn_context_release(srn_context_t *ctx)
Definition context.c:63
srn_mm_t * srn_mm_init()
Initialize the memory manager, this function will panic on error.
Definition default.c:294
void srn_mm_shutdown(srn_mm_t *mm)
Shut down the memory manager and release the resources.
Definition default.c:325
srn_engine_t * srn_engine_make(srn_mm_t *mm)
Definition engine.c:92
void srn_engine_shutdown(srn_engine_t *engine)
Definition engine.c:123
srn_fiber_t * srn_fiber_make(srn_context_t *ctx, srn_scheduler_t *sched, srn_fiber_entry_t entry, void *arg, size_t stack_size)
Create a fiber that will run entry(ctx, arg).
Definition fiber.c:169
AI Generated (🤦) Fiber subsystem overview.
void * srn_fiber_result_t
Definition fiber.h:117
void srn_fiber_ready(srn_fiber_t *fiber)
Mark a suspended fiber runnable again.
Definition scheduler.c:918
void srn_sched_run(srn_scheduler_t *sched, int nworkers)
Run the scheduler with nworkers os threads draining it, returning once the pool goes quiescent (every...
Definition scheduler.c:784
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:898
srn_fiber_t * waiter
Engine is a structure to own the long living and main pieces of the compiler.
Definition engine.h:49
srn_scheduler_t * scheduler
The fiber scheduler, set by srn_sched_init.
Definition engine.h:67
Main memory manager structure that will own all the allocated blocks and data.
Definition interface.h:112