6
6
[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-blue.svg )] ( https://opensource.org/licenses/MIT )
7
7
![ Stability: experimental] ( https://img.shields.io/badge/stability-experimental-orange.svg )
8
8
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
13
10
14
11
This package exports a set of macros to synthesize static procedure-based automata from
15
12
a declarative description of states, triggers and transitions
16
13
with all states, triggers and transitions known at compile-time.
17
14
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.
19
16
20
17
Within each states you also have the full power
21
18
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
27
24
28
25
A detailed usage tutorial is available at [ examples/water_phase_transitions.nim] ( ) . It is executable.
29
26
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
31
96
``` Nim
32
97
type Phase = enum
33
98
## States of your automaton.
0 commit comments