Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
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// Hello world for an external program that links libserene.runtime built with
20// -Dwith-serene=disabled. It uses only the substrate: the memory manager, the
21// engine/context, and the fiber subsystem. No value model, no LLVM.
22
23#include <stdio.h>
24
25#include "serene/rt/context.h"
26#include "serene/rt/engine.h"
27#include "serene/rt/fiber.h"
29
30// A fiber result must be non-null. This sentinel stands in for "no value".
31static int ok;
32
34 (void)ctx;
35 (void)arg;
36 printf("hello from a Serene fiber (substrate runtime)\n");
37 return &ok;
38}
39
40int main(void) {
41 srn_mm_t *mm = srn_mm_init();
42 srn_engine_t *engine = srn_engine_make(mm);
43 srn_context_t *ctx = srn_context_make(engine);
44 srn_scheduler_t *sched = engine->scheduler;
45
46 (void)srn_fiber_make(ctx, sched, say_hello, NULL, 0);
47 srn_sched_run(sched, 1);
48
49 // Tear the engine (hence the scheduler) down before releasing the context the
50 // fibers were allocated from.
51 srn_engine_shutdown(engine);
54 return 0;
55}
static int ok
Definition 01_hello.c:36
static srn_fiber_result_t say_hello(srn_context_t *ctx, void *arg)
Definition 01_hello.c:38
static srn_fiber_result_t say_hello(srn_context_t *ctx, void *arg)
Definition hello.c:33
int main(void)
Definition hello.c:40
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
AI Generated (🤦) Fiber subsystem overview.
void * srn_fiber_result_t
Definition fiber.h:117
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