193 lines7.6 KB
1#ifndef COROUTINE_H
2#define COROUTINE_H
3
4#include <stdbool.h>
5#include <stddef.h>
6#include <stdint.h>
7#include "cor_platform_inc.h"
8#include "coroutine_names_def.h"
9
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);
22// void *result;
23// if (Coroutine_Run(co, initial_value, &result)) {
24// // Handle the failure
25// }
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:
62// +------------------+ <- stack top
63// | coroutine header | <- more claimed as needed in Coroutine_New
64// +------------------+ <-
65// | coroutine stack | <-
66// +------------------+ <-
67// | coroutine header |
68// +------------------+
69// | coroutine stack |
70// +------------------+
71// | coroutine header |
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 |
86// +------------------+ <- stack bottom
87
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
104
105#ifndef Coroutine_API_FUNC
106 #define Coroutine_API_FUNC(T) extern T
107#endif
108
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 *))
112#endif
113
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:
117#ifndef COROUTINE_STARTUP_STACK_SIZE
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
123#endif
124
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
128#endif
129
130#ifndef COROUTINE_RECORD_LOWEST_HEADROOM
131 #define COROUTINE_RECORD_LOWEST_HEADROOM 1
132#endif
133
134// Returned by Coroutine_StopSystem(), this summarises the coroutine session
135typedef struct Coroutine_Report {
136 unsigned coroutines_created;
137 unsigned coroutines_pool_size;
138 size_t lowest_headroom;
139 size_t largest_stack;
140} Coroutine_Report;
141
142typedef 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
157typedef struct Coroutine Coroutine;
158
159typedef void (*Coroutine_YieldCallback)(void *me);
160typedef Coroutine_Err (*Coroutine_SystemStart)(void *, Coroutine *);
161typedef void *(*Coroutine_Start)(void *);
162
163Coroutine_API_FUNC(void) Coroutine_SetStackLimit(void *);
164Coroutine_API_FUNC(Coroutine_Report) Coroutine_GetReport(void);
165#ifndef NDEBUG
166 Coroutine_API_FUNC(Coroutine_Err) Coroutine_CheckIntegrity(void);
167#else
168 static inline Coroutine_Err Coroutine_CheckIntegrity(void){return Coroutine_OK;}
169#endif
170Coroutine_API_FUNC(Coroutine *) Coroutine_New(size_t min_size, size_t min_headroom, Coroutine_Start start);
171Coroutine_API_FUNC(Coroutine_Err) Coroutine_Run_Coroutine(Coroutine *cor, void *value);
172Coroutine_API_FUNC(Coroutine_Err) Coroutine_RunSystem(size_t min_size, size_t min_headroom, Coroutine_SystemStart start, void *value);
173Coroutine_API_FUNC(Coroutine_Err) Coroutine_Run(size_t min_size, size_t min_headroom, Coroutine_Start start, void *value, void **result);
174Coroutine_API_FUNC(void) Coroutine_Delete(Coroutine *cor);
175Coroutine_API_FUNC(Coroutine_Err) Coroutine_Continue(Coroutine *cor, void *value, bool early);
176Coroutine_API_FUNC(void *) Coroutine_Yield(void *value, Coroutine_YieldCallback on_yield, void *me);
177Coroutine_API_FUNC(void *) Coroutine_GetValue(Coroutine *cor);
178Coroutine_API_FUNC(Coroutine *) Coroutine_GetActive(void);
179Coroutine_API_FUNC(ptrdiff_t) Coroutine_GetStackHeadroom(void);
180Coroutine_API_FUNC(void *) Coroutine_GetStackHWM(void);
181Coroutine_API_FUNC(void) Coroutine_ClearStackForHWM(void);
182Coroutine_API_FUNC(bool) Coroutine_CanStartCoroutine(size_t size);
183Coroutine_API_FUNC(void *) Coroutine_GetCStackTop(void);
184Coroutine_API_FUNC(Coroutine_Err) Coroutine_Chain(size_t min_size, size_t min_headroom, Coroutine_Start start, void *value, void **result);
185Coroutine_API_FUNC(bool) Coroutine_IsStarted(void);
186Coroutine_API_FUNC(bool) Coroutine_IsRunning(Coroutine *cor);
187Coroutine_API_FUNC(bool) Coroutine_IsComplete(Coroutine *cor);
188
189Coroutine_API_FUNC(void) Coroutine_Dump_(void);
190
191#include "coroutine_names_undef.h"
192#endif
193