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

Go to the source code of this file.

Data Structures

struct  sleeper_args_t

Functions

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

Function Documentation

◆ main()

int main ( void )

Definition at line 52 of file 07_sleep.c.

52 {
53 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
54 srn_context_t *ctx = srn_context_make(engine);
55
56 sleeper_args_t a = {"A", 300};
57 sleeper_args_t b = {"B", 100};
58 sleeper_args_t c = {"C", 200};
59 (void)srn_fiber_spawn(ctx, sleeper, &a);
60 (void)srn_fiber_spawn(ctx, sleeper, &b);
61 (void)srn_fiber_spawn(ctx, sleeper, &c);
62
63 // One worker: all three park on their timers, and the run stays alive until
64 // the last timer fires -- the reactor keeps it from going quiescent early.
65 srn_sched_run(sched, 1);
66 printf("all sleepers done\n");
67
70 return 0;
71}
static srn_fiber_result_t sleeper(srn_context_t *ctx, void *arg)
Definition 07_sleep.c:43
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:

◆ sleeper()

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

Definition at line 43 of file 07_sleep.c.

43 {
44 (void)ctx;
45 sleeper_args_t *a = arg;
46 printf("%s: sleeping %llu ms\n", a->name, (unsigned long long)a->ms);
48 printf("%s: woke up (%s)\n", a->name, err == nullptr ? "ok" : err->msg);
49 return nullptr;
50}
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
const char * name
Definition 07_sleep.c:39
uint64_t ms
Definition 07_sleep.c:40
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
char * msg
Definition errors.h:127
Here is the call graph for this function:
Here is the caller graph for this function: