Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
01_hello.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 01 -- hello fiber.
20//
21// The smallest complete fiber program. It shows the bootstrap every other
22// example repeats. One macro brings up the memory manager and the engine and
23// binds the scheduler the engine already created, then the program makes a
24// context, spawns one fiber, and runs the scheduler until the work drains.
25//
26// A fiber's entry has the shape `srn_fiber_result_t (srn_context_t *, void *)`.
27// The return value is type erased and lands on the fiber's `result` field. A
28// null result is legal, the natural value for a fiber run for its effects.
29
30#include <stdio.h>
31
32#include <serene/runtime.h>
33
35 (void)ctx;
36 (void)arg;
37 printf("hello from a fiber\n");
38 return nullptr;
39}
40
41int main(void) {
42 // The memory manager and the engine come up in one step; the engine
43 // creates one scheduler at startup and `sched` is bound to it.
44 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
45 srn_context_t *ctx = srn_context_make(engine);
46
47 // Create the fiber. It is registered and made ready, but nothing runs until
48 // the scheduler does.
49 srn_fiber_t *fiber = srn_fiber_spawn(ctx, say_hello, nullptr);
50
51 // Drive the scheduler on this one thread. It returns once the fiber has run
52 // and the ready queue is empty.
53 srn_sched_run(sched, 1);
54
55 printf("fiber finished in state %d\n", (int)fiber->state);
56
59 return 0;
60}
static srn_fiber_result_t say_hello(srn_context_t *ctx, void *arg)
Definition 01_hello.c:34
int main(void)
Definition 01_hello.c:41
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
#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
_Atomic srn_fiber_state_t state
The lifecycle state.
Definition fiber.h:273