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

Go to the source code of this file.

Macros

#define FIBER_COUNT   1000
#define WORKER_COUNT   4

Functions

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

Variables

static atomic_int total

Macro Definition Documentation

◆ FIBER_COUNT

#define FIBER_COUNT   1000

Definition at line 37 of file 05_parallel.c.

◆ WORKER_COUNT

#define WORKER_COUNT   4

Definition at line 38 of file 05_parallel.c.

Function Documentation

◆ increment()

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

Definition at line 42 of file 05_parallel.c.

42 {
43 (void)ctx;
44 (void)arg;
45 // The one piece of shared state. An atomic add is safe from any worker.
46 atomic_fetch_add(&total, 1);
47 return nullptr;
48}
static atomic_int total
Definition 05_parallel.c:40
Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 50 of file 05_parallel.c.

50 {
51 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
52 srn_context_t *ctx = srn_context_make(engine);
53
54 atomic_store(&total, 0);
55 for (int i = 0; i < FIBER_COUNT; i++) {
56 (void)srn_fiber_spawn(ctx, increment, nullptr);
57 }
58
59 printf("running %d fibers across %d workers\n", FIBER_COUNT, WORKER_COUNT);
61
62 int counted = atomic_load(&total);
63 printf(
64 "counted %d (expected %d): %s\n", counted, FIBER_COUNT,
65 counted == FIBER_COUNT ? "all fibers ran" : "lost an update"
66 );
67
70 return 0;
71}
#define FIBER_COUNT
Definition 05_parallel.c:37
static srn_fiber_result_t increment(srn_context_t *ctx, void *arg)
Definition 05_parallel.c:42
#define WORKER_COUNT
Definition 05_parallel.c:38
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:

Variable Documentation

◆ total

atomic_int total
static

Definition at line 40 of file 05_parallel.c.