Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
10_drain.c File Reference
#include <stdint.h>
#include <stdio.h>
#include <serene/rt/fiber/io.h>
#include <serene/runtime.h>
Include dependency graph for 10_drain.c:

Go to the source code of this file.

Functions

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

Function Documentation

◆ controller()

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

Definition at line 53 of file 10_drain.c.

53 {
54 (void)ctx;
55 srn_scheduler_t *sched = arg;
56 // Let the worker fiber run a few rounds before winding the pool down. This
57 // sleep is submitted before the drain, so it completes normally.
58 (void)srn_fiber_sleep_ms(70);
59 printf("controller: draining\n");
60 srn_sched_drain(sched);
61 return nullptr;
62}
srn_error_t * srn_fiber_sleep_ms(uint64_t duration_ms)
Suspend the calling fiber for at least duration_ms miliseconds, letting its worker run other fibers m...
Definition io.c:278
void srn_sched_drain(srn_scheduler_t *sched)
Ask a running scheduler to wind down gracefully.
Definition scheduler.c:963
Here is the call graph for this function:

◆ main()

int main ( void )

Definition at line 64 of file 10_drain.c.

64 {
65 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
66 srn_context_t *ctx = srn_context_make(engine);
67
68 (void)srn_fiber_spawn(ctx, worker_loop, nullptr);
69 (void)srn_fiber_spawn(ctx, controller, sched);
70
71 srn_sched_run(sched, 1);
72 printf("run returned; drain converged\n");
73
76 return 0;
77}
static srn_fiber_result_t controller(srn_context_t *ctx, void *arg)
static srn_fiber_result_t worker_loop(srn_context_t *ctx, void *arg)
Definition 10_drain.c:39
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:

◆ worker_loop()

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

Definition at line 39 of file 10_drain.c.

39 {
40 (void)ctx;
41 (void)arg;
42 for (int i = 0;; i++) {
44 if (err != nullptr && err->tag == CANCELED) {
45 printf("worker: sleep cancelled after %d rounds, winding down\n", i);
46 break;
47 }
48 printf("worker: round %d\n", i);
49 }
50 return nullptr;
51}
@ CANCELED
Definition errors.h:97
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
srn_error_tag_t tag
Definition errors.h:126
Here is the call graph for this function:
Here is the caller graph for this function: