193 lines7.6 KB
Newer
Older
-
+
commited
{line.log.rev}
on
9 months ago
1
#ifndef COROUTINE_H
2
#define COROUTINE_H
3
10 months ago
2
4
#include <stdbool.h>
6 months ago
5
#include <stddef.h>
8 months ago
6
#include <stdint.h>
Last month
7
#include "cor_platform_inc.h"
10 hours ago
8
#include "coroutine_names_def.h"
10 months ago
2
9
9 months ago
10
///////////////////////////////////////////////////////////////////////////////
11
// Coroutine
12
//
13
// Coroutines for C, based on setjmp/longjmp.
14
// Thread safe - each thread has its own coroutine system
15
// Coroutines are cooperatively scheduled
16
// Coroutines have their own stack (currently 16K each)
17
// A coroutine can be continued, queried, or deleted on a different thread.
18
//
19
// Usage:
20
// Coroutine_StartSystem(); // call once per thread before using coroutines
21
// Coroutine *co = Coroutine_New(start_function);
6 months ago
22
// void *result;
4 months ago
23
// if (Coroutine_Run(co, initial_value, &result)) {
6 months ago
24
// // Handle the failure
4 months ago
25
// }
9 months ago
26
// Coroutine_Delete(co);
27
// Coroutine_StopSystem(); // call once per thread when done with coroutines
28
//
29
// Inside the coroutine function:
30
// void *value = Coroutine_Yield(yield_value, on_yield, this);
31
// ...
32
// return return_value;
33
//
34
// To create a coroutine:
35
// Coroutine *co = Coroutine_New(start_function);
36
// To start or continue a coroutine:
37
// void *result = Coroutine_Continue(co, value, early);
38
// // early=true puts the coroutine at the head of the run queue
39
// // early=false puts the coroutine at the tail of the run queue
40
// To yield from inside a coroutine:
41
// void *value = Coroutine_Yield(yield_value, on_yield, this);
42
// // on_yield is called before the next coroutine is run
43
// // 'this' is passed to on_yield as its parameter
44
// // value is the value passed to Coroutine_Continue
45
// To delete a coroutine:
46
// Coroutine_Delete(co);
47
// To get the value yielded from, or returned by a corotuine:
48
// void *value = Coroutine_GetValue(co);
49
// To get the currently running coroutine (NULL if none):
50
// Coroutine *co = Coroutine_GetActive();
51
// To check if a coroutine is currently running:
52
// bool running = Coroutine_IsRunning(co);
53
//
54
// Notes:
55
// Coroutine is not expected to be used directly, but as a foundation for
56
// higher level constructs such as Generators, Async, etc.
57
//
58
///////////////////////////////////////////////////////////////////////////////
59
60
61
// The stack is used as follows:
4 months ago
62
// +------------------+ <- stack top
63
// | coroutine header | <- more claimed as needed in Coroutine_New
64
// +------------------+ <-
65
// | coroutine stack | <-
66
// +------------------+ <-
67
// | coroutine header |
9 months ago
68
// +------------------+
69
// | coroutine stack |
70
// +------------------+
4 months ago
71
// | coroutine header |
9 months ago
72
// +------------------+
73
// | coroutine stack |
74
// +------------------+
75
// | coroutine header |
76
// +------------------+
77
// | coroutine stack |
78
// +------------------+
79
// | coroutine header |
80
// +------------------+
81
// | startup space | <- set aside by Coroutine_StartSystem
82
// +------------------+
83
// | caller | <- This calls Coroutine_StartSystem etc
84
// +------------------+
85
// | used stack |
4 months ago
86
// +------------------+ <- stack bottom
9 months ago
87
4 months ago
88
// Each coroutine has this much stack:
89
// For Python, we set it to 17 * (enough for a PyEval_EvalDefault), so we get at least 7
90
// calls deep before we need a new chunk, ie maximum multi-chunk wastage is under 6% address space.
91
//
92
// There's a trade-off between smaller chunk sizes, which allow more async tasks to co-exist
93
// on a thread, and larger chunk sizes which waste less memory in part-used chunks.
94
//
95
// ... which means 10000 async tasks need a 2.6 GB stack, which fits comfortably in the address map.
96
//
97
// Note, when developing the use of Coroutine in Python, the author found the following used
98
// excessive amounts of stack space:
99
// Tk_Init: on an Intel 64 bit Mac it used 72k.
100
// _decimal multplies of big decimal numbers: 256k+640 (2 x 128k buffers in squaretrans_pow2() + workings)
101
//
102
// On 64 bit macos, PYOS_STACK_MARGIN_BYTES is 2k * sizeof(void *), ie 16k, or 17 of those, 272k, should give enough slack to operate well.
103
Last month
104
Last month
105
#ifndef Coroutine_API_FUNC
106
#define Coroutine_API_FUNC(T) extern T
107
#endif
108
6 months ago
109
// No coroutine will ask for less stack than this
110
#ifndef COROUTINE_MINIMUM_STACK_SIZE
111
#define COROUTINE_MINIMUM_STACK_SIZE (4096 * sizeof(void *))
9 months ago
112
#endif
9 months ago
113
4 months ago
114
// When Coroutine is started, an amount of stack is set aside to give
115
// the caller of Coroutine_StartSystem a bit of room to work before calling
116
// Coroutine_Run(), that is this amount:
9 months ago
117
#ifndef COROUTINE_STARTUP_STACK_SIZE
Last month
118
#ifndef _NDEBUG
119
#define COROUTINE_STARTUP_STACK_SIZE (1024 * sizeof(void *))
120
#else
121
#define COROUTINE_STARTUP_STACK_SIZE (128 * sizeof(void *))
122
#endif
9 months ago
123
#endif
9 months ago
124
4 months ago
125
// This is *expensive* to turn on, especially if you have lots of stack pieces (eg when there's lots of Tasks)
126
#ifndef COROUTINE_CHECK_INTEGRITY_ON_STACK_CHECK
127
#define COROUTINE_CHECK_INTEGRITY_ON_STACK_CHECK 0
7 months ago
128
#endif
129
4 months ago
130
#ifndef COROUTINE_RECORD_LOWEST_HEADROOM
131
#define COROUTINE_RECORD_LOWEST_HEADROOM 1
132
#endif
133
9 months ago
134
// Returned by Coroutine_StopSystem(), this summarises the coroutine session
135
typedef struct Coroutine_Report {
136
unsigned coroutines_created;
137
unsigned coroutines_pool_size;
6 months ago
138
size_t lowest_headroom;
139
size_t largest_stack;
9 months ago
140
} Coroutine_Report;
141
4 months ago
142
typedef enum Coroutine_Err {
143
Coroutine_OK = 0,
144
Coroutine_Err_SystemNotRunning,
145
Coroutine_Err_SystemRunning,
146
Coroutine_Err_NoStack,
147
Coroutine_Err_CoroutineFromWrongThread,
148
Coroutine_Err_ACoroutineIsAlreadyRunning,
149
Coroutine_Err_ExitWithRunningCoroutines,
150
Coroutine_Err_StackOverrun,
151
Coroutine_Err_InternalInsistency,
152
Coroutine_Err_CouldNotInitialiseSystem,
153
Coroutine_Err_WrongState,
154
Coroutine_Err_Canceled
155
} Coroutine_Err;
156
10 months ago
2
157
typedef struct Coroutine Coroutine;
158
Last month
159
typedef void (*Coroutine_YieldCallback)(void *me);
1 weeks ago
160
typedef Coroutine_Err (*Coroutine_SystemStart)(void *, Coroutine *);
10 months ago
2
161
typedef void *(*Coroutine_Start)(void *);
162
10 hours ago
163
Coroutine_API_FUNC(void) Coroutine_SetStackLimit(void *);
164
Coroutine_API_FUNC(Coroutine_Report) Coroutine_GetReport(void);
4 months ago
165
#ifndef NDEBUG
10 hours ago
166
Coroutine_API_FUNC(Coroutine_Err) Coroutine_CheckIntegrity(void);
4 months ago
167
#else
10 hours ago
168
static inline Coroutine_Err Coroutine_CheckIntegrity(void){return Coroutine_OK;}
4 months ago
169
#endif
10 hours ago
170
Coroutine_API_FUNC(Coroutine *) Coroutine_New(size_t min_size, size_t min_headroom, Coroutine_Start start);
171
Coroutine_API_FUNC(Coroutine_Err) Coroutine_Run_Coroutine(Coroutine *cor, void *value);
172
Coroutine_API_FUNC(Coroutine_Err) Coroutine_RunSystem(size_t min_size, size_t min_headroom, Coroutine_SystemStart start, void *value);
173
Coroutine_API_FUNC(Coroutine_Err) Coroutine_Run(size_t min_size, size_t min_headroom, Coroutine_Start start, void *value, void **result);
174
Coroutine_API_FUNC(void) Coroutine_Delete(Coroutine *cor);
175
Coroutine_API_FUNC(Coroutine_Err) Coroutine_Continue(Coroutine *cor, void *value, bool early);
176
Coroutine_API_FUNC(void *) Coroutine_Yield(void *value, Coroutine_YieldCallback on_yield, void *me);
177
Coroutine_API_FUNC(void *) Coroutine_GetValue(Coroutine *cor);
178
Coroutine_API_FUNC(Coroutine *) Coroutine_GetActive(void);
179
Coroutine_API_FUNC(ptrdiff_t) Coroutine_GetStackHeadroom(void);
180
Coroutine_API_FUNC(void *) Coroutine_GetStackHWM(void);
181
Coroutine_API_FUNC(void) Coroutine_ClearStackForHWM(void);
182
Coroutine_API_FUNC(bool) Coroutine_CanStartCoroutine(size_t size);
183
Coroutine_API_FUNC(void *) Coroutine_GetCStackTop(void);
184
Coroutine_API_FUNC(Coroutine_Err) Coroutine_Chain(size_t min_size, size_t min_headroom, Coroutine_Start start, void *value, void **result);
185
Coroutine_API_FUNC(bool) Coroutine_IsStarted(void);
186
Coroutine_API_FUNC(bool) Coroutine_IsRunning(Coroutine *cor);
187
Coroutine_API_FUNC(bool) Coroutine_IsComplete(Coroutine *cor);
9 months ago
188
10 hours ago
189
Coroutine_API_FUNC(void) Coroutine_Dump_(void);
6 months ago
190
10 hours ago
191
#include "coroutine_names_undef.h"
9 months ago
192
#endif
193