Serene Runtime 1.0.0
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 int ok
 
static atomic_int tasks_ran
 

Macro Definition Documentation

◆ TASK_COUNT

#define TASK_COUNT   16

Definition at line 43 of file 06_request_term.c.

Function Documentation

◆ controller()

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

Definition at line 54 of file 06_request_term.c.

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

◆ main()

int main ( void )

Definition at line 65 of file 06_request_term.c.

65 {
66 srn_mm_t *mm = srn_mm_init();
67 srn_engine_t *engine = srn_engine_make(mm);
68 srn_context_t *ctx = srn_context_make(engine);
69 srn_scheduler_t *sched = engine->scheduler;
70
71 atomic_store(&tasks_ran, 0);
72
73 // The controller is created first so it reaches the front of the ready queue
74 // first and requests the stop before any task is dequeued.
75 (void)srn_fiber_make(ctx, sched, controller, sched, 0);
76 for (int i = 0; i < TASK_COUNT; i++) {
77 (void)srn_fiber_make(ctx, sched, task, nullptr, 0);
78 }
79
80 // One worker keeps the order deterministic: the controller runs, stops the
81 // scheduler, and the worker exits at the top of its next loop with the tasks
82 // still queued.
83 srn_sched_run(sched, 1);
84
85 int ran = atomic_load(&tasks_ran);
86 printf("%d of %d tasks ran before termination\n", ran, TASK_COUNT);
87 printf("the rest were left queued and are reclaimed at shutdown\n");
88
89 // Teardown order matters once a run stops with work still queued. The unrun
90 // tasks are still on the scheduler's registry, yet their fiber structs live
91 // in this context's blocks. srn_engine_shutdown reaps the scheduler, walking
92 // that registry, so it has to run before the context that owns those structs
93 // is released. The examples that drain every fiber leave an empty registry,
94 // so the order does not matter for them.
95 srn_engine_shutdown(engine);
98 return 0;
99}
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: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:

◆ task()

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

Definition at line 47 of file 06_request_term.c.

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

Variable Documentation

◆ ok

int ok
static

Definition at line 41 of file 06_request_term.c.

◆ tasks_ran

atomic_int tasks_ran
static

Definition at line 45 of file 06_request_term.c.