127 lines4.6 KB
1#ifndef COROUTINE_H
2#define COROUTINE_H
3
4#include <stdbool.h>
5#include <stdint.h>
6
7///////////////////////////////////////////////////////////////////////////////
8// Coroutine
9//
10// Coroutines for C, based on setjmp/longjmp.
11// Thread safe - each thread has its own coroutine system
12// Coroutines are cooperatively scheduled
13// Coroutines have their own stack (currently 16K each)
14// A coroutine can be continued, queried, or deleted on a different thread.
15//
16// Usage:
17// Coroutine_StartSystem(); // call once per thread before using coroutines
18// Coroutine *co = Coroutine_New(start_function);
19// void *result = Coroutine_Run(co, initial_value);
20// Coroutine_Delete(co);
21// Coroutine_StopSystem(); // call once per thread when done with coroutines
22//
23// Inside the coroutine function:
24// void *value = Coroutine_Yield(yield_value, on_yield, this);
25// ...
26// return return_value;
27//
28// To create a coroutine:
29// Coroutine *co = Coroutine_New(start_function);
30// To start or continue a coroutine:
31// void *result = Coroutine_Continue(co, value, early);
32// // early=true puts the coroutine at the head of the run queue
33// // early=false puts the coroutine at the tail of the run queue
34// To yield from inside a coroutine:
35// void *value = Coroutine_Yield(yield_value, on_yield, this);
36// // on_yield is called before the next coroutine is run
37// // 'this' is passed to on_yield as its parameter
38// // value is the value passed to Coroutine_Continue
39// To delete a coroutine:
40// Coroutine_Delete(co);
41// To get the value yielded from, or returned by a corotuine:
42// void *value = Coroutine_GetValue(co);
43// To get the currently running coroutine (NULL if none):
44// Coroutine *co = Coroutine_GetActive();
45// To check if a coroutine is currently running:
46// bool running = Coroutine_IsRunning(co);
47//
48// Notes:
49// Coroutine is not expected to be used directly, but as a foundation for
50// higher level constructs such as Generators, Async, etc.
51//
52///////////////////////////////////////////////////////////////////////////////
53
54
55// The stack is used as follows:
56// +------------------+ <- stack top
57// | coroutine header | <- more claimed as needed in Coroutine_New
58// +------------------+ <-
59// | coroutine stack | <-
60// +------------------+ <-
61// | coroutine header |
62// +------------------+
63// | coroutine stack |
64// +------------------+
65// | coroutine header |
66// +------------------+
67// | coroutine stack |
68// +------------------+
69// | coroutine header |
70// +------------------+
71// | coroutine stack |
72// +------------------+
73// | coroutine header |
74// +------------------+
75// | startup space | <- set aside by Coroutine_StartSystem
76// +------------------+
77// | caller | <- This calls Coroutine_StartSystem etc
78// +------------------+
79// | used stack |
80// +------------------+ <- stack bottom
81
82// Each coroutine has this much stack:
83#ifndef COROUTINE_STACK_SIZE
84 #define COROUTINE_STACK_SIZE 65536
85#endif
86
87// When Coroutine is started, an amount of stack is set aside to give
88// the caller of Coroutine_StartSystem a bit of room to work before calling
89// Coroutine_Run(), that is this amount:
90#ifndef COROUTINE_STARTUP_STACK_SIZE
91 #define COROUTINE_STARTUP_STACK_SIZE 4096
92#endif
93
94// Returned by Coroutine_StopSystem(), this summarises the coroutine session
95typedef struct Coroutine_Report {
96 unsigned coroutines_created;
97 unsigned coroutines_pool_size;
98 unsigned lowest_headroom;
99 uintptr_t stack_per_coroutine;
100} Coroutine_Report;
101
102typedef struct Coroutine Coroutine;
103
104typedef void (*Coroutine_YieldCallback)(void *me);
105typedef void *(*Coroutine_Start)(void *);
106
107extern void Coroutine_StartSystem(void);
108extern Coroutine_Report Coroutine_StopSystem(void);
109extern Coroutine *Coroutine_New(Coroutine_Start start);
110extern void Coroutine_Run_Coroutine(Coroutine *cor, void *value);
111extern void *Coroutine_Run(Coroutine_Start start, void *value);
112extern void Coroutine_Delete(Coroutine *cor);
113extern void Coroutine_Continue(Coroutine *cor, void *value, bool early);
114extern void *Coroutine_Yield(void *value, Coroutine_YieldCallback on_yield, void *me);
115extern void *Coroutine_GetValue(Coroutine *cor);
116extern Coroutine *Coroutine_GetActive(void);
117extern intptr_t Coroutine_GetStackHeadroom(void);
118extern void *Coroutine_GetStackHWM(void);
119extern void Coroutine_ClearStackForHWM(void);
120extern bool Coroutine_CanStartCoroutine(void *);
121extern void *Coroutine_GetCStackTop(void);
122extern void *Coroutine_Chain(Coroutine_Start start, void *value);
123extern bool Coroutine_IsStarted(void);
124extern bool Coroutine_IsRunning(Coroutine *cor);
125
126#endif
127