Skip to content

Commit 77a7c80

Browse files
committed
Update README with examples from Weave
1 parent 0505724 commit 77a7c80

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

Diff for: README.md

+71-6
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
77
![Stability: experimental](https://img.shields.io/badge/stability-experimental-orange.svg)
88

9-
This is a support library for the [Weave](https://github.com/mratsim/weave) multithreading runtime.
10-
Requirements for a multithreading runtime makes Synthesis also an excellent fit
11-
to generate state machines for embedded devices, protocols
12-
and managing complex event-driven workloads in general.
9+
## Overview
1310

1411
This package exports a set of macros to synthesize static procedure-based automata from
1512
a declarative description of states, triggers and transitions
1613
with all states, triggers and transitions known at compile-time.
1714

18-
It is fast, composable, generates compact code and does not allocate on the heap.
15+
It is fast, composable, threadsafe, generates compact code and does not allocate on the heap.
1916

2017
Within each states you also have the full power
2118
of the Nim language instead of being restricted to only operations
@@ -27,7 +24,75 @@ introduce a stack of past states to create a pushdown automata (for parsing Brai
2724

2825
A detailed usage tutorial is available at [examples/water_phase_transitions.nim](). It is executable.
2926

30-
Snippets:
27+
This is a support library for the [Weave](https://github.com/mratsim/weave) multithreading runtime.
28+
Requirements for a multithreading runtime makes Synthesis also an excellent fit
29+
to generate state machines for embedded devices, protocols
30+
and managing complex event-driven workloads in general.
31+
32+
## Appetizers
33+
34+
Here are 2 simple examples of the usage of Synthesis in production code to implement components of the Weave multithreading runtime.
35+
36+
### Worker state machine
37+
38+
[Source](https://github.com/mratsim/weave/blob/4493b493/weave/work_fsm.nim)
39+
40+
This is the description of the transitions of a worker thread that ran out of tasks in its own task queue
41+
and checks if it managed to steal tasks from other threads. (The theft is handled in another state machine.)
42+
43+
```Nim
44+
type
45+
RecvTaskState = enum
46+
RT_CheckChannel
47+
RT_FoundTask
48+
49+
RT_Event = enum
50+
RTE_CheckedAllChannels
51+
RTE_FoundTask
52+
RTE_isWaiting
53+
```
54+
55+
![worker thread FSA](media/work_fsm.png)
56+
57+
### sync (await) a task that may be spawned on another thread
58+
59+
[Source](https://github.com/mratsim/weave/blob/4493b493/weave/await_fsm.nim)
60+
61+
This is the description of the transitions of any thread that syncs (awaits) a future that may be handled in another thread.
62+
63+
In summary, while the awaited task has child tasks still pending in this worker thread, those are processed in priority,
64+
otherwise, it steals tasks from other threads to help them on their workload.
65+
As soon as the future is ready, it exits.
66+
67+
```Nim
68+
type AwaitState = enum
69+
AW_CheckTask
70+
AW_OutOfChildTasks
71+
AW_Steal
72+
AW_SuccessfulTheft
73+
74+
type AwaitEvent = enum
75+
AWE_FutureReady
76+
AWE_HasChildTask
77+
AWE_ReceivedTask
78+
```
79+
80+
![sync/await FSA](media/await_fsm.png)
81+
82+
## Table of Contents
83+
84+
- [Synthesis](#synthesis)
85+
- [Overview](#overview)
86+
- [Appetizers](#appetizers)
87+
- [Worker state machine](#worker-state-machine)
88+
- [sync (await) a task that may be spawned on another thread](#sync-await-a-task-that-may-be-spawned-on-another-thread)
89+
- [Table of Contents](#table-of-contents)
90+
- [Commented example: Water phases](#commented-example-water-phases)
91+
- [Displaying the state machine](#displaying-the-state-machine)
92+
- [Technical constraints](#technical-constraints)
93+
- [References](#references)
94+
95+
## Commented example: Water phases
3196
```Nim
3297
type Phase = enum
3398
## States of your automaton.

Diff for: media/await_fsm.png

165 KB
Loading

Diff for: media/work_fsm.png

99.6 KB
Loading

0 commit comments

Comments
 (0)