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

Go to the source code of this file.

Macros

#define FIBER_COUNT   64
#define ITERATIONS   100
#define WORKER_COUNT   4

Functions

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

Variables

static srn_fiber_mutex_t lock
static long counter

Macro Definition Documentation

◆ FIBER_COUNT

#define FIBER_COUNT   64

Definition at line 39 of file 14_mutex.c.

◆ ITERATIONS

#define ITERATIONS   100

Definition at line 40 of file 14_mutex.c.

◆ WORKER_COUNT

#define WORKER_COUNT   4

Definition at line 41 of file 14_mutex.c.

Function Documentation

◆ bump()

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

Definition at line 49 of file 14_mutex.c.

49 {
50 (void)ctx;
51 (void)arg;
52 for (int i = 0; i < ITERATIONS; i++) {
54 // The read and the write must not interleave with another fiber. Two fibers
55 // reading the same value would each write it back once and lose an update.
56 long seen = counter;
57 counter = seen + 1;
59 }
60 return nullptr;
61}
static atomic_uint counter
static srn_fiber_mutex_t lock
Definition 14_mutex.c:43
#define ITERATIONS
Definition 14_mutex.c:40
void srn_fiber_mutex_unlock(srn_fiber_mutex_t *m)
Release m.
Definition mutex.c:99
void srn_fiber_mutex_lock(srn_fiber_mutex_t *m)
Acquire m.
Definition mutex.c:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 63 of file 14_mutex.c.

63 {
64 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
65 srn_context_t *ctx = srn_context_make(engine);
66
68 counter = 0;
69
70 for (int i = 0; i < FIBER_COUNT; i++) {
71 (void)srn_fiber_spawn(ctx, bump, nullptr);
72 }
73
74 long expected = (long)FIBER_COUNT * ITERATIONS;
75 printf(
76 "running %d fibers x %d iterations across %d workers\n", FIBER_COUNT, ITERATIONS, WORKER_COUNT
77 );
79
80 printf(
81 "counted %ld (expected %ld): %s\n", counter, expected,
82 counter == expected ? "every increment landed" : "lost an update"
83 );
84
87 return 0;
88}
#define FIBER_COUNT
Definition 05_parallel.c:37
#define WORKER_COUNT
Definition 05_parallel.c:38
static srn_fiber_result_t bump(srn_context_t *ctx, void *arg)
Definition 14_mutex.c:49
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:250
void srn_fiber_mutex_init(srn_fiber_mutex_t *m)
Initialise m to the unlocked state.
Definition mutex.c:76
#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:875
Here is the call graph for this function:

Variable Documentation

◆ counter

long counter
static

Definition at line 47 of file 14_mutex.c.

◆ lock

srn_fiber_mutex_t lock
static

Definition at line 43 of file 14_mutex.c.