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

Go to the source code of this file.

Functions

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

Variables

static int answer
static srn_fiber_tworker_fiber

Function Documentation

◆ main()

int main ( void )

Definition at line 62 of file 03_wait_for.c.

62 {
63 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
64 srn_context_t *ctx = srn_context_make(engine);
65
66 // Nothing runs until scheduled, so the join target can be wired after both
67 // fibers exist. Scheduling order (not creation order) sets who runs first
68 // on one worker, the waiter parks, then the worker finishes and wakes it.
69 srn_fiber_t *waiting = srn_fiber_make(ctx, sched, "waiter", waiter, nullptr, 0);
70 worker_fiber = srn_fiber_make(ctx, sched, "worker", worker, nullptr, 0);
71 srn_fiber_schedule(waiting);
73
74 srn_sched_run(sched, 1);
75
78 return 0;
79}
static srn_fiber_result_t worker(srn_context_t *ctx, void *arg)
Definition 03_wait_for.c:44
static srn_fiber_t * worker_fiber
Definition 03_wait_for.c:42
static srn_fiber_result_t waiter(srn_context_t *ctx, void *arg)
Definition 03_wait_for.c:51
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_make(srn_context_t *ctx, srn_scheduler_t *sched, const char *name, srn_fiber_entry_t entry, void *arg, size_t stack_size)
Create a fiber that will run entry(ctx, arg), registered with sched but NOT scheduled.
Definition fiber.c:201
#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_fiber_schedule(srn_fiber_t *fiber)
Schedule a NEW fiber, making it eligible to run.
Definition scheduler.c:630
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:

◆ waiter()

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

Definition at line 51 of file 03_wait_for.c.

51 {
52 (void)ctx;
53 (void)arg;
54 printf("waiter: blocking on the worker\n");
56 printf(
57 "waiter: worker produced %s\n", result == &answer ? "the expected value" : "an unexpected value"
58 );
59 return nullptr;
60}
static int answer
Definition 03_wait_for.c:39
void * srn_fiber_result_t
What a fiber's entry produces, type-erased.
Definition fiber.h:157
srn_fiber_result_t srn_fiber_wait_for(srn_fiber_t *target)
Block the calling fiber until target finishes, then return its result.
Definition scheduler.c:1091
Here is the call graph for this function:
Here is the caller graph for this function:

◆ worker()

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

Definition at line 44 of file 03_wait_for.c.

44 {
45 (void)ctx;
46 (void)arg;
47 printf("worker: computing\n");
48 return &answer;
49}
Here is the caller graph for this function:

Variable Documentation

◆ answer

int answer
static

Definition at line 39 of file 03_wait_for.c.

◆ worker_fiber

srn_fiber_t* worker_fiber
static

Definition at line 42 of file 03_wait_for.c.