Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
10_drain.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 10 -- graceful drain.
20//
21// srn_sched_stop (example 06) is abrupt, queued fibers never run.
22// srn_sched_drain is the graceful counterpart. Workers keep running every
23// runnable fiber and let in-flight IO finish, but new IO submissions are
24// fenced, a fiber's next IO call fails with the CANCELED error tag so it
25// unwinds instead of parking on fresh work. The pool then reaches the same
26// quiescence as a natural finish and the run returns.
27//
28// A worker fiber loops doing short sleeps. A controller fiber lets it run a few
29// rounds, then drains the scheduler. The worker's in-flight sleep still
30// completes, but its next sleep fails with CANCELED, so it stops its loop and
31// returns. No slice is ever cut mid-step.
32
33#include <stdint.h>
34#include <stdio.h>
35
36#include <serene/rt/fiber/io.h>
37#include <serene/runtime.h>
38
40 (void)ctx;
41 (void)arg;
42 for (int i = 0;; i++) {
44 if (err != nullptr && err->tag == CANCELED) {
45 printf("worker: sleep cancelled after %d rounds, winding down\n", i);
46 break;
47 }
48 printf("worker: round %d\n", i);
49 }
50 return nullptr;
51}
52
54 (void)ctx;
55 srn_scheduler_t *sched = arg;
56 // Let the worker fiber run a few rounds before winding the pool down. This
57 // sleep is submitted before the drain, so it completes normally.
58 (void)srn_fiber_sleep_ms(70);
59 printf("controller: draining\n");
60 srn_sched_drain(sched);
61 return nullptr;
62}
63
64int main(void) {
65 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
66 srn_context_t *ctx = srn_context_make(engine);
67
68 (void)srn_fiber_spawn(ctx, worker_loop, nullptr);
69 (void)srn_fiber_spawn(ctx, controller, sched);
70
71 srn_sched_run(sched, 1);
72 printf("run returned; drain converged\n");
73
76 return 0;
77}
static srn_fiber_result_t controller(srn_context_t *ctx, void *arg)
static srn_fiber_result_t controller(srn_context_t *ctx, void *arg)
Definition 10_drain.c:53
int main(void)
Definition 10_drain.c:64
static srn_fiber_result_t worker_loop(srn_context_t *ctx, void *arg)
Definition 10_drain.c:39
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
@ CANCELED
Definition errors.h:97
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_drain(srn_scheduler_t *sched)
Ask a running scheduler to wind down gracefully.
Definition scheduler.c:963
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
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
srn_error_tag_t tag
Definition errors.h:126