Serene Runtime 1.0.0
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)
 

Variables

static int ok
 

Function Documentation

◆ counter()

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

Definition at line 37 of file 02_yield.c.

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

◆ main()

int main ( void )

Definition at line 51 of file 02_yield.c.

51 {
52 srn_mm_t *mm = srn_mm_init();
53 srn_engine_t *engine = srn_engine_make(mm);
54 srn_context_t *ctx = srn_context_make(engine);
55 srn_scheduler_t *sched = engine->scheduler;
56
57 for (long id = 1; id <= 3; id++) {
58 (void)srn_fiber_make(ctx, sched, counter, (void *)(intptr_t)id, 0);
59 }
60
61 // One worker, so the turn taking is deterministic and the output interleaves
62 // the three fibers a step at a time.
63 srn_sched_run(sched, 1);
64
66 srn_engine_shutdown(engine);
68 return 0;
69}
static srn_fiber_result_t counter(srn_context_t *ctx, void *arg)
Definition 02_yield.c:37
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
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
Here is the call graph for this function:

Variable Documentation

◆ ok

int ok
static

Definition at line 35 of file 02_yield.c.