summaryrefslogtreecommitdiffstats
path: root/doc/developer-guide/syncop.md
blob: 4e30451f30e9e73da83dd713b961e25e1dfc7137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#syncop framework
A coroutines-based, cooperative multi-tasking framework.

## Topics

- Glossary
- Lifecycle of a synctask
- Existing usage


## Glossary

### syncenv

syncenv is an object that provides access to a pool of worker threads.
synctasks execute in a syncenv.

### synctask

synctask can be informally defined as a pair of function pointers, namely _the
call_ and _the callback_ (see syncop.h for more details).

        synctask_fn_t - 'the call'
        synctask_cbk_t - 'the callback'

synctask has two modes of operation,

1. The calling thread waits for the synctask to complete.
2. The calling thread schedules the synctask and continues.

synctask guarantees that the callback is called _after_ the call completes.

### Lifecycle of a synctask

A synctask could go into the following stages while in execution.

- CREATED  - On calling synctask_create/synctask_new.

- RUNNABLE - synctask is queued in env->runq.

- RUNNING  - When one of syncenv's worker threads calls synctask_switch_to.

- WAITING  - When a synctask calls synctask_yield.

- DONE     - When a synctask has run to completion.


                                +-------------------------------+
                                |            CREATED            |
                                +-------------------------------+
                                  |
                                  | synctask_new/synctask_create
                                  v
                                +-------------------------------+
                                |    RUNNABLE (in env->runq)    | <+
                                +-------------------------------+  |
                                  |                                |
                                  | synctask_switch_to             |
                                  v                                |
 +------+  on task completion   +-------------------------------+  |
 | DONE | <-------------------- |            RUNNING            |  | synctask_wake/wake
 +------+                       +-------------------------------+  |
                                  |                                |
                                  | synctask_yield/yield           |
                                  v                                |
                                +-------------------------------+  |
                                |    WAITING (in env->waitq)    | -+
                                +-------------------------------+

Note: A synctask is not guaranteed to run on the same thread throughout its
lifetime. Every time a synctask yields, it is possible for it to run on a
different thread.