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