Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
03_wait_for.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 03 -- joining on a fiber's result.
20//
21// `srn_fiber_wait_for` blocks the calling fiber until a target fiber finishes,
22// then returns the target's result. This is the fiber equivalent of joining a
23// thread. If the target has already finished the call returns its result at
24// once without blocking.
25//
26// A worker fiber computes a value and returns it. A waiter fiber joins on the
27// worker and reads what it produced. The waiter is created first, so it runs
28// and parks before the worker finishes, which is the blocking path.
29
30#include <stdio.h>
31
32#include <serene/runtime.h>
33
34// The worker reports a real result, so this example reads an actual payload out
35// of the returned value rather than a bare sentinel.
36static int answer;
37
38// Filled in main before the run, so the waiter knows which fiber to join.
40
41static srn_fiber_result_t worker(srn_context_t *ctx, void *arg) {
42 (void)ctx;
43 (void)arg;
44 printf("worker: computing\n");
45 return &answer;
46}
47
48static srn_fiber_result_t waiter(srn_context_t *ctx, void *arg) {
49 (void)ctx;
50 (void)arg;
51 printf("waiter: blocking on the worker\n");
53 printf("waiter: worker produced %s\n",
54 result == &answer ? "the expected value" : "an unexpected value");
55 return &answer;
56}
57
58int main(void) {
59 srn_mm_t *mm = srn_mm_init();
60 srn_engine_t *engine = srn_engine_make(mm);
61 srn_context_t *ctx = srn_context_make(engine);
62 srn_scheduler_t *sched = engine->scheduler;
63
64 // Order of creation sets who runs first on one worker. The waiter runs first
65 // and parks, then the worker finishes and wakes it.
66 (void)srn_fiber_make(ctx, sched, waiter, nullptr, 0);
67 worker_fiber = srn_fiber_make(ctx, sched, worker, nullptr, 0);
68
69 srn_sched_run(sched, 1);
70
72 srn_engine_shutdown(engine);
74 return 0;
75}
static int answer
Definition 03_wait_for.c:36
int main(void)
Definition 03_wait_for.c:58
static srn_fiber_result_t worker(srn_context_t *ctx, void *arg)
Definition 03_wait_for.c:41
static srn_fiber_t * worker_fiber
Definition 03_wait_for.c:39
static srn_fiber_result_t waiter(srn_context_t *ctx, void *arg)
Definition 03_wait_for.c:48
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
void * srn_fiber_result_t
Definition fiber.h:117
srn_fiber_result_t srn_fiber_wait_for(srn_fiber_t *target)
Block the calling fiber until target finishes, then return its result.
Definition scheduler.c:963
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
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