Serene Runtime 1.0.0-dev
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. Both fibers are created with
28// `srn_fiber_make`, which leaves them NEW, so the whole graph is wired
29// before `srn_fiber_schedule` starts anything and creation order carries no
30// meaning. The waiter is scheduled first so it runs and parks before the
31// worker finishes, which is the blocking path.
32
33#include <stdio.h>
34
35#include <serene/runtime.h>
36
37// The worker reports a real result, so this example reads an actual payload
38// out of the returned value.
39static int answer;
40
41// Filled in main before the run, so the waiter knows which fiber to join.
43
44static srn_fiber_result_t worker(srn_context_t *ctx, void *arg) {
45 (void)ctx;
46 (void)arg;
47 printf("worker: computing\n");
48 return &answer;
49}
50
51static srn_fiber_result_t waiter(srn_context_t *ctx, void *arg) {
52 (void)ctx;
53 (void)arg;
54 printf("waiter: blocking on the worker\n");
56 printf(
57 "waiter: worker produced %s\n", result == &answer ? "the expected value" : "an unexpected value"
58 );
59 return nullptr;
60}
61
62int main(void) {
63 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
64 srn_context_t *ctx = srn_context_make(engine);
65
66 // Nothing runs until scheduled, so the join target can be wired after both
67 // fibers exist. Scheduling order (not creation order) sets who runs first
68 // on one worker, the waiter parks, then the worker finishes and wakes it.
69 srn_fiber_t *waiting = srn_fiber_make(ctx, sched, "waiter", waiter, nullptr, 0);
70 worker_fiber = srn_fiber_make(ctx, sched, "worker", worker, nullptr, 0);
71 srn_fiber_schedule(waiting);
73
74 srn_sched_run(sched, 1);
75
78 return 0;
79}
static int answer
Definition 03_wait_for.c:39
int main(void)
Definition 03_wait_for.c:62
static srn_fiber_result_t worker(srn_context_t *ctx, void *arg)
Definition 03_wait_for.c:44
static srn_fiber_t * worker_fiber
Definition 03_wait_for.c:42
static srn_fiber_result_t waiter(srn_context_t *ctx, void *arg)
Definition 03_wait_for.c:51
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_make(srn_context_t *ctx, srn_scheduler_t *sched, const char *name, srn_fiber_entry_t entry, void *arg, size_t stack_size)
Create a fiber that will run entry(ctx, arg), registered with sched but NOT scheduled.
Definition fiber.c:201
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
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:1091
void srn_fiber_schedule(srn_fiber_t *fiber)
Schedule a NEW fiber, making it eligible to run.
Definition scheduler.c:630
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