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

Go to the source code of this file.

Macros

#define TASK_COUNT   16

Functions

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

Variables

static atomic_int tasks_ran

Macro Definition Documentation

◆ TASK_COUNT

#define TASK_COUNT   16

Definition at line 44 of file 06_request_term.c.

Function Documentation

◆ controller()

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

Definition at line 55 of file 06_request_term.c.

55 {
56 (void)ctx;
57 // The scheduler arrives through the argument, so the controller can act on
58 // it.
59 srn_scheduler_t *sched = arg;
60 printf("controller: stopping the scheduler before the tasks run\n");
61 srn_sched_stop(sched);
62 // Returning normally is the clean boundary the worker stops on.
63 return nullptr;
64}
void srn_sched_stop(srn_scheduler_t *sched)
Ask a running scheduler to stop.
Definition scheduler.c:939
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 66 of file 06_request_term.c.

66 {
67 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
68 srn_context_t *ctx = srn_context_make(engine);
69
70 atomic_store(&tasks_ran, 0);
71
72 // The controller is created first so it reaches the front of the ready queue
73 // first and requests the stop before any task is dequeued.
74 (void)srn_fiber_spawn(ctx, controller, sched);
75 for (int i = 0; i < TASK_COUNT; i++) {
76 (void)srn_fiber_spawn(ctx, task, nullptr);
77 }
78
79 // One worker keeps the order deterministic: the controller runs, stops the
80 // scheduler, and the worker exits at the top of its next loop with the tasks
81 // still queued.
82 srn_sched_run(sched, 1);
83
84 int ran = atomic_load(&tasks_ran);
85 printf("%d of %d tasks ran before termination\n", ran, TASK_COUNT);
86 printf("the rest were left queued and are reclaimed at shutdown\n");
87
88 // Teardown order matters once a run stops with work still queued. The unrun
89 // tasks are still on the scheduler's registry, yet their fiber structs live
90 // in this context's blocks, so the context must not be released while they
91 // are unreaped. SERENE_RUNTIME_SHUTDOWN reaps the scheduler first and the
92 // memory manager then reclaims every context chain, this one included.
94 return 0;
95}
static srn_fiber_result_t controller(srn_context_t *ctx, void *arg)
static srn_fiber_result_t task(srn_context_t *ctx, void *arg)
#define TASK_COUNT
static atomic_int tasks_ran
srn_context_t * srn_context_make(srn_engine_t *engine)
Make an empty context, by allocating a new memory block.
Definition context.c:39
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:

◆ task()

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

Definition at line 48 of file 06_request_term.c.

48 {
49 (void)ctx;
50 (void)arg;
51 atomic_fetch_add(&tasks_ran, 1);
52 return nullptr;
53}
Here is the caller graph for this function:

Variable Documentation

◆ tasks_ran

atomic_int tasks_ran
static

Definition at line 46 of file 06_request_term.c.