Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
07_sleep.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 07 -- sleeping without blocking the worker.
20//
21// srn_fiber_sleep_ms parks the calling fiber for a duration and lets its worker run
22// other fibers meanwhile. The reactor holds a timer per sleeper and wakes it
23// once its deadline passes. This is also why running out of runnable fibers no
24// longer ends a run: the reactor keeps it alive while any timer (or IO) is
25// outstanding, and the run ends only once nothing is runnable AND nothing is in
26// flight.
27//
28// Three fibers sleep for different spans. On a single worker they park almost at
29// once, then wake in deadline order (100, 200, 300 ms) rather than creation
30// order, because the reactor fires timers by their deadline.
31
32#include <stdint.h>
33#include <stdio.h>
34
35#include <serene/rt/fiber/io.h>
36#include <serene/runtime.h>
37
38typedef struct {
39 const char *name;
40 uint64_t ms;
42
43static srn_fiber_result_t sleeper(srn_context_t *ctx, void *arg) {
44 (void)ctx;
45 sleeper_args_t *a = arg;
46 printf("%s: sleeping %llu ms\n", a->name, (unsigned long long)a->ms);
48 printf("%s: woke up (%s)\n", a->name, err == nullptr ? "ok" : err->msg);
49 return nullptr;
50}
51
52int main(void) {
53 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
54 srn_context_t *ctx = srn_context_make(engine);
55
56 sleeper_args_t a = {"A", 300};
57 sleeper_args_t b = {"B", 100};
58 sleeper_args_t c = {"C", 200};
59 (void)srn_fiber_spawn(ctx, sleeper, &a);
60 (void)srn_fiber_spawn(ctx, sleeper, &b);
61 (void)srn_fiber_spawn(ctx, sleeper, &c);
62
63 // One worker: all three park on their timers, and the run stays alive until
64 // the last timer fires -- the reactor keeps it from going quiescent early.
65 srn_sched_run(sched, 1);
66 printf("all sleepers done\n");
67
70 return 0;
71}
int main(void)
Definition 07_sleep.c:52
static srn_fiber_result_t sleeper(srn_context_t *ctx, void *arg)
Definition 07_sleep.c:43
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
srn_error_t * srn_fiber_sleep_ms(uint64_t duration_ms)
Suspend the calling fiber for at least duration_ms miliseconds, letting its worker run other fibers m...
Definition io.c:278
The fiber facing IO API: blocking looking calls a fiber uses to do IO.
#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_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
const char * name
Definition 07_sleep.c:39
uint64_t ms
Definition 07_sleep.c:40
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
char * msg
Definition errors.h:127