0 branches 0 tags
49 50
51
52 53
Stack headroom improvements
on 3:13 PM Oct 27 2025
trunk/README.md
50
51
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
Stackful coroutines in C.
* `Task` & `Future` coroutines which can pause, waiting for a future.
* `ASleep` an example of pausing `Task`s using `Future`s.
* `Generator` coroutines used as generators for loops.
* `Coroutine` the base coroutine engine.
Your code doesn't need to do anything special to be a coroutine. Only standard, or commonly available libraries are needed.
## Prerequisites
These libraries rely on as much as possible on C's cross-platform comfort zone. C's standard libraries are used as far as possible, but, as `threads.h` is not usually supported, `pthread.h` has been used instead.
You will need to build & link the code, `coroutine/*.c`, as part of your system, and ensure the headers, `include/*`, are available on your include path.
If your system doesn't have pthread, all the system-specific bits have been collected into cor_platform.c & .h. Replace these with versions which work with your platform.
## Quick Start
### Tasks
To run `Task`s:
#!C
#include "coroutine.h"
#include "task.h"
main(){
Coroutine_StartSystem();
void *res = NULL;
bool canceled = Task_Run(maintask, &param, &res);
Coroutine_StopSystem();
}
`Task_Run` runs tasks, switching between them when the current task waits on an `Future`. `maintask()` is run as a task. The start function for any task looks like this:
#!C
bool mytask(void *param, void **res){
// do your thing here
return canceled;
}
When `Task` returns from its start function, it returns whether it was canceled. Canceled `Task`s are assumed to have not finished what they were doing.
Within your main task, create `Task`s and `Task_Await()` them when you want to wait for their result:
#!C
Task task1;
Task_ctor(&task1, adifferenttask, &task1param);
void *result;
bool canceled = Task_Await(&task1, &result);
Task_dtor(&task1);
// use the result
When a task needs to wait for something, and wants to allow other tasks to run, it should use a `Future`:
#!C
Future future;
Future_ctor(&future);
// pass the future to the background-thing-which-might-take-a-while
void *res;
bool canceled = Future_Await(&future, &res);
Future_dtor(&future);
When the background-thing-which-might-take-a-while has a result:
#!C
Future_SetResult(future, false, result);
### ASleep
`ASleep()` needs its own system to be started to work:
#!C
ASleep_StartSystem()
Coroutine_StartSystem();
// Run tasks here which may now use ASLeep()
Coroutine_StopSystem();
ASleep_StopSystem();
Note that `ASleep_StartSystem()` / `ASleep_StopSystem()` is only needed once per process, whereas `Coroutine_StartSystem()` / `Coroutine_StopSystem()` is needed on each thread where coroutines are used.
Sleeping in a task:
#!C
bool mytask(void *param, void **result){
..
ASleep(time_to_sleep);
..
}
### Generators
The coroutine system needs to be started:
#!C
Coroutine_StartSYstem();
// you can use generators now
Coroutine_StopSystem();
Note that you need to start the coroutine system on each thread you want to use them.
You will need a generator function:
#!C
void *yield_my_things(void *param){
bool domore = true;
// loop/call functions to find more values to yield, and when you have one:
domore = Generator_Yield(thing);
// .. if domore is false, exit your generator - it is being destructed
// not actually used by generators, but this is a useful convention for bubbling
// the flag out to calling functions.
return (void *)domore;
}
And to use it:
#!C
Generator gen;
Generator_ctor(&gen, yield_my_things, "..");
void *thing;
while(Generator_Next(&gen, &thing)){
// use thing - a value yielded by your generator
}
Generator_dtor(&gen);
### Coroutines
While you can use coroutines directly, it's designed as a system to support more useful patterns, like `Async` and `Generators`.
The Coroutines system must be started:
#!C
Coroutine_StartSystem();
// use coroutines here
Coroutine_StopSystem();
Your coroutine will need to have a start function:
#!C
void *start(void *param){
...
}
When there is no coroutine running, start your 'main' coroutine:
#!C
void *result = Coroutine_Run(comain, param);
Create other coroutines like this:
#!C
Coroutine *cor = Coroutine_New(start);
When you want a Coroutine to run, or to return from a yield:
#!C
Coroutine_Continue(cor, value, run_early);
`value` will be start function's parameter, or the value returned from the yield.
Within the Coroutine, to yield a value:
#!C
void *Coroutine_Yield(value, on_yield, void *me);
The on_yield function is called after the coroutine has been 'wait'ed, but before the next coroutine is resumed.
## How it Works
The coroutine system uses the stack divided into smaller stacks for the coroutines. This means you may need to consider whether the coroutine stack size, set by `COROUTINE_STARTUP_STACK_SIZE`, is right for your application, and whether your stack size is enough for the number of coroutines you might run.
Each of your threads has its own stack - the coroutine system can be run (or not) independantly on each thread. For some special cases, you may want to adjust each of your thread's stack sizes depending on how it is used.
## Style
The style is influenced by C++. For example, where possible, a `Something *Something_New(a, b, c)` and `Something_Delete(Something *)` will have corresponding `Somthing_ctor(Somthing *, a, b, c)` and `Something_dtor(Something *)` to initialise and finalise a `Something` on the stack, or within another object. Using `.._ctor()` and `.._dtor()` will be faster as they avoid the `malloc()` and `free()`.
#!C
Something *oneofthem = Something_New();
// use oneofthem
Something_Delete(oneofthem);
Can be also be done like this, and this will run faster:
#!C
Something oneofthem;
Something_ctor(&oneofthem);
// use oneofthem
Something_dtor(&oneofthem);
The exception is `Coroutine_New()` and `Coroutine_Delete()`. The returned `Coroutine` is somewhere on your thread's stack - its memory is managed by the coroutine system, and is allocated and freed quickly.
## Usage
When you are using coroutines or generators:
#!C
void *myfunc(void *){
// your function here
}
Coroutine_StartSystem();
Coroutine_Run(myfunc, (void *)myparam);
Coroutine_StopSystem();
While the system is started, you can make many calls to `Coroutine_Run()` or `Task_Run()`, but only one of them can be running at once. A running system is thread local - each thread you want to use coroutines on will need to be `Coroutine_StartSystem()`ed.
## Stack Overruns
The C stack is divided down into smaller stacks. There's one to give some work room between `..StartSystem()` and `..Run()`, and one for each coroutine. These have guard markers which are checked to see if the stack has overrun. If there is a stack overrun, the system cannot continue - a message is output and the programe exited. There's a number of ways to avoid this issue:
* Use less stack. This is, sometimes, the right advice, especially if the startup stack overrins. The expectation is that very little is done between `.._StartSystem()` and `..Run()`. If your situation needs more doing, you can...
* increase the stack size. Adjust `COROUTINE_STACK_SIZE` (for startup) or `COROUTINE_STARTUP_STACK_SIZE` (for coroutines) as appropriate. If your use case is even more demanding, such as if you want 1000s of coroutines (so you need small stack chunks), /and/ some of them can recurse an unknown amount (so you need a deep stack for that coroutine), then you can...
* monitor stack headroom, and add another stack chunk if you need to:
In this last case you'll need to add some code at key points:
#!C
void *myfunction(void *param){
if (Coroutine_GetStackHeadroom() < MIN_ALLOWED_STACK){
return Coroutine_Chain(myfunction, param);
}
// do everything normally
}
More realistically:
#!C
struct myfunctionparams {
int a;
char *b;
struct dog *d;
}
void *mychain(void *param){
struct myfunctionparams *myparams = (struct myfunctionparams *)params;
return (void *)myfunction(myparams->a, myparams->b, *myparams->d);
}
int myfunction(int a, char *b, struct dog d){
if (Coroutine_GetStackHeadroom() < MIN_ALLOWED_STACK){
struct myfunctionparams params = {
a,
b,
&d
};
return (int)Coroutine_Chain(mychain, &params);
}
}
And if you want to panic if the C stack overruns:
#!C
if (Coroutine_GetStackHeadroom() < MIN_ALLOWED_COROUTINE_STACK){
if (Coroutine_HasCoroutinesInFreePool() ||
(char *)Coroutine_GetCStackTop() - c_stack_end >= MIN_ALLOWED_C_STACK) {
struct myfunctionparams params = {
a,
b,
&d
};
return (int)Coroutine_Chain(mychain, &params);
}
// panic now
}
# API
## Task & Future
The pattern for using async is:
#!C
bool mymaintask(void *param, void **result){
// do your main task things here, like starting more tasks
}
Coroutine_StartSystem();
void *res = NULL;
bool canceled = Task_Run(mymaintask, NULL, &res);
Coroutine_StopSystem();
To create and wait for a task:
#!C
Task task1;
Task_ctor(&task1, asynctask1, &task1param);
void *res = NULL;
bool canceled = Task_Await(&task1, void **res)
Task_dtor(&task1);
or, if you prefer new & delete:
#!C
Task *task1 = Task_New(asynctask1, &task1param);
void *res = NULL;
bool canceled = Task_Await(task1, void **res)
Task_Delete(task1);
Inside your task, when there is something to wait for and you want other tasks to run while your task is waiting, you will need a future:
#!C
Future future;
Future_ctor(&future);
// keep &future to hand for when the background thing completes
bool canceled = Future_Await(&future, NULL);
Future_dtor(&future);
`Future_New()` and `Future_Delete()` are also available if you prefer that style.
Inside the callback when the background thing is complete:
#!C
// result is a void *
Future_SetResult(future, result, false);
or, if something went wrong:
#!C
// exception is a void *
Future_SetResult(future, exception, true);
Back in the task, you can respond to the future:
#!C
... Future_Await has returned
if (canceled){
// exit quickly - you've been canceled
// you could, for example, use the future's result as an exception, or error code here
}
// carry on - the future's result may be an actual result, that's up to you
##### void Future_ctor(Future *fut)
fut
: The `Future` being constructed
Initialise a future. When you no longer need it, use `Future_dtor()`.
##### Future *Future_New()
(returns)
: The new future
Allocates and initialises a future, When you no longer need it, use `Future_Delete()`.
##### void Future_dtor(Future *fut)
fut
: The `Future` being destructed
Destruct a future previously constructed with `Future_ctor()`.
##### void Future_Delete(Future *fut)
fut
: The `Future` to be destructed and freed
Delete (finalise and free) a future previously new'ed with `Future_New()`
##### void Future_SetResult(Future *fut, bool canceled, void *value)
fut
: The `Future` whose result is being set
canceled
: The future's `canceled` setting
value
: The future's result `value`
Set the result of a future. This has an effect only the first time its done, ie a completed future can't be canceled and a canceled future can't be completed. When an `Future` has a result, its watchers are called back.
The `value` of a future might be a result if the future completes (when `canceled == false`), or could be some sort of exception value if `canceled == true`. The interpretation of a future's `value` is up to the user - as far as the async system is concerned, it's only a `void *`.
##### bool Future_GetResult(Future *fut, void **res)
(returns)
: The `canceled` value of the `Future`.
res
: Where to store the value of the `Future`. This may be `NULL`.
Get the result of a future.
##### typedef void (*Future_Watcher)(void *me, Future *fut)
A `Future_Watcher` is a callback called when a future has a result. The `me` parameter is the one passed to `Future_AddWatcher()`. `fut` is the future which has just got its result.
##### void Future_AddWatcher(Future *fut, Future_Watcher watcher, void *me)
fut
: the `Future` to add a watcher to
watcher
: the callback to call when the `Future` has a result.
me
: the `me` value to pass to `watcher` when it is called back.
Add a watcher (callback) to be called when the future has a result. If the future is already complete, `watcher` is immediately called. The `me` value is passed to the watcher as its `me` parameter. It is assumed that a watcher, identified by the `(watcher, me)` pair, will only be added once.
##### void Future_RemoveWatcher(Future *fut, Future_Watcher watcher, void *me)
fut
: the `Future` to remove a watcher from
watcher
: the callback of the watcher to remove.
me
: the `me` value of the watcher to remove.
Remove a watcher from a future. It is not an error if no watcher matching `(watcher, me)` is found - it has probably already been called back.
##### bool Future_Await(Future *fut, void **res)
(returns)
: whether the `Future` was canceled.
fut
: The `Future` to wait for.
res
: Where to store the `value` of the future when it is has a result. May be `NULL`.
The current `Task` is paused until the `Future` has a result. Other `Task`s are run while this one is waiting.
##### typedef bool (*Task_Entry)(void *param, void **res)
The entry function to an `Task`.
##### void Task_ctor(Task *tsk, Task_Entry entry, void *param)
tsk
: The task to construct.
entry
: The entry function for the task.
param
: The value for `param` to pass to `entry`.
Initialises an `Task`. When you have finished with an `Task` you must finalise it using `Task_dtor()`
#!C
Task tsk;
Task_ctor(&tsk, mytask, myparam);
// tsk will run if you wait for a task or future
Task_Await(&tsk, NULL);
Task_dtor(&tsk);
##### Task *Task_New(Task_Entry entry, void *param)
(returns)
: The new `Task`.
entry
: The entry function for the task.
param
: The value for `param` to pass to `entry`.
This allocates and initialises a new `Task`. When you have finished with your task, you must `Task_Delete()` it.
#!C
Task *tsk = Task_New(mytask, myparam);
// tsk will run if you wait for a task or future
Task_Await(tsk, NULL);
Task_Delete(tsk);
##### void Task_dtor(Task *tsk)
tsk
: The `Task` to destruct.
This finalises an `Task` you ealier initalised with `Task_ctor()`. It is an error to attempt to destruct a task which is running.
#!C
Task tsk;
Task_ctor(&tsk, mytask, myparam);
// use tsk
Task_dtor(&tsk);
##### void Task_Delete(Task *tsk)
tsk
: The `Task` to delete.
This finalises and frees an `Task` you ealier new'ed with `Task_New()`. It is an error to attempt to delete a task which is running.
#!C
Task *tsk = Task_New(mytask, myparam);
// use tsk
Task_Delete(tsk);
##### static inline bool Task_Await(Task *tsk, void **res)
(returns)
: Whether the task was canceled.
tsk
: The `Task` to wait for.
res
: Where to store the `Task`'s value when it finishes. This may be NULL.
The current `Task` waits for `tsk` to finish, and returns the result.
##### void Task_Cancel(Task *tsk, void *cancel_value)
tsk
: The task to cancel.
cancel_value
: The value to set on any future this task waits on.
This marks a task as canceled. When that task waits on a future that future will be canceled too, using `cancel_value`.
##### static inline bool Task_IsCanceled(Task *tsk)
(returns)
: Whether the task is canceled.
tsk
: The task to get its canceled setting from.
##### static inline Future *Task_GetAwaitedFuture(Task *tsk)
(returns)
: The future the task is waiting on. May be NULL.
tsk
: Teh task to read the future it is waiting on.
Return the future a task is waiting on.
##### bool Task_Run(Task_Entry start, void *value, void **res)
(returns)
: Whether `start` was canceled.
start
: The function to use as the main task.
value
: The value to pass to `start`.
res
: Where to store the result of `start`.
Runs `start` as an `Task`. When `start` returns all other tasks must have been destructed, using `Task_dtor()` or `Task_Delete()`.
## ASleep
##### void ASleep_StartSystem()
You must start the `ASleep` system to use it. This needs to happen per process (whereas `Coroutine_StartSystem()` needs to happen per-thread). Once you've finished with `ASleep` you must `ASleep_StopSystem()`.
#!C
ASleep_StartSystem();
// Now you can use ASleep() on any thread
ASleep_StopSystem();
##### void ASleep_StopSystem()
Call this to stop the `ASleep` system.
##### bool ASleep(float delay, void **value)
(returns)
: Whether the task was canceled.
delay
: How many seconds to delay for.
value
: Where to store the cancellation value. This may be NULL.
Sleep for `delay` seconds. `*value` will be set to `NULL` if the sleep is successful, and the `cancel_value` if the task is canceled.
## Generator
The pattern for a `Generator` is:
#### A loop which uses the `Generator
#!C
Generator gen;
Generator_ctor(&gen, mygen, &param);
void *value;
while(Generator_Next(&gen, &value)){
// use value here
}
// value is now the return value from the Generator
Generator_dtor(&gen);
Or:
#!C
Generator *gen = Generator_New(mygen, &param);
void *value;
while(Generator_Next(gen, &value)){
// use value here
}
Generator_Delete(gen);
`Generator`s yield a series of `void *`s - what the `void *`s mean is up to you. `Generator_Next()` returns a `bool` to indicate whether the `Generator` has finished.
#### A generator function
#!C
void *mygen(void *param){
bool domore = true;
// The parameter is a pointer to a string of chars
for (char *str = param; *str; ++str) {
// The value yielded is a pointer to a character in the string
domore = Generator_Yield(str);
if (!domore){
break;
}
}
return (void *)domore;
}
The `bool` returned from `Generator_Yield()` indicates whether the generator function should yield more values. When it is `false` the `Generator` is being finalised - your generator function should close files, and release any other resources it has claimed, before exiting.
##### void Generator_ctor(Generator *gen, void *(*start)(void *), void *param)
gen
: The `Generator` to construct.
start
: The function which is the start/entry-point of the `Generator`.
param
: The value to pass to `start`.
Initialise a `Generator`. When you no longer need the `Generator`, use `Generator_dtor()` to destruct it.
#!C
Generator gen;
Generator_ctor(&gen, mystart, &params);
// Generator is used
// ... later:
Generator_dtor(&gen);
##### Generator *Generator_New(void *(*start)(void *), void *param)
start
: The function which is the start/entry-point of the `Generator`.
param
: The value to pass to `start`.
`new` a `Generator` - malloc, and initialise it. When you no longer need the `Generator` use `Generator_dtor` to finalise it.
#!C
Generator *gen = Generator_New(mystart, &params);
// Generator is used
// ... later:
Generator_Delete(gen);
##### void Generator_dtor(Generator *gen)
gen
: The `Generator` to destruct.
Finalise a `Generator`. Once a `Generator` is no longer needed, it must be finalised:
#!C
// earlier...
Generator gen;
Generator_ctor(&gen, mystart, &params);
// Generator is used
// the Generator is no longer needed
Generator_dtor(&gen);
##### void Generator_Delete(Generator *gen)
gen
: The `Generator` to delete.
Finalise then `free()` a `Generator`. Once a `new`ed `Generator` is no longer needed, it must be deleted:
#!C
// earlier...
Generator *gen = Generator_New(mystart, &params);
// Generator is used
// the Generator is no longer needed
Generator_Delete(gen);
##### bool Generator_Next(Generator *gen, void **value)
(returns)
: Whether there is a next value. `true` - there is a next value; `false` - the `Generator` has finished
gen
: The `Generator` to get the next value from.
value
: Where to store the next value.
Get the next value yielded by the `Generator`.
#!C
void *value;
while(Generator_Next(gen, &value)){
// use value here
}
The `Generator` feeds values to its client using `Generator_Yield()` - it is these values which `Generator_Next()` sets, in the example, `value` to.
When a `Generator` is finished it returns from `start`. When you call `Generator_Yield()` on a finished `Generator` it returns `false` and `value` will be the return value from `start`.
##### bool Generator_Yield(void *value)
(returns)
: Whether the `Generator` should do more.
value
: The `Generator`'s next value.
Yield a value from a `Generator`.
#!C
bool domore = Generator_Yield(value);
`value` is then provided by `Generator_Next()` as the next value from the generator.
The `bool` returned by `Generator_Yield()` says whether more values should be provided by your generator function. `true` - provide more values if there are any. `false` - close files, free memory, free up any other resources and `return`. `false` is returned when the `Generator` is being finalised before it has finished, ie the client has exited its `for`-loop early.
## Coroutine
##### void Coroutine_StartSystem()
Start the coroutine system on this thread. When you've finished with `Coroutine` must call `Coroutine_Stop()`.
#!C
Coroutine_Start();
// prepare
void *result = Coroutine_Run(...);
// use result
Coroutine_Stop();
`Coroutine` can be started & stopped many times. While `Coroutine` is started, `Coroutine_Run()` can be called any number of times.
The total stack allowed for all coroutines running on any thread is the size of the call stack on that thread.
##### Coroutine_Report Coroutine_StopSystem()
Stop the coroutine system on this thread. A `Coroutine_Report` is returned, which summarises the coroutine activity on this thread:
#!C
typedef struct Coroutine_Report {
unsigned coroutines_created;
unsigned coroutines_pool_size;
unsigned lowest_headroom;
} Coroutine_Report;
coroutines_created
: How many coroutines were created
coroutines_pool_size
: The size of the coroutine pool (count of available, free `Coroutine` objects) when the system stopped. This is also the peak number of active coroutines. This will give you an idea of how much stack was needed for your coroutines.
lowest_headroom
: The lowest headroom (unused bytes before the guard word) any of the coroutine had.
##### Coroutine_Start
#!C
void *(*)(void *param)
The entry function for a coroutine. The `param` is the value passed to `Coroutine_Continue`, and the `void *` return value can be accessed through the `Coroutine` object using `Coroutine_GetValue()`.
##### Coroutine *Coroutine_New(Coroutine_Start start)
Create a new `Coroutine`. The `Coroutine` system must be started to create a `Coroutine`. The stack size available to the coroutine will be `COROUTINE_STACK_SIZE` defined in `coroutine.h`. When you have finished with your `Coroutine`, use `Coroutine_Delete()` to delete it.
##### void Coroutine_Run_Coroutine(Coroutine *cor, void *value)
Run the `Coroutine` and return when it returns. This is how to start coroutines running in the coroutine system. It is an error for the run coroutine to return before all other coroutines have completed.
##### void *Coroutine_Run(Coroutine_Start start, void *value)
Convenience wrapper for `Coroutine_Run_Coroutine` which creates the `Coroutine` and retrieves its result.
##### void Coroutine_Delete(Coroutine *cor)
Use `Coroutine_Delete()` to delete a coroutine when it is no longer needed. It is an error to attempt to delete a coroutine which is running.
##### void Coroutine_Continue(Coroutine *cor, void *value, bool early)
Continue the given `Coroutine`. `value` is passed to the coroutine, as `param` to the `start` function, or as the return value from `Coroutine_Yield`. `early` determines whether the continued coroutine is added to the head of tail of the list of runable coroutines.
##### void *Coroutine_Yield(void *value, Coroutine_YieldCallback on_yield, void *this)
Yield `value` from the current coroutine; this coroutine is moved to the list of coroutines waiting to be continued. The next runable coroutine is run - either by its start routine being called with `value` as its `param`, or by `value`being returned from its `Coroutine_Yield()`.
##### void *Coroutine_GetValue(Coroutine *cor)
Return the `Coroutine`'s value - the value last yielded, or returned by its `start` routine.
##### Coroutine *Coroutine_GetActive()
Return whihc coroutine is currently running, ie the caller's `Coroutine`.
##### bool Coroutine_IsRunning(Coroutine *cor)
Return whether the given coroutine is still running - it may be running, ready to run, or waiting to be continued, but won't have returned from its `start` function.
##### int Coroutine_GetStackHeadroom()
Return the headroom available in the current coroutine's stack. This can be used to detect when your coroutine is nearing its stack limit, and then use `Coroutine_Chain()` to continue in a new chunk of coroutine stack.
##### bool Coroutine_HasCoroutinesInFreePool()
Return whether the coroutine system has any coroutine stacks available in its coroutine stack free pool.
##### void *Coroutine_GetCStackTop()
Return an address which is near to the top of used C stack.
##### void *Coroutine_Chain(Coroutine_Start start, void *value)
Run `start` with `value` on a new coroutine, and return its return value. It is expected that `Coroutine_Chain()` will be used when your coroutine is running short of stack - it is not an alternative to `Coroutine_Run()`.
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
Stackful coroutines in C.
* `Task` & `Future` coroutines which can pause, waiting for a future.
* `ASleep` an example of pausing `Task`s using `Future`s.
* `Generator` coroutines used as generators for loops.
* `Coroutine` the base coroutine engine.
Your code doesn't need to do anything special to be a coroutine. Only standard, or commonly available libraries are needed.
## Prerequisites
These libraries rely on as much as possible on C's cross-platform comfort zone. C's standard libraries are used as far as possible, but, as `threads.h` is not usually supported, `pthread.h` has been used instead.
You will need to build & link the code, `coroutine/*.c`, as part of your system, and ensure the headers, `include/*`, are available on your include path.
If your system doesn't have pthread, all the system-specific bits have been collected into cor_platform.c & .h. Replace these with versions which work with your platform.
## Quick Start
### Tasks
To run `Task`s:
#!C
#include "coroutine.h"
#include "task.h"
main(){
Coroutine_StartSystem();
void *res = NULL;
bool canceled = Task_Run(maintask, &param, &res);
Coroutine_StopSystem();
}
`Task_Run` runs tasks, switching between them when the current task waits on an `Future`. `maintask()` is run as a task. The start function for any task looks like this:
#!C
bool mytask(void *param, void **res){
// do your thing here
return canceled;
}
When `Task` returns from its start function, it returns whether it was canceled. Canceled `Task`s are assumed to have not finished what they were doing.
Within your main task, create `Task`s and `Task_Await()` them when you want to wait for their result:
#!C
Task task1;
Task_ctor(&task1, adifferenttask, &task1param);
void *result;
bool canceled = Task_Await(&task1, &result);
Task_dtor(&task1);
// use the result
When a task needs to wait for something, and wants to allow other tasks to run, it should use a `Future`:
#!C
Future future;
Future_ctor(&future);
// pass the future to the background-thing-which-might-take-a-while
void *res;
bool canceled = Future_Await(&future, &res);
Future_dtor(&future);
When the background-thing-which-might-take-a-while has a result:
#!C
Future_SetResult(future, false, result);
### ASleep
`ASleep()` needs its own system to be started to work:
#!C
ASleep_StartSystem()
Coroutine_StartSystem();
// Run tasks here which may now use ASLeep()
Coroutine_StopSystem();
ASleep_StopSystem();
Note that `ASleep_StartSystem()` / `ASleep_StopSystem()` is only needed once per process, whereas `Coroutine_StartSystem()` / `Coroutine_StopSystem()` is needed on each thread where coroutines are used.
Sleeping in a task:
#!C
bool mytask(void *param, void **result){
..
ASleep(time_to_sleep);
..
}
### Generators
The coroutine system needs to be started:
#!C
Coroutine_StartSYstem();
// you can use generators now
Coroutine_StopSystem();
Note that you need to start the coroutine system on each thread you want to use them.
You will need a generator function:
#!C
void *yield_my_things(void *param){
bool domore = true;
// loop/call functions to find more values to yield, and when you have one:
domore = Generator_Yield(thing);
// .. if domore is false, exit your generator - it is being destructed
// not actually used by generators, but this is a useful convention for bubbling
// the flag out to calling functions.
return (void *)domore;
}
And to use it:
#!C
Generator gen;
Generator_ctor(&gen, yield_my_things, "..");
void *thing;
while(Generator_Next(&gen, &thing)){
// use thing - a value yielded by your generator
}
Generator_dtor(&gen);
### Coroutines
While you can use coroutines directly, it's designed as a system to support more useful patterns, like `Async` and `Generators`.
The Coroutines system must be started:
#!C
Coroutine_StartSystem();
// use coroutines here
Coroutine_StopSystem();
Your coroutine will need to have a start function:
#!C
void *start(void *param){
...
}
When there is no coroutine running, start your 'main' coroutine:
#!C
void *result = Coroutine_Run(comain, param);
Create other coroutines like this:
#!C
Coroutine *cor = Coroutine_New(start);
When you want a Coroutine to run, or to return from a yield:
#!C
Coroutine_Continue(cor, value, run_early);
`value` will be start function's parameter, or the value returned from the yield.
Within the Coroutine, to yield a value:
#!C
void *Coroutine_Yield(value, on_yield, void *me);
The on_yield function is called after the coroutine has been 'wait'ed, but before the next coroutine is resumed.
## How it Works
The coroutine system uses the stack divided into smaller stacks for the coroutines. This means you may need to consider whether the coroutine stack size, set by `COROUTINE_STARTUP_STACK_SIZE`, is right for your application, and whether your stack size is enough for the number of coroutines you might run.
Each of your threads has its own stack - the coroutine system can be run (or not) independantly on each thread. For some special cases, you may want to adjust each of your thread's stack sizes depending on how it is used.
## Style
The style is influenced by C++. For example, where possible, a `Something *Something_New(a, b, c)` and `Something_Delete(Something *)` will have corresponding `Somthing_ctor(Somthing *, a, b, c)` and `Something_dtor(Something *)` to initialise and finalise a `Something` on the stack, or within another object. Using `.._ctor()` and `.._dtor()` will be faster as they avoid the `malloc()` and `free()`.
#!C
Something *oneofthem = Something_New();
// use oneofthem
Something_Delete(oneofthem);
Can be also be done like this, and this will run faster:
#!C
Something oneofthem;
Something_ctor(&oneofthem);
// use oneofthem
Something_dtor(&oneofthem);
The exception is `Coroutine_New()` and `Coroutine_Delete()`. The returned `Coroutine` is somewhere on your thread's stack - its memory is managed by the coroutine system, and is allocated and freed quickly.
## Usage
When you are using coroutines or generators:
#!C
void *myfunc(void *){
// your function here
}
Coroutine_StartSystem();
Coroutine_Run(myfunc, (void *)myparam);
Coroutine_StopSystem();
While the system is started, you can make many calls to `Coroutine_Run()` or `Task_Run()`, but only one of them can be running at once. A running system is thread local - each thread you want to use coroutines on will need to be `Coroutine_StartSystem()`ed.
## Stack Overruns
The C stack is divided down into smaller stacks. There's one to give some work room between `..StartSystem()` and `..Run()`, and one for each coroutine. These have guard markers which are checked to see if the stack has overrun. If there is a stack overrun, the system cannot continue - a message is output and the programe exited. There's a number of ways to avoid this issue:
* Use less stack. This is, sometimes, the right advice, especially if the startup stack overrins. The expectation is that very little is done between `.._StartSystem()` and `..Run()`. If your situation needs more doing, you can...
* increase the stack size. Adjust `COROUTINE_STACK_SIZE` (for startup) or `COROUTINE_STARTUP_STACK_SIZE` (for coroutines) as appropriate. If your use case is even more demanding, such as if you want 1000s of coroutines (so you need small stack chunks), /and/ some of them can recurse an unknown amount (so you need a deep stack for that coroutine), then you can...
* monitor stack headroom, and add another stack chunk if you need to:
In this last case you'll need to add some code at key points:
#!C
void *myfunction(void *param){
if (Coroutine_GetStackHeadroom() < MIN_ALLOWED_STACK){
return Coroutine_Chain(myfunction, param);
}
// do everything normally
}
More realistically:
#!C
struct myfunctionparams {
int a;
char *b;
struct dog *d;
}
void *mychain(void *param){
struct myfunctionparams *myparams = (struct myfunctionparams *)params;
return (void *)myfunction(myparams->a, myparams->b, *myparams->d);
}
int myfunction(int a, char *b, struct dog d){
if (Coroutine_GetStackHeadroom() < MIN_ALLOWED_STACK){
struct myfunctionparams params = {
a,
b,
&d
};
return (int)Coroutine_Chain(mychain, &params);
}
}
And if you want to panic if the C stack overruns:
#!C
if (Coroutine_GetStackHeadroom() < MIN_ALLOWED_COROUTINE_STACK){
if (Coroutine_HasCoroutinesInFreePool() ||
(char *)Coroutine_GetCStackTop() - c_stack_end >= MIN_ALLOWED_C_STACK) {
struct myfunctionparams params = {
a,
b,
&d
};
return (int)Coroutine_Chain(mychain, &params);
}
// panic now
}
# API
## Task & Future
The pattern for using async is:
#!C
bool mymaintask(void *param, void **result){
// do your main task things here, like starting more tasks
}
Coroutine_StartSystem();
void *res = NULL;
bool canceled = Task_Run(mymaintask, NULL, &res);
Coroutine_StopSystem();
To create and wait for a task:
#!C
Task task1;
Task_ctor(&task1, asynctask1, &task1param);
void *res = NULL;
bool canceled = Task_Await(&task1, void **res)
Task_dtor(&task1);
or, if you prefer new & delete:
#!C
Task *task1 = Task_New(asynctask1, &task1param);
void *res = NULL;
bool canceled = Task_Await(task1, void **res)
Task_Delete(task1);
Inside your task, when there is something to wait for and you want other tasks to run while your task is waiting, you will need a future:
#!C
Future future;
Future_ctor(&future);
// keep &future to hand for when the background thing completes
bool canceled = Future_Await(&future, NULL);
Future_dtor(&future);
`Future_New()` and `Future_Delete()` are also available if you prefer that style.
Inside the callback when the background thing is complete:
#!C
// result is a void *
Future_SetResult(future, result, false);
or, if something went wrong:
#!C
// exception is a void *
Future_SetResult(future, exception, true);
Back in the task, you can respond to the future:
#!C
... Future_Await has returned
if (canceled){
// exit quickly - you've been canceled
// you could, for example, use the future's result as an exception, or error code here
}
// carry on - the future's result may be an actual result, that's up to you
##### void Future_ctor(Future *fut)
fut
: The `Future` being constructed
Initialise a future. When you no longer need it, use `Future_dtor()`.
##### Future *Future_New()
(returns)
: The new future
Allocates and initialises a future, When you no longer need it, use `Future_Delete()`.
##### void Future_dtor(Future *fut)
fut
: The `Future` being destructed
Destruct a future previously constructed with `Future_ctor()`.
##### void Future_Delete(Future *fut)
fut
: The `Future` to be destructed and freed
Delete (finalise and free) a future previously new'ed with `Future_New()`
##### void Future_SetResult(Future *fut, bool canceled, void *value)
fut
: The `Future` whose result is being set
canceled
: The future's `canceled` setting
value
: The future's result `value`
Set the result of a future. This has an effect only the first time its done, ie a completed future can't be canceled and a canceled future can't be completed. When an `Future` has a result, its watchers are called back.
The `value` of a future might be a result if the future completes (when `canceled == false`), or could be some sort of exception value if `canceled == true`. The interpretation of a future's `value` is up to the user - as far as the async system is concerned, it's only a `void *`.
##### bool Future_GetResult(Future *fut, void **res)
(returns)
: The `canceled` value of the `Future`.
res
: Where to store the value of the `Future`. This may be `NULL`.
Get the result of a future.
##### typedef void (*Future_Watcher)(void *me, Future *fut)
A `Future_Watcher` is a callback called when a future has a result. The `me` parameter is the one passed to `Future_AddWatcher()`. `fut` is the future which has just got its result.
##### void Future_AddWatcher(Future *fut, Future_Watcher watcher, void *me)
fut
: the `Future` to add a watcher to
watcher
: the callback to call when the `Future` has a result.
me
: the `me` value to pass to `watcher` when it is called back.
Add a watcher (callback) to be called when the future has a result. If the future is already complete, `watcher` is immediately called. The `me` value is passed to the watcher as its `me` parameter. It is assumed that a watcher, identified by the `(watcher, me)` pair, will only be added once.
##### void Future_RemoveWatcher(Future *fut, Future_Watcher watcher, void *me)
fut
: the `Future` to remove a watcher from
watcher
: the callback of the watcher to remove.
me
: the `me` value of the watcher to remove.
Remove a watcher from a future. It is not an error if no watcher matching `(watcher, me)` is found - it has probably already been called back.
##### bool Future_Await(Future *fut, void **res)
(returns)
: whether the `Future` was canceled.
fut
: The `Future` to wait for.
res
: Where to store the `value` of the future when it is has a result. May be `NULL`.
The current `Task` is paused until the `Future` has a result. Other `Task`s are run while this one is waiting.
##### typedef bool (*Task_Entry)(void *param, void **res)
The entry function to an `Task`.
##### void Task_ctor(Task *tsk, Task_Entry entry, void *param)
tsk
: The task to construct.
entry
: The entry function for the task.
param
: The value for `param` to pass to `entry`.
Initialises an `Task`. When you have finished with an `Task` you must finalise it using `Task_dtor()`
#!C
Task tsk;
Task_ctor(&tsk, mytask, myparam);
// tsk will run if you wait for a task or future
Task_Await(&tsk, NULL);
Task_dtor(&tsk);
##### Task *Task_New(Task_Entry entry, void *param)
(returns)
: The new `Task`.
entry
: The entry function for the task.
param
: The value for `param` to pass to `entry`.
This allocates and initialises a new `Task`. When you have finished with your task, you must `Task_Delete()` it.
#!C
Task *tsk = Task_New(mytask, myparam);
// tsk will run if you wait for a task or future
Task_Await(tsk, NULL);
Task_Delete(tsk);
##### void Task_dtor(Task *tsk)
tsk
: The `Task` to destruct.
This finalises an `Task` you ealier initalised with `Task_ctor()`. It is an error to attempt to destruct a task which is running.
#!C
Task tsk;
Task_ctor(&tsk, mytask, myparam);
// use tsk
Task_dtor(&tsk);
##### void Task_Delete(Task *tsk)
tsk
: The `Task` to delete.
This finalises and frees an `Task` you ealier new'ed with `Task_New()`. It is an error to attempt to delete a task which is running.
#!C
Task *tsk = Task_New(mytask, myparam);
// use tsk
Task_Delete(tsk);
##### static inline bool Task_Await(Task *tsk, void **res)
(returns)
: Whether the task was canceled.
tsk
: The `Task` to wait for.
res
: Where to store the `Task`'s value when it finishes. This may be NULL.
The current `Task` waits for `tsk` to finish, and returns the result.
##### void Task_Cancel(Task *tsk, void *cancel_value)
tsk
: The task to cancel.
cancel_value
: The value to set on any future this task waits on.
This marks a task as canceled. When that task waits on a future that future will be canceled too, using `cancel_value`.
##### static inline bool Task_IsCanceled(Task *tsk)
(returns)
: Whether the task is canceled.
tsk
: The task to get its canceled setting from.
##### static inline Future *Task_GetAwaitedFuture(Task *tsk)
(returns)
: The future the task is waiting on. May be NULL.
tsk
: Teh task to read the future it is waiting on.
Return the future a task is waiting on.
##### bool Task_Run(Task_Entry start, void *value, void **res)
(returns)
: Whether `start` was canceled.
start
: The function to use as the main task.
value
: The value to pass to `start`.
res
: Where to store the result of `start`.
Runs `start` as an `Task`. When `start` returns all other tasks must have been destructed, using `Task_dtor()` or `Task_Delete()`.
## ASleep
##### void ASleep_StartSystem()
You must start the `ASleep` system to use it. This needs to happen per process (whereas `Coroutine_StartSystem()` needs to happen per-thread). Once you've finished with `ASleep` you must `ASleep_StopSystem()`.
#!C
ASleep_StartSystem();
// Now you can use ASleep() on any thread
ASleep_StopSystem();
##### void ASleep_StopSystem()
Call this to stop the `ASleep` system.
##### bool ASleep(float delay, void **value)
(returns)
: Whether the task was canceled.
delay
: How many seconds to delay for.
value
: Where to store the cancellation value. This may be NULL.
Sleep for `delay` seconds. `*value` will be set to `NULL` if the sleep is successful, and the `cancel_value` if the task is canceled.
## Generator
The pattern for a `Generator` is:
#### A loop which uses the `Generator
#!C
Generator gen;
Generator_ctor(&gen, mygen, &param);
void *value;
while(Generator_Next(&gen, &value)){
// use value here
}
// value is now the return value from the Generator
Generator_dtor(&gen);
Or:
#!C
Generator *gen = Generator_New(mygen, &param);
void *value;
while(Generator_Next(gen, &value)){
// use value here
}
Generator_Delete(gen);
`Generator`s yield a series of `void *`s - what the `void *`s mean is up to you. `Generator_Next()` returns a `bool` to indicate whether the `Generator` has finished.
#### A generator function
#!C
void *mygen(void *param){
bool domore = true;
// The parameter is a pointer to a string of chars
for (char *str = param; *str; ++str) {
// The value yielded is a pointer to a character in the string
domore = Generator_Yield(str);
if (!domore){
break;
}
}
return (void *)domore;
}
The `bool` returned from `Generator_Yield()` indicates whether the generator function should yield more values. When it is `false` the `Generator` is being finalised - your generator function should close files, and release any other resources it has claimed, before exiting.
##### void Generator_ctor(Generator *gen, void *(*start)(void *), void *param)
gen
: The `Generator` to construct.
start
: The function which is the start/entry-point of the `Generator`.
param
: The value to pass to `start`.
Initialise a `Generator`. When you no longer need the `Generator`, use `Generator_dtor()` to destruct it.
#!C
Generator gen;
Generator_ctor(&gen, mystart, &params);
// Generator is used
// ... later:
Generator_dtor(&gen);
##### Generator *Generator_New(void *(*start)(void *), void *param)
start
: The function which is the start/entry-point of the `Generator`.
param
: The value to pass to `start`.
`new` a `Generator` - malloc, and initialise it. When you no longer need the `Generator` use `Generator_dtor` to finalise it.
#!C
Generator *gen = Generator_New(mystart, &params);
// Generator is used
// ... later:
Generator_Delete(gen);
##### void Generator_dtor(Generator *gen)
gen
: The `Generator` to destruct.
Finalise a `Generator`. Once a `Generator` is no longer needed, it must be finalised:
#!C
// earlier...
Generator gen;
Generator_ctor(&gen, mystart, &params);
// Generator is used
// the Generator is no longer needed
Generator_dtor(&gen);
##### void Generator_Delete(Generator *gen)
gen
: The `Generator` to delete.
Finalise then `free()` a `Generator`. Once a `new`ed `Generator` is no longer needed, it must be deleted:
#!C
// earlier...
Generator *gen = Generator_New(mystart, &params);
// Generator is used
// the Generator is no longer needed
Generator_Delete(gen);
##### bool Generator_Next(Generator *gen, void **value)
(returns)
: Whether there is a next value. `true` - there is a next value; `false` - the `Generator` has finished
gen
: The `Generator` to get the next value from.
value
: Where to store the next value.
Get the next value yielded by the `Generator`.
#!C
void *value;
while(Generator_Next(gen, &value)){
// use value here
}
The `Generator` feeds values to its client using `Generator_Yield()` - it is these values which `Generator_Next()` sets, in the example, `value` to.
When a `Generator` is finished it returns from `start`. When you call `Generator_Yield()` on a finished `Generator` it returns `false` and `value` will be the return value from `start`.
##### bool Generator_Yield(void *value)
(returns)
: Whether the `Generator` should do more.
value
: The `Generator`'s next value.
Yield a value from a `Generator`.
#!C
bool domore = Generator_Yield(value);
`value` is then provided by `Generator_Next()` as the next value from the generator.
The `bool` returned by `Generator_Yield()` says whether more values should be provided by your generator function. `true` - provide more values if there are any. `false` - close files, free memory, free up any other resources and `return`. `false` is returned when the `Generator` is being finalised before it has finished, ie the client has exited its `for`-loop early.
## Coroutine
##### void Coroutine_StartSystem()
Start the coroutine system on this thread. When you've finished with `Coroutine` must call `Coroutine_Stop()`.
#!C
Coroutine_Start();
// prepare
void *result = Coroutine_Run(...);
// use result
Coroutine_Stop();
`Coroutine` can be started & stopped many times. While `Coroutine` is started, `Coroutine_Run()` can be called any number of times.
The total stack allowed for all coroutines running on any thread is the size of the call stack on that thread.
##### Coroutine_Report Coroutine_StopSystem()
Stop the coroutine system on this thread. A `Coroutine_Report` is returned, which summarises the coroutine activity on this thread:
#!C
typedef struct Coroutine_Report {
unsigned coroutines_created;
unsigned coroutines_pool_size;
unsigned lowest_headroom;
} Coroutine_Report;
coroutines_created
: How many coroutines were created
coroutines_pool_size
: The size of the coroutine pool (count of available, free `Coroutine` objects) when the system stopped. This is also the peak number of active coroutines. This will give you an idea of how much stack was needed for your coroutines.
lowest_headroom
: The lowest headroom (unused bytes before the guard word) any of the coroutine had.
##### Coroutine_Start
#!C
void *(*)(void *param)
The entry function for a coroutine. The `param` is the value passed to `Coroutine_Continue`, and the `void *` return value can be accessed through the `Coroutine` object using `Coroutine_GetValue()`.
##### Coroutine *Coroutine_New(Coroutine_Start start)
Create a new `Coroutine`. The `Coroutine` system must be started to create a `Coroutine`. The stack size available to the coroutine will be `COROUTINE_STACK_SIZE` defined in `coroutine.h`. When you have finished with your `Coroutine`, use `Coroutine_Delete()` to delete it.
##### void Coroutine_Run_Coroutine(Coroutine *cor, void *value)
Run the `Coroutine` and return when it returns. This is how to start coroutines running in the coroutine system. It is an error for the run coroutine to return before all other coroutines have completed.
##### void *Coroutine_Run(Coroutine_Start start, void *value)
Convenience wrapper for `Coroutine_Run_Coroutine` which creates the `Coroutine` and retrieves its result.
##### void Coroutine_Delete(Coroutine *cor)
Use `Coroutine_Delete()` to delete a coroutine when it is no longer needed. It is an error to attempt to delete a coroutine which is running.
##### void Coroutine_Continue(Coroutine *cor, void *value, bool early)
Continue the given `Coroutine`. `value` is passed to the coroutine, as `param` to the `start` function, or as the return value from `Coroutine_Yield`. `early` determines whether the continued coroutine is added to the head of tail of the list of runable coroutines.
##### void *Coroutine_Yield(void *value, Coroutine_YieldCallback on_yield, void *this)
Yield `value` from the current coroutine; this coroutine is moved to the list of coroutines waiting to be continued. The next runable coroutine is run - either by its start routine being called with `value` as its `param`, or by `value`being returned from its `Coroutine_Yield()`.
##### void *Coroutine_GetValue(Coroutine *cor)
Return the `Coroutine`'s value - the value last yielded, or returned by its `start` routine.
##### Coroutine *Coroutine_GetActive()
Return whihc coroutine is currently running, ie the caller's `Coroutine`.
##### bool Coroutine_IsRunning(Coroutine *cor)
Return whether the given coroutine is still running - it may be running, ready to run, or waiting to be continued, but won't have returned from its `start` function.
##### intptr_t Coroutine_GetStackHeadroom()
Return the headroom available in the current coroutine's stack. This can be used to detect when your coroutine is nearing its stack limit, and then use `Coroutine_Chain()` to continue in a new chunk of coroutine stack.
##### bool Coroutine_HasCoroutinesInFreePool()
Return whether the coroutine system has any coroutine stacks available in its coroutine stack free pool.
##### void *Coroutine_GetCStackTop()
Return an address which is near to the top of used C stack.
##### void *Coroutine_Chain(Coroutine_Start start, void *value)
Run `start` with `value` on a new coroutine, and return its return value. It is expected that `Coroutine_Chain()` will be used when your coroutine is running short of stack - it is not an alternative to `Coroutine_Run()`.
trunk/coroutine/coroutine.c
50
51
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#include "coroutine.h"
#include <assert.h>
#include <setjmp.h>
#include <stdbool.h>
#include <stddef.h>
#include "cor_platform.h"
static void Coroutine_RunNext(void);
static void _Coroutine_Continue(Coroutine *cor, void *value, bool early);
///////////////////////////////////////////////////////////////////////////////
// 2-way linked lists...
//
// Brought inline here to avoid namespace polution
///////////////////////////////////////////////////////////////////////////////
typedef struct List_Link List_Link;
struct List_Link {
List_Link *next;
List_Link *prev;
};
typedef struct List_Head List_Head;
struct List_Head {
union {
struct {
List_Link link;
List_Link *filler;
} fwd;
struct {
List_Link *filler;
List_Link link;
} back;
};
};
static inline bool List_IsEmpty(
const List_Head *list
){
return list->fwd.link.next == &list->back.link;
}
static inline List_Link *List_GetHead(
const List_Head *list
){
return List_IsEmpty(list) ? NULL : list->fwd.link.next;
}
// static inline List_Link *List_GetTail(
// const List_Head *list
// ){
// return List_IsEmpty(list) ? NULL : list->back.link.prev;
// }
#define OFFSETOF(Container, Field) ((char *)&((Container *)4)->Field - (char *)(Container *)4)
#define List_Link_Container(Container, Link, link) ((Container *)((char *)(link) - OFFSETOF(Container, Link)))
static inline void List_Init(
List_Head *list
){
list->fwd.link.next = &list->back.link;
list->fwd.link.prev = NULL;
list->back.link.prev = &list->fwd.link;
}
static inline void List_AddHead(
List_Head *list,
List_Link *link
){
List_Link *first = list->fwd.link.next;
link->next = first;
link->prev = &list->fwd.link;
first->prev = link;
list->fwd.link.next = link;
}
static inline void List_AddTail(
List_Head *list,
List_Link *link
){
List_Link *last = list->back.link.prev;
link->prev = last;
link->next = &list->back.link;
last->next = link;
list->back.link.prev = link;
}
static inline void List_Remove(
List_Link *link
){
link->prev->next = link->next;
link->next->prev = link->prev;
}
///////////////////////////////////////////////////////////////////////////////
// ...2-way linked lists
///////////////////////////////////////////////////////////////////////////////
typedef struct Coroutines Coroutines;
enum {
Coroutines_Idle,
Coroutines_Starting,
Coroutines_Started,
Coroutines_Active,
Coroutines_Stopping
};
enum {
Chunk_Initial,
Chunk_Create,
Chunk_Enter
};
typedef enum Coroutine_State {
Coroutine_Constructing,
Coroutine_Free,
Coroutine_Idle,
Coroutine_Running,
Coroutine_Waiting,
Coroutine_Complete
} Coroutine_State;
enum {
Coroutines_Init,
Coroutines_AllocatedChunk,
Coroutines_CoroutineComplete,
};
struct Coroutine {
Coroutines *coroutines; // so can work with it off-thread
List_Link link; // for whichever list it's on
jmp_buf buf; // how to get back to it
unsigned char *guard; // where the stack overrun guard is
Coroutine_Start start; // entry point
void *entry_param; // to pass to start
void *value; // yielded/returned
Coroutine_State state;
};
struct Coroutines {
_Cor_Mutex mutex;
jmp_buf controller; // to return from Coroutine_Run
jmp_buf chunk_allocated;// for chunk allocation
unsigned char *guard; // the stack guard for the startup sequence
// singletons
Coroutine *tip; // top of stack chunk
Coroutine *active; // currently running coroutine
Coroutine *primary; // Coroutine_Run coroutine
// lists
List_Head free;
List_Head inactive; // idle or complete
List_Head runable; // running or waiting to run
List_Head waiting; // yielded / waiting to run
_Cor_Mutex waiting_mutex;
// Summary of the system
Coroutine_Report report;
// state
char state;
};
_Cor_thread_local Coroutines g_c;
static void stack_chunk_chunk(Coroutine *parent);
static void stack_chunk_base(Coroutine *parent, unsigned char *guard);
// Check whether the guard is intact
static inline bool Check_Guard(
unsigned char *guard
){
return guard[0] == 0xde &&
guard[1] == 0xad &&
guard[2] == 0xbe &&
guard[3] == 0xef;
}
static void Coroutine_PrimeStackChunks(void)
{
unsigned char chunk_of_stack[COROUTINE_STARTUP_STACK_SIZE];
for (uintptr_t i = 0; i < COROUTINE_STARTUP_STACK_SIZE-3; i += 4){
chunk_of_stack[i+0] = 0xde;
chunk_of_stack[i+1] = 0xad;
chunk_of_stack[i+2] = 0xbe;
chunk_of_stack[i+3] = 0xef;
}
assert(Check_Guard(chunk_of_stack));
// Stacks grow down in memory (almost always), so if the caller of this function changes
// the guard before entering the coroutine system, it has overrun the startup stack
g_c.guard = chunk_of_stack;
stack_chunk_base(NULL, NULL);
}
static void stack_chunk_chunk(
Coroutine *parent
){
unsigned char chunk_of_stack[COROUTINE_STACK_SIZE];
for (uintptr_t i = 0; i < COROUTINE_STACK_SIZE-3; i += 4){
chunk_of_stack[i+0] = 0xde;
chunk_of_stack[i+1] = 0xad;
chunk_of_stack[i+2] = 0xbe;
chunk_of_stack[i+3] = 0xef;
}
stack_chunk_base(parent, chunk_of_stack);
}
static void stack_chunk_base(
Coroutine *parent,
unsigned char *guard
){
Coroutine here;
here.state = Coroutine_Constructing;
switch (setjmp(here.buf)) {
case Chunk_Initial:
// got here for the first time
// parent now has a chunk_of_stack - add it to the free list
if (parent) {
assert(parent->state == Coroutine_Constructing);
assert(Check_Guard(guard));
parent->guard = guard;
parent->state = Coroutine_Free;
List_AddHead(&g_c.free, &parent->link);
g_c.report.coroutines_pool_size += 1;
}
// note that here is the tip of the chunk-claim stack
here.coroutines = &g_c;
g_c.tip = &here;
// return to the coroutine allocator
longjmp(g_c.chunk_allocated, 1);
case Chunk_Create:
// request to create a new chunk on the stack
assert(here.state == Coroutine_Constructing);
stack_chunk_chunk(&here);
assert(false);
case Chunk_Enter:
// request to start a coroutine (ie use the chunk for a coroutine)
// arrive here with mutex locked
assert(here.state == Coroutine_Running);
g_c.active = &here;
_Cor_Mutex_Unlock(&g_c.mutex);
here.value = here.start(here.entry_param);
// check the guard
assert(Check_Guard(here.guard));
_Cor_Mutex_Lock(&g_c.mutex);
g_c.active = NULL;
assert(here.state == Coroutine_Running);
List_Remove(&here.link);
here.state = Coroutine_Complete;
List_AddTail(&g_c.inactive, &here.link);
// coroutine has completed
if (g_c.primary == &here) {
// if primary coroutine - return to Coroutine_Run
longjmp(g_c.controller, Coroutines_CoroutineComplete);
}
_Cor_Mutex_Unlock(&g_c.mutex);
Coroutine_RunNext();
assert(false);
}
}
static void Coroutine_RunNext(void)
{
// arrive here with mutex unlocked
_Cor_Mutex_Lock(&g_c.waiting_mutex);
_Cor_Mutex_Lock(&g_c.mutex);
Coroutine *next = List_Link_Container(Coroutine, link, List_GetHead(&g_c.runable));
assert(next->state == Coroutine_Running);
longjmp(next->buf, Chunk_Enter);
assert(false);
}
void Coroutine_StartSystem(void)
{
assert(g_c.state == Coroutines_Idle);
g_c.state = Coroutines_Starting;
_Cor_Mutex_ctor(&g_c.mutex);
g_c.tip = NULL;
g_c.active = NULL;
List_Init(&g_c.free);
List_Init(&g_c.inactive);
List_Init(&g_c.runable);
List_Init(&g_c.waiting);
_Cor_Mutex_ctor(&g_c.waiting_mutex);
_Cor_Mutex_Lock(&g_c.waiting_mutex);
g_c.report.coroutines_created = 0;
g_c.report.coroutines_pool_size = 0;
g_c.report.lowest_headroom = COROUTINE_STACK_SIZE;
// prime the chunk system
if (!setjmp(g_c.chunk_allocated)){
Coroutine_PrimeStackChunks();
assert(false);
}
assert(g_c.state == Coroutines_Starting);
g_c.state = Coroutines_Started;
}
Coroutine_Report Coroutine_StopSystem(void)
{
_Cor_Mutex_Lock(&g_c.mutex);
assert(g_c.state == Coroutines_Started);
g_c.state = Coroutines_Stopping;
uintptr_t stackminheadroom = COROUTINE_STACK_SIZE;
for (List_Link *link = g_c.free.fwd.link.next; link->next; link = link->next){
Coroutine *cor = List_Link_Container(Coroutine, link, link);
for (uintptr_t i = 4; i < COROUTINE_STACK_SIZE-3; i += 4){
if (!Check_Guard(&cor->guard[i])){
stackminheadroom = i < stackminheadroom ? i : stackminheadroom;
break;
}
}
}
g_c.report.lowest_headroom = stackminheadroom;
assert(List_IsEmpty(&g_c.inactive));
_Cor_Mutex_Unlock(&g_c.waiting_mutex);
_Cor_Mutex_dtor(&g_c.waiting_mutex);
assert(g_c.state == Coroutines_Stopping);
g_c.state = Coroutines_Idle;
_Cor_Mutex_Unlock(&g_c.mutex);
_Cor_Mutex_dtor(&g_c.mutex);
return g_c.report;
}
void Coroutine_Run_Coroutine(
Coroutine *cor,
void *value
){
Coroutines *cors = cor->coroutines;
assert(&g_c == cors);
_Cor_Mutex_Lock(&cors->mutex);
assert(cors->state == Coroutines_Started);
cors->state = Coroutines_Active;
cors->primary = cor;
_Coroutine_Continue(cor, value, true);
if (!setjmp(cors->controller)){
_Cor_Mutex_Unlock(&cors->mutex);
// check the guard
assert(Check_Guard(cors->guard));
// start the first coroutine
Coroutine_RunNext();
}
// arrive here with mutex locked
assert(List_IsEmpty(&cors->runable));
assert(List_IsEmpty(&cors->waiting));
assert(cors->state == Coroutines_Active);
cors->state = Coroutines_Started;
_Cor_Mutex_Unlock(&cors->mutex);
}
void *Coroutine_Run(
Coroutine_Start start,
void *value
){
Coroutine *cor = Coroutine_New(start);
Coroutine_Run_Coroutine(cor, value);
void *res = Coroutine_GetValue(cor);
Coroutine_Delete(cor);
return res;
}
Coroutine *Coroutine_New(
Coroutine_Start start
){
assert((g_c.state == Coroutines_Started && List_IsEmpty(&g_c.inactive)) || g_c.state == Coroutines_Active);
// if none free - add one
if (List_IsEmpty(&g_c.free)){
if (!setjmp(g_c.chunk_allocated)){
longjmp(g_c.tip->buf, Chunk_Create);
}
}
Coroutine *cor = List_Link_Container(Coroutine, link, List_GetHead(&g_c.free));
assert(cor->state == Coroutine_Free);
cor->state = Coroutine_Idle;
cor->start = start;
cor->value = NULL;
List_Remove(&cor->link);
List_AddHead(&g_c.inactive, &cor->link);
g_c.report.coroutines_created += 1;
return cor;
}
void Coroutine_Delete(
Coroutine *cor
){
Coroutines *cors = cor->coroutines;
_Cor_Mutex_Lock(&cors->mutex);
assert(cor->state == Coroutine_Idle || cor->state == Coroutine_Complete);
cor->state = Coroutine_Free;
List_Remove(&cor->link);
List_AddTail(&cors->free, &cor->link);
_Cor_Mutex_Unlock(&cors->mutex);
}
// Coroutine_Continue, assuming the mutex is claimed
static void _Coroutine_Continue(
Coroutine *cor,
void *value,
bool early
){
Coroutines *cors = cor->coroutines;
assert(cor->state == Coroutine_Idle || cor->state == Coroutine_Waiting);
cor->entry_param = value;
cor->state = Coroutine_Running;
List_Remove(&cor->link);
if ( early ) {
List_AddHead(&cors->runable, &cor->link);
} else {
List_AddTail(&cors->runable, &cor->link);
}
_Cor_Mutex_Unlock(&cors->waiting_mutex);
}
void Coroutine_Continue(
Coroutine *cor,
void *value,
bool early
){
Coroutines *cors = cor->coroutines;
_Cor_Mutex_Lock(&cors->mutex);
_Coroutine_Continue(cor, value, early);
_Cor_Mutex_Unlock(&cors->mutex);
}
void *Coroutine_Yield(
void *value,
Coroutine_YieldCallback on_yield,
void *yield_me
){
Coroutine *me = g_c.active;
assert(Check_Guard(me->guard));
_Cor_Mutex_Lock(&g_c.mutex);
Coroutines *cors = me->coroutines;
assert(me && me->state == Coroutine_Running && cors == &g_c);
me->value = value;
me->state = Coroutine_Waiting;
List_Remove(&me->link);
if (!List_IsEmpty(&cors->runable)){
_Cor_Mutex_Unlock(&cors->waiting_mutex);
}
List_AddTail(&cors->waiting, &me->link);
switch (setjmp(me->buf)){
case Chunk_Initial:
_Cor_Mutex_Unlock(&cors->mutex);
on_yield(yield_me);
Coroutine_RunNext();
case Chunk_Create:
assert(false);
case Chunk_Enter:
// arrive here with mutex locked
cors->active = me;
// when we return here - we are running again
assert(me->state == Coroutine_Running);
void *res = me->entry_param;
_Cor_Mutex_Unlock(&cors->mutex);
return res;
}
return NULL;
}
void *Coroutine_GetValue(
Coroutine *cor
){
return cor->value;
}
Coroutine *Coroutine_GetActive(void)
{
return g_c.active;
}
int Coroutine_GetStackHeadroom(void){
unsigned char tbuf[4];
return tbuf - g_c.active->guard - 4;
}
bool Coroutine_HasCoroutinesInFreePool(void){
return (g_c.state == Coroutines_Started || g_c.state == Coroutines_Active) && !List_IsEmpty(&g_c.free);
}
// Pass the address of a local variable through this to avoid warnings about taking its address
static inline void *mask_address(void *ptr){return ptr;}
void *Coroutine_GetCStackTop(void){
char here;
return (g_c.state == Coroutines_Started || g_c.state == Coroutines_Active) ? (void *)g_c.tip : mask_address(&here);
}
struct Coroutine_ChainParam {
Coroutine_Start start;
void *value;
Coroutine *ret;
};
static void *Coroutine_ChainFn(
void *param
){
struct Coroutine_ChainParam *params = (struct Coroutine_ChainParam *)param;
Coroutine_Continue(params->ret, params->start(params->value), true);
return NULL;
}
static void Coroutine_ChainYield(
void *unused
){
(void)unused;
}
void *Coroutine_Chain(
Coroutine_Start start,
void *value
){
assert(Check_Guard(Coroutine_GetActive()->guard));
Coroutine *cor = Coroutine_New(Coroutine_ChainFn);
struct Coroutine_ChainParam params = {
start,
value,
Coroutine_GetActive()
};
Coroutine_Continue(cor, &params, true);
void *res = Coroutine_Yield(NULL, Coroutine_ChainYield, NULL);
Coroutine_Delete(cor);
return res;
}
bool Coroutine_IsRunning(
Coroutine *cor
)
{
int state = cor->state;
return state == Coroutine_Running || state == Coroutine_Waiting;
}
bool Coroutine_IsStarted(void){
return g_c.state == Coroutines_Active || g_c.state == Coroutines_Started;
}
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#include "coroutine.h"
#include <assert.h>
#include <setjmp.h>
#include <stdbool.h>
#include <stddef.h>
#include "cor_platform.h"
static void Coroutine_RunNext(void);
static void _Coroutine_Continue(Coroutine *cor, void *value, bool early);
///////////////////////////////////////////////////////////////////////////////
// 2-way linked lists...
//
// Brought inline here to avoid namespace polution
///////////////////////////////////////////////////////////////////////////////
typedef struct List_Link List_Link;
struct List_Link {
List_Link *next;
List_Link *prev;
};
typedef struct List_Head List_Head;
struct List_Head {
union {
struct {
List_Link link;
List_Link *filler;
} fwd;
struct {
List_Link *filler;
List_Link link;
} back;
};
};
static inline bool List_IsEmpty(
const List_Head *list
){
return list->fwd.link.next == &list->back.link;
}
static inline List_Link *List_GetHead(
const List_Head *list
){
return List_IsEmpty(list) ? NULL : list->fwd.link.next;
}
// static inline List_Link *List_GetTail(
// const List_Head *list
// ){
// return List_IsEmpty(list) ? NULL : list->back.link.prev;
// }
#define OFFSETOF(Container, Field) ((char *)&((Container *)4)->Field - (char *)(Container *)4)
#define List_Link_Container(Container, Link, link) ((Container *)((char *)(link) - OFFSETOF(Container, Link)))
static inline void List_Init(
List_Head *list
){
list->fwd.link.next = &list->back.link;
list->fwd.link.prev = NULL;
list->back.link.prev = &list->fwd.link;
}
static inline void List_AddHead(
List_Head *list,
List_Link *link
){
List_Link *first = list->fwd.link.next;
link->next = first;
link->prev = &list->fwd.link;
first->prev = link;
list->fwd.link.next = link;
}
static inline void List_AddTail(
List_Head *list,
List_Link *link
){
List_Link *last = list->back.link.prev;
link->prev = last;
link->next = &list->back.link;
last->next = link;
list->back.link.prev = link;
}
static inline void List_Remove(
List_Link *link
){
link->prev->next = link->next;
link->next->prev = link->prev;
}
///////////////////////////////////////////////////////////////////////////////
// ...2-way linked lists
///////////////////////////////////////////////////////////////////////////////
typedef struct Coroutines Coroutines;
enum {
Coroutines_Idle,
Coroutines_Starting,
Coroutines_Started,
Coroutines_Active,
Coroutines_Stopping
};
enum {
Chunk_Initial,
Chunk_Create,
Chunk_Enter
};
typedef enum Coroutine_State {
Coroutine_Constructing,
Coroutine_Free,
Coroutine_Idle,
Coroutine_Running,
Coroutine_Waiting,
Coroutine_Complete
} Coroutine_State;
enum {
Coroutines_Init,
Coroutines_AllocatedChunk,
Coroutines_CoroutineComplete,
};
struct Coroutine {
Coroutines *coroutines; // so can work with it off-thread
List_Link link; // for whichever list it's on
jmp_buf buf; // how to get back to it
unsigned char *guard; // where the stack overrun guard is
Coroutine_Start start; // entry point
void *entry_param; // to pass to start
void *value; // yielded/returned
Coroutine_State state;
};
struct Coroutines {
_Cor_Mutex mutex;
jmp_buf controller; // to return from Coroutine_Run
jmp_buf chunk_allocated;// for chunk allocation
unsigned char *guard; // the stack guard for the startup sequence
// singletons
Coroutine *tip; // top of stack chunk
Coroutine *active; // currently running coroutine
Coroutine *primary; // Coroutine_Run coroutine
// lists
List_Head free;
List_Head inactive; // idle or complete
List_Head runable; // running or waiting to run
List_Head waiting; // yielded / waiting to run
_Cor_Mutex waiting_mutex;
// Summary of the system
Coroutine_Report report;
// state
char state;
};
_Cor_thread_local Coroutines g_c;
static void stack_chunk_chunk(Coroutine *parent);
static void stack_chunk_base(Coroutine *parent, unsigned char *guard);
// Check whether the guard is intact
static inline bool Check_Guard(
unsigned char *guard
){
return guard[0] == 0xde &&
guard[1] == 0xad &&
guard[2] == 0xbe &&
guard[3] == 0xef;
}
static void Coroutine_PrimeStackChunks(void)
{
unsigned char chunk_of_stack[COROUTINE_STARTUP_STACK_SIZE];
for (uintptr_t i = 0; i < COROUTINE_STARTUP_STACK_SIZE-3; i += 4){
chunk_of_stack[i+0] = 0xde;
chunk_of_stack[i+1] = 0xad;
chunk_of_stack[i+2] = 0xbe;
chunk_of_stack[i+3] = 0xef;
}
assert(Check_Guard(chunk_of_stack));
// Stacks grow down in memory (almost always), so if the caller of this function changes
// the guard before entering the coroutine system, it has overrun the startup stack
g_c.guard = chunk_of_stack;
stack_chunk_base(NULL, NULL);
}
static void stack_chunk_chunk(
Coroutine *parent
){
unsigned char chunk_of_stack[COROUTINE_STACK_SIZE];
for (uintptr_t i = 0; i < COROUTINE_STACK_SIZE-3; i += 4){
chunk_of_stack[i+0] = 0xde;
chunk_of_stack[i+1] = 0xad;
chunk_of_stack[i+2] = 0xbe;
chunk_of_stack[i+3] = 0xef;
}
stack_chunk_base(parent, chunk_of_stack);
}
static void stack_chunk_base(
Coroutine *parent,
unsigned char *guard
){
Coroutine here;
here.state = Coroutine_Constructing;
switch (setjmp(here.buf)) {
case Chunk_Initial:
// got here for the first time
// parent now has a chunk_of_stack - add it to the free list
if (parent) {
assert(parent->state == Coroutine_Constructing);
assert(Check_Guard(guard));
parent->guard = guard;
parent->state = Coroutine_Free;
List_AddHead(&g_c.free, &parent->link);
g_c.report.coroutines_pool_size += 1;
}
// note that here is the tip of the chunk-claim stack
here.coroutines = &g_c;
g_c.tip = &here;
// return to the coroutine allocator
longjmp(g_c.chunk_allocated, 1);
case Chunk_Create:
// request to create a new chunk on the stack
assert(here.state == Coroutine_Constructing);
stack_chunk_chunk(&here);
assert(false);
case Chunk_Enter:
// request to start a coroutine (ie use the chunk for a coroutine)
// arrive here with mutex locked
assert(here.state == Coroutine_Running);
g_c.active = &here;
_Cor_Mutex_Unlock(&g_c.mutex);
here.value = here.start(here.entry_param);
// check the guard
assert(Check_Guard(here.guard));
_Cor_Mutex_Lock(&g_c.mutex);
g_c.active = NULL;
assert(here.state == Coroutine_Running);
List_Remove(&here.link);
here.state = Coroutine_Complete;
List_AddTail(&g_c.inactive, &here.link);
// coroutine has completed
if (g_c.primary == &here) {
// if primary coroutine - return to Coroutine_Run
longjmp(g_c.controller, Coroutines_CoroutineComplete);
}
_Cor_Mutex_Unlock(&g_c.mutex);
Coroutine_RunNext();
assert(false);
}
}
static void Coroutine_RunNext(void)
{
// arrive here with mutex unlocked
_Cor_Mutex_Lock(&g_c.waiting_mutex);
_Cor_Mutex_Lock(&g_c.mutex);
Coroutine *next = List_Link_Container(Coroutine, link, List_GetHead(&g_c.runable));
assert(next->state == Coroutine_Running);
longjmp(next->buf, Chunk_Enter);
assert(false);
}
void Coroutine_StartSystem(void)
{
assert(g_c.state == Coroutines_Idle);
g_c.state = Coroutines_Starting;
_Cor_Mutex_ctor(&g_c.mutex);
g_c.tip = NULL;
g_c.active = NULL;
List_Init(&g_c.free);
List_Init(&g_c.inactive);
List_Init(&g_c.runable);
List_Init(&g_c.waiting);
_Cor_Mutex_ctor(&g_c.waiting_mutex);
_Cor_Mutex_Lock(&g_c.waiting_mutex);
g_c.report.coroutines_created = 0;
g_c.report.coroutines_pool_size = 0;
g_c.report.lowest_headroom = COROUTINE_STACK_SIZE;
// prime the chunk system
if (!setjmp(g_c.chunk_allocated)){
Coroutine_PrimeStackChunks();
assert(false);
}
assert(g_c.state == Coroutines_Starting);
g_c.state = Coroutines_Started;
}
Coroutine_Report Coroutine_StopSystem(void)
{
_Cor_Mutex_Lock(&g_c.mutex);
assert(g_c.state == Coroutines_Started);
g_c.state = Coroutines_Stopping;
uintptr_t stackminheadroom = COROUTINE_STACK_SIZE;
for (List_Link *link = g_c.free.fwd.link.next; link->next; link = link->next){
Coroutine *cor = List_Link_Container(Coroutine, link, link);
for (uintptr_t i = 4; i < COROUTINE_STACK_SIZE-3; i += 4){
if (!Check_Guard(&cor->guard[i])){
stackminheadroom = i < stackminheadroom ? i : stackminheadroom;
break;
}
}
}
g_c.report.lowest_headroom = stackminheadroom;
assert(List_IsEmpty(&g_c.inactive));
_Cor_Mutex_Unlock(&g_c.waiting_mutex);
_Cor_Mutex_dtor(&g_c.waiting_mutex);
assert(g_c.state == Coroutines_Stopping);
g_c.state = Coroutines_Idle;
_Cor_Mutex_Unlock(&g_c.mutex);
_Cor_Mutex_dtor(&g_c.mutex);
return g_c.report;
}
void Coroutine_Run_Coroutine(
Coroutine *cor,
void *value
){
Coroutines *cors = cor->coroutines;
assert(&g_c == cors);
_Cor_Mutex_Lock(&cors->mutex);
assert(cors->state == Coroutines_Started);
cors->state = Coroutines_Active;
cors->primary = cor;
_Coroutine_Continue(cor, value, true);
if (!setjmp(cors->controller)){
_Cor_Mutex_Unlock(&cors->mutex);
// check the guard
assert(Check_Guard(cors->guard));
// start the first coroutine
Coroutine_RunNext();
}
// arrive here with mutex locked
assert(List_IsEmpty(&cors->runable));
assert(List_IsEmpty(&cors->waiting));
assert(cors->state == Coroutines_Active);
cors->state = Coroutines_Started;
_Cor_Mutex_Unlock(&cors->mutex);
}
void *Coroutine_Run(
Coroutine_Start start,
void *value
){
Coroutine *cor = Coroutine_New(start);
Coroutine_Run_Coroutine(cor, value);
void *res = Coroutine_GetValue(cor);
Coroutine_Delete(cor);
return res;
}
Coroutine *Coroutine_New(
Coroutine_Start start
){
assert((g_c.state == Coroutines_Started && List_IsEmpty(&g_c.inactive)) || g_c.state == Coroutines_Active);
// if none free - add one
if (List_IsEmpty(&g_c.free)){
if (!setjmp(g_c.chunk_allocated)){
longjmp(g_c.tip->buf, Chunk_Create);
}
}
Coroutine *cor = List_Link_Container(Coroutine, link, List_GetHead(&g_c.free));
assert(cor->state == Coroutine_Free);
cor->state = Coroutine_Idle;
cor->start = start;
cor->value = NULL;
List_Remove(&cor->link);
List_AddHead(&g_c.inactive, &cor->link);
g_c.report.coroutines_created += 1;
return cor;
}
void Coroutine_Delete(
Coroutine *cor
){
Coroutines *cors = cor->coroutines;
_Cor_Mutex_Lock(&cors->mutex);
assert(cor->state == Coroutine_Idle || cor->state == Coroutine_Complete);
cor->state = Coroutine_Free;
List_Remove(&cor->link);
List_AddTail(&cors->free, &cor->link);
_Cor_Mutex_Unlock(&cors->mutex);
}
// Coroutine_Continue, assuming the mutex is claimed
static void _Coroutine_Continue(
Coroutine *cor,
void *value,
bool early
){
Coroutines *cors = cor->coroutines;
assert(cor->state == Coroutine_Idle || cor->state == Coroutine_Waiting);
cor->entry_param = value;
cor->state = Coroutine_Running;
List_Remove(&cor->link);
if ( early ) {
List_AddHead(&cors->runable, &cor->link);
} else {
List_AddTail(&cors->runable, &cor->link);
}
_Cor_Mutex_Unlock(&cors->waiting_mutex);
}
void Coroutine_Continue(
Coroutine *cor,
void *value,
bool early
){
Coroutines *cors = cor->coroutines;
_Cor_Mutex_Lock(&cors->mutex);
_Coroutine_Continue(cor, value, early);
_Cor_Mutex_Unlock(&cors->mutex);
}
void *Coroutine_Yield(
void *value,
Coroutine_YieldCallback on_yield,
void *yield_me
){
Coroutine *me = g_c.active;
assert(Check_Guard(me->guard));
_Cor_Mutex_Lock(&g_c.mutex);
Coroutines *cors = me->coroutines;
assert(me && me->state == Coroutine_Running && cors == &g_c);
me->value = value;
me->state = Coroutine_Waiting;
List_Remove(&me->link);
if (!List_IsEmpty(&cors->runable)){
_Cor_Mutex_Unlock(&cors->waiting_mutex);
}
List_AddTail(&cors->waiting, &me->link);
switch (setjmp(me->buf)){
case Chunk_Initial:
_Cor_Mutex_Unlock(&cors->mutex);
on_yield(yield_me);
Coroutine_RunNext();
case Chunk_Create:
assert(false);
case Chunk_Enter:
// arrive here with mutex locked
cors->active = me;
// when we return here - we are running again
assert(me->state == Coroutine_Running);
void *res = me->entry_param;
_Cor_Mutex_Unlock(&cors->mutex);
return res;
}
return NULL;
}
void *Coroutine_GetValue(
Coroutine *cor
){
return cor->value;
}
Coroutine *Coroutine_GetActive(void)
{
return g_c.active;
}
intptr_t Coroutine_GetStackHeadroom(void){
unsigned char tbuf[4];
return g_c.active ? tbuf - g_c.active->guard - 4 : COROUTINE_STACK_SIZE;
}
bool Coroutine_HasCoroutinesInFreePool(void){
return (g_c.state == Coroutines_Started || g_c.state == Coroutines_Active) && !List_IsEmpty(&g_c.free);
}
// Pass the address of a local variable through this to avoid warnings about taking its address
static inline void *mask_address(void *ptr){return ptr;}
void *Coroutine_GetCStackTop(void){
char here;
return (g_c.state == Coroutines_Started || g_c.state == Coroutines_Active) ? (void *)g_c.tip : mask_address(&here);
}
struct Coroutine_ChainParam {
Coroutine_Start start;
void *value;
Coroutine *ret;
};
static void *Coroutine_ChainFn(
void *param
){
struct Coroutine_ChainParam *params = (struct Coroutine_ChainParam *)param;
Coroutine_Continue(params->ret, params->start(params->value), true);
return NULL;
}
static void Coroutine_ChainYield(
void *unused
){
(void)unused;
}
void *Coroutine_Chain(
Coroutine_Start start,
void *value
){
assert(Check_Guard(Coroutine_GetActive()->guard));
Coroutine *cor = Coroutine_New(Coroutine_ChainFn);
struct Coroutine_ChainParam params = {
start,
value,
Coroutine_GetActive()
};
Coroutine_Continue(cor, &params, true);
void *res = Coroutine_Yield(NULL, Coroutine_ChainYield, NULL);
Coroutine_Delete(cor);
return res;
}
bool Coroutine_IsRunning(
Coroutine *cor
)
{
int state = cor->state;
return state == Coroutine_Running || state == Coroutine_Waiting;
}
bool Coroutine_IsStarted(void){
return g_c.state == Coroutines_Active || g_c.state == Coroutines_Started;
}
trunk/examples/chaining_demo.c
50
51
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
#include <stdio.h>
#include <stddef.h>
#include "coroutine.h"
#include "generator.h"
#include "asleep.h"
#include "task.h"
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void *chaindeeper(void *param){
// enough headroom for printf on an Intel Mac - your system may be different
if (Coroutine_GetStackHeadroom() < 2000){
return Coroutine_Chain(chaindeeper, param);
}
printf("%ld %d\n", (long)param, Coroutine_GetStackHeadroom());
long depth = (long)param;
if (depth > 10000){
return NULL;
}
return chaindeeper((void *)(depth + 1));
}
void *chaintest(
void *param
){
(void)param;
chaindeeper((void *)0);
return param;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
Coroutine_StartSystem();
Coroutine_Run(chaintest, NULL);
Coroutine_Report report = Coroutine_StopSystem();
printf("%d routines using a pool of %d, min headroom %d\n", report.coroutines_created, report.coroutines_pool_size, report.lowest_headroom);
}
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
#include <stdio.h>
#include <stddef.h>
#include "coroutine.h"
#include "generator.h"
#include "asleep.h"
#include "task.h"
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void *chaindeeper(void *param){
// enough headroom for printf on an Intel Mac - your system may be different
if (Coroutine_GetStackHeadroom() < 2000){
return Coroutine_Chain(chaindeeper, param);
}
printf("%ld %ld\n", (long)param, Coroutine_GetStackHeadroom());
long depth = (long)param;
if (depth > 10000){
return NULL;
}
return chaindeeper((void *)(depth + 1));
}
void *chaintest(
void *param
){
(void)param;
chaindeeper((void *)0);
return param;
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
Coroutine_StartSystem();
Coroutine_Run(chaintest, NULL);
Coroutine_Report report = Coroutine_StopSystem();
printf("%d routines using a pool of %d, min headroom %d\n", report.coroutines_created, report.coroutines_pool_size, report.lowest_headroom);
}
trunk/include/coroutine.h
50
51
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef COROUTINE_H
#define COROUTINE_H
#include <stdbool.h>
///////////////////////////////////////////////////////////////////////////////
// Coroutine
//
// Coroutines for C, based on setjmp/longjmp.
// Thread safe - each thread has its own coroutine system
// Coroutines are cooperatively scheduled
// Coroutines have their own stack (currently 16K each)
// A coroutine can be continued, queried, or deleted on a different thread.
//
// Usage:
// Coroutine_StartSystem(); // call once per thread before using coroutines
// Coroutine *co = Coroutine_New(start_function);
// void *result = Coroutine_Run(co, initial_value);
// Coroutine_Delete(co);
// Coroutine_StopSystem(); // call once per thread when done with coroutines
//
// Inside the coroutine function:
// void *value = Coroutine_Yield(yield_value, on_yield, this);
// ...
// return return_value;
//
// To create a coroutine:
// Coroutine *co = Coroutine_New(start_function);
// To start or continue a coroutine:
// void *result = Coroutine_Continue(co, value, early);
// // early=true puts the coroutine at the head of the run queue
// // early=false puts the coroutine at the tail of the run queue
// To yield from inside a coroutine:
// void *value = Coroutine_Yield(yield_value, on_yield, this);
// // on_yield is called before the next coroutine is run
// // 'this' is passed to on_yield as its parameter
// // value is the value passed to Coroutine_Continue
// To delete a coroutine:
// Coroutine_Delete(co);
// To get the value yielded from, or returned by a corotuine:
// void *value = Coroutine_GetValue(co);
// To get the currently running coroutine (NULL if none):
// Coroutine *co = Coroutine_GetActive();
// To check if a coroutine is currently running:
// bool running = Coroutine_IsRunning(co);
//
// Notes:
// Coroutine is not expected to be used directly, but as a foundation for
// higher level constructs such as Generators, Async, etc.
//
///////////////////////////////////////////////////////////////////////////////
// The stack is used as follows:
// +------------------+ <- stack top
// | coroutine header | <- more claimed as needed in Coroutine_New
// +------------------+ <-
// | coroutine stack | <-
// +------------------+ <-
// | coroutine header |
// +------------------+
// | coroutine stack |
// +------------------+
// | coroutine header |
// +------------------+
// | coroutine stack |
// +------------------+
// | coroutine header |
// +------------------+
// | coroutine stack |
// +------------------+
// | coroutine header |
// +------------------+
// | startup space | <- set aside by Coroutine_StartSystem
// +------------------+
// | caller | <- This calls Coroutine_StartSystem etc
// +------------------+
// | used stack |
// +------------------+ <- stack bottom
// Each coroutine has this much stack:
#ifndef COROUTINE_STACK_SIZE
#define COROUTINE_STACK_SIZE 65536
#endif
// When Coroutine is started, an amount of stack is set aside to give
// the caller of Coroutine_StartSystem a bit of room to work before calling
// Coroutine_Run(), that is this amount:
#ifndef COROUTINE_STARTUP_STACK_SIZE
#define COROUTINE_STARTUP_STACK_SIZE 4096
#endif
// Returned by Coroutine_StopSystem(), this summarises the coroutine session
typedef struct Coroutine_Report {
unsigned coroutines_created;
unsigned coroutines_pool_size;
unsigned lowest_headroom;
} Coroutine_Report;
typedef struct Coroutine Coroutine;
typedef void (*Coroutine_YieldCallback)(void *me);
typedef void *(*Coroutine_Start)(void *);
extern void Coroutine_StartSystem(void);
extern Coroutine_Report Coroutine_StopSystem(void);
extern Coroutine *Coroutine_New(Coroutine_Start start);
extern void Coroutine_Run_Coroutine(Coroutine *cor, void *value);
extern void *Coroutine_Run(Coroutine_Start start, void *value);
extern void Coroutine_Delete(Coroutine *cor);
extern void Coroutine_Continue(Coroutine *cor, void *value, bool early);
extern void *Coroutine_Yield(void *value, Coroutine_YieldCallback on_yield, void *me);
extern void *Coroutine_GetValue(Coroutine *cor);
extern Coroutine *Coroutine_GetActive(void);
extern int Coroutine_GetStackHeadroom(void);
extern bool Coroutine_HasCoroutinesInFreePool(void);
extern void *Coroutine_GetCStackTop(void);
extern void *Coroutine_Chain(Coroutine_Start start, void *value);
extern bool Coroutine_IsStarted(void);
extern bool Coroutine_IsRunning(Coroutine *cor);
#endif
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#ifndef COROUTINE_H
#define COROUTINE_H
#include <stdbool.h>
#include <stdint.h>
///////////////////////////////////////////////////////////////////////////////
// Coroutine
//
// Coroutines for C, based on setjmp/longjmp.
// Thread safe - each thread has its own coroutine system
// Coroutines are cooperatively scheduled
// Coroutines have their own stack (currently 16K each)
// A coroutine can be continued, queried, or deleted on a different thread.
//
// Usage:
// Coroutine_StartSystem(); // call once per thread before using coroutines
// Coroutine *co = Coroutine_New(start_function);
// void *result = Coroutine_Run(co, initial_value);
// Coroutine_Delete(co);
// Coroutine_StopSystem(); // call once per thread when done with coroutines
//
// Inside the coroutine function:
// void *value = Coroutine_Yield(yield_value, on_yield, this);
// ...
// return return_value;
//
// To create a coroutine:
// Coroutine *co = Coroutine_New(start_function);
// To start or continue a coroutine:
// void *result = Coroutine_Continue(co, value, early);
// // early=true puts the coroutine at the head of the run queue
// // early=false puts the coroutine at the tail of the run queue
// To yield from inside a coroutine:
// void *value = Coroutine_Yield(yield_value, on_yield, this);
// // on_yield is called before the next coroutine is run
// // 'this' is passed to on_yield as its parameter
// // value is the value passed to Coroutine_Continue
// To delete a coroutine:
// Coroutine_Delete(co);
// To get the value yielded from, or returned by a corotuine:
// void *value = Coroutine_GetValue(co);
// To get the currently running coroutine (NULL if none):
// Coroutine *co = Coroutine_GetActive();
// To check if a coroutine is currently running:
// bool running = Coroutine_IsRunning(co);
//
// Notes:
// Coroutine is not expected to be used directly, but as a foundation for
// higher level constructs such as Generators, Async, etc.
//
///////////////////////////////////////////////////////////////////////////////
// The stack is used as follows:
// +------------------+ <- stack top
// | coroutine header | <- more claimed as needed in Coroutine_New
// +------------------+ <-
// | coroutine stack | <-
// +------------------+ <-
// | coroutine header |
// +------------------+
// | coroutine stack |
// +------------------+
// | coroutine header |
// +------------------+
// | coroutine stack |
// +------------------+
// | coroutine header |
// +------------------+
// | coroutine stack |
// +------------------+
// | coroutine header |
// +------------------+
// | startup space | <- set aside by Coroutine_StartSystem
// +------------------+
// | caller | <- This calls Coroutine_StartSystem etc
// +------------------+
// | used stack |
// +------------------+ <- stack bottom
// Each coroutine has this much stack:
#ifndef COROUTINE_STACK_SIZE
#define COROUTINE_STACK_SIZE 65536
#endif
// When Coroutine is started, an amount of stack is set aside to give
// the caller of Coroutine_StartSystem a bit of room to work before calling
// Coroutine_Run(), that is this amount:
#ifndef COROUTINE_STARTUP_STACK_SIZE
#define COROUTINE_STARTUP_STACK_SIZE 4096
#endif
// Returned by Coroutine_StopSystem(), this summarises the coroutine session
typedef struct Coroutine_Report {
unsigned coroutines_created;
unsigned coroutines_pool_size;
unsigned lowest_headroom;
} Coroutine_Report;
typedef struct Coroutine Coroutine;
typedef void (*Coroutine_YieldCallback)(void *me);
typedef void *(*Coroutine_Start)(void *);
extern void Coroutine_StartSystem(void);
extern Coroutine_Report Coroutine_StopSystem(void);
extern Coroutine *Coroutine_New(Coroutine_Start start);
extern void Coroutine_Run_Coroutine(Coroutine *cor, void *value);
extern void *Coroutine_Run(Coroutine_Start start, void *value);
extern void Coroutine_Delete(Coroutine *cor);
extern void Coroutine_Continue(Coroutine *cor, void *value, bool early);
extern void *Coroutine_Yield(void *value, Coroutine_YieldCallback on_yield, void *me);
extern void *Coroutine_GetValue(Coroutine *cor);
extern Coroutine *Coroutine_GetActive(void);
extern intptr_t Coroutine_GetStackHeadroom(void);
extern bool Coroutine_HasCoroutinesInFreePool(void);
extern void *Coroutine_GetCStackTop(void);
extern void *Coroutine_Chain(Coroutine_Start start, void *value);
extern bool Coroutine_IsStarted(void);
extern bool Coroutine_IsRunning(Coroutine *cor);
#endif