Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
02_yield.c File Reference
#include <stdio.h>
#include <serene/runtime.h>
Include dependency graph for 02_yield.c:

Go to the source code of this file.

Functions

static srn_fiber_result_t counter (srn_context_t *ctx, void *arg)
int main (void)

Function Documentation

◆ counter()

srn_fiber_result_t counter ( srn_context_t * ctx,
void * arg )
static

Definition at line 35 of file 02_yield.c.

35 {
36 (void)ctx;
37 // The argument carries the fiber's label, passed as a small integer.
38 long id = (long)(intptr_t)arg;
39 for (int step = 1; step <= 3; step++) {
40 printf("fiber %ld: step %d\n", id, step);
41 // Hand the worker to the next ready fiber. This fiber resumes here on its
42 // next turn.
44 }
45 printf("fiber %ld: done\n", id);
46 return nullptr;
47}
void srn_fiber_yield(void)
Yield cooperatively, re-enqueue the running fiber and run the next ready one.
Definition scheduler.c:1000
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 49 of file 02_yield.c.

49 {
50 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
51 srn_context_t *ctx = srn_context_make(engine);
52
53 for (long id = 1; id <= 3; id++) {
54 (void)srn_fiber_spawn(ctx, counter, (void *)(intptr_t)id);
55 }
56
57 // One worker, so the turn taking is deterministic and the output interleaves
58 // the three fibers a step at a time.
59 srn_sched_run(sched, 1);
60
63 return 0;
64}
static srn_fiber_result_t counter(srn_context_t *ctx, void *arg)
Definition 02_yield.c:35
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
#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
Here is the call graph for this function: