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

Go to the source code of this file.

Data Structures

struct  mailbox_t

Functions

static bool park_on_empty (srn_fiber_t *self, void *arg)
static srn_fiber_result_t consumer (srn_context_t *ctx, void *arg)
static srn_fiber_result_t producer (srn_context_t *ctx, void *arg)
int main (void)

Variables

static mailbox_t mailbox

Function Documentation

◆ consumer()

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

Definition at line 62 of file 04_suspend_resume.c.

62 {
63 (void)ctx;
64 (void)arg;
65 printf("consumer: waiting for a value\n");
67 printf("consumer: received %d\n", mailbox.value);
68 return nullptr;
69}
static bool park_on_empty(srn_fiber_t *self, void *arg)
static mailbox_t mailbox
void srn_fiber_suspend(srn_fiber_park_fn commit, void *arg)
A suspended fiber is on no scheduler queue, and the scheduler does not track what it waits on – whoev...
Definition scheduler.c:1024
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 86 of file 04_suspend_resume.c.

86 {
87 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
88 srn_context_t *ctx = srn_context_make(engine);
89
90 // The consumer is created first so it runs and parks before the producer
91 // fills the slot.
92 (void)srn_fiber_spawn(ctx, consumer, nullptr);
93 (void)srn_fiber_spawn(ctx, producer, nullptr);
94
95 srn_sched_run(sched, 1);
96
99 return 0;
100}
static srn_fiber_result_t producer(srn_context_t *ctx, void *arg)
static srn_fiber_result_t consumer(srn_context_t *ctx, void *arg)
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:

◆ park_on_empty()

bool park_on_empty ( srn_fiber_t * self,
void * arg )
static

Definition at line 53 of file 04_suspend_resume.c.

53 {
54 mailbox_t *mb = arg;
55 if (mb->has_value) {
56 return false;
57 }
58 mb->waiter = self;
59 return true;
60}
srn_fiber_t * waiter
Here is the caller graph for this function:

◆ producer()

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

Definition at line 71 of file 04_suspend_resume.c.

71 {
72 (void)ctx;
73 (void)arg;
74 mailbox.value = 42;
75 mailbox.has_value = true;
76 printf("producer: put %d in the mailbox\n", mailbox.value);
77 // Wake the consumer if one is parked on the mailbox.
78 if (mailbox.waiter != nullptr) {
79 srn_fiber_t *w = mailbox.waiter;
80 mailbox.waiter = nullptr;
82 }
83 return nullptr;
84}
void srn_fiber_ready(srn_fiber_t *fiber)
Mark a suspended fiber runnable again, waking it when the event it awaited occurs.
Definition scheduler.c:1044
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ mailbox

mailbox_t mailbox
static

Definition at line 48 of file 04_suspend_resume.c.