Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
04_suspend_resume.c File Reference
#include <stdio.h>
#include "serene/rt/context.h"
#include "serene/rt/engine.h"
#include "serene/rt/fiber.h"
#include "serene/rt/mm/interface.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 int ok
 
static mailbox_t mailbox
 

Function Documentation

◆ consumer()

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

Definition at line 67 of file 04_suspend_resume.c.

67 {
68 (void)ctx;
69 (void)arg;
70 printf("consumer: waiting for a value\n");
72 printf("consumer: received %d\n", mailbox.value);
73 return &ok;
74}
static int ok
Definition 01_hello.c:36
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:898
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 91 of file 04_suspend_resume.c.

91 {
92 srn_mm_t *mm = srn_mm_init();
93 srn_engine_t *engine = srn_engine_make(mm);
94 srn_context_t *ctx = srn_context_make(engine);
95 srn_scheduler_t *sched = engine->scheduler;
96
97 // The consumer is created first so it runs and parks before the producer
98 // fills the slot.
99 (void)srn_fiber_make(ctx, sched, consumer, nullptr, 0);
100 (void)srn_fiber_make(ctx, sched, producer, nullptr, 0);
101
102 srn_sched_run(sched, 1);
103
105 srn_engine_shutdown(engine);
106 srn_mm_shutdown(mm);
107 return 0;
108}
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: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:

◆ park_on_empty()

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

Definition at line 58 of file 04_suspend_resume.c.

58 {
59 mailbox_t *mb = arg;
60 if (mb->has_value) {
61 return false;
62 }
63 mb->waiter = self;
64 return true;
65}
srn_fiber_t * waiter
Here is the caller graph for this function:

◆ producer()

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

Definition at line 76 of file 04_suspend_resume.c.

76 {
77 (void)ctx;
78 (void)arg;
79 mailbox.value = 42;
80 mailbox.has_value = true;
81 printf("producer: put %d in the mailbox\n", mailbox.value);
82 // Wake the consumer if one is parked on the mailbox.
83 if (mailbox.waiter != nullptr) {
84 srn_fiber_t *w = mailbox.waiter;
85 mailbox.waiter = nullptr;
87 }
88 return &ok;
89}
void srn_fiber_ready(srn_fiber_t *fiber)
Mark a suspended fiber runnable again.
Definition scheduler.c:918
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 53 of file 04_suspend_resume.c.

◆ ok

int ok
static

Definition at line 44 of file 04_suspend_resume.c.