[0e76cf4f] | 1 | #include "threads" |
---|
[9129a84] | 2 | #include "assert" |
---|
[0e76cf4f] | 3 | |
---|
[9129a84] | 4 | #include <stddef.h> |
---|
| 5 | |
---|
| 6 | #include <fstream> |
---|
| 7 | |
---|
| 8 | static coroutine main_coroutine; |
---|
| 9 | static coroutine* current_coroutine = &main_coroutine; |
---|
| 10 | |
---|
| 11 | void ctxSwitchDirect(void* src, void* dst) { |
---|
| 12 | current_coroutine = dst; |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | coroutine* this_coroutine() { |
---|
| 16 | return current_coroutine; |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | void ?{}(coroutine* this) |
---|
| 20 | { |
---|
| 21 | this->last = NULL; |
---|
| 22 | this->name = "A Coroutine"; |
---|
| 23 | this->notHalted = true; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | void suspend() { |
---|
| 27 | coroutine* src = this_coroutine(); // optimization |
---|
| 28 | |
---|
| 29 | assertf( src->last == (coroutine*)0, |
---|
| 30 | "Attempt to suspend coroutine %.256s (%p) that has never been resumed.\n" |
---|
| 31 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", |
---|
| 32 | src->name, src ); |
---|
| 33 | assertf( src->last->notHalted, |
---|
| 34 | "Attempt by coroutine %.256s (%p) to suspend back to terminated coroutine %.256s (%p).\n" |
---|
| 35 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
| 36 | src->name, src, src->last->name, src->last ); |
---|
[0e76cf4f] | 37 | |
---|
[9129a84] | 38 | ctxSwitchDirect( src, src->last ); |
---|
[0e76cf4f] | 39 | } |
---|
| 40 | |
---|
| 41 | forall(dtype T | coroutine_t(T)) |
---|
| 42 | void resume(T* cor) { |
---|
[9129a84] | 43 | coroutine* src = this_coroutine(); // optimization |
---|
| 44 | coroutine* dst = this_coroutine(cor); |
---|
[0e76cf4f] | 45 | |
---|
[9129a84] | 46 | if ( src != dst ) { // not resuming self ? |
---|
| 47 | assertf( dst->notHalted , |
---|
| 48 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" |
---|
| 49 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
| 50 | src->name, src, dst->name, dst ); |
---|
| 51 | dst->last = src; // set last resumer |
---|
| 52 | } // if |
---|
| 53 | ctxSwitchDirect( src, dst ); // always done for performance testing |
---|
[0e76cf4f] | 54 | } |
---|