[6a3d2e7] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[75a17f1] | 7 | // coroutine -- |
---|
[6a3d2e7] | 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Mon Nov 28 12:27:26 2016 |
---|
[91c389a] | 11 | // Last Modified By : Peter A. Buhr |
---|
[8a97248] | 12 | // Last Modified On : Thu Feb 2 11:31:42 2023 |
---|
| 13 | // Update Count : 13 |
---|
[6a3d2e7] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[6a3d2e7] | 17 | |
---|
[91c389a] | 18 | #include <assert.h> |
---|
[6a3d2e7] | 19 | #include "invoke.h" |
---|
[1c01c58] | 20 | #include "../exception.hfa" |
---|
| 21 | |
---|
| 22 | //----------------------------------------------------------------------------- |
---|
| 23 | // Exception thrown from resume when a coroutine stack is cancelled. |
---|
[c715e5f] | 24 | forall(coroutine_t &) |
---|
| 25 | exception CoroutineCancelled { |
---|
[1c01c58] | 26 | coroutine_t * the_coroutine; |
---|
| 27 | exception_t * the_exception; |
---|
[c715e5f] | 28 | }; |
---|
[1c01c58] | 29 | |
---|
[fd54fef] | 30 | forall(T &) |
---|
[1c01c58] | 31 | void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src); |
---|
| 32 | |
---|
[fd54fef] | 33 | forall(T &) |
---|
[1c01c58] | 34 | const char * msg(CoroutineCancelled(T) *); |
---|
[6a3d2e7] | 35 | |
---|
| 36 | //----------------------------------------------------------------------------- |
---|
| 37 | // Coroutine trait |
---|
| 38 | // Anything that implements this trait can be resumed. |
---|
| 39 | // Anything that is resumed is a coroutine. |
---|
[8a97248] | 40 | forall( T & | IS_RESUMPTION_EXCEPTION(CoroutineCancelled(T)) ) |
---|
| 41 | trait is_coroutine { |
---|
[1c01c58] | 42 | void main(T & this); |
---|
[e84ab3d] | 43 | coroutine$ * get_coroutine(T & this); |
---|
[6a3d2e7] | 44 | }; |
---|
| 45 | |
---|
[e84ab3d] | 46 | #define DECL_COROUTINE(X) static inline coroutine$* get_coroutine(X& this) { return &this.__cor; } void main(X& this) |
---|
[c84e80a] | 47 | |
---|
[6a3d2e7] | 48 | //----------------------------------------------------------------------------- |
---|
| 49 | // Ctors and dtors |
---|
[de6319f] | 50 | // void ?{}( coStack_t & this ); |
---|
| 51 | // void ^?{}( coStack_t & this ); |
---|
| 52 | |
---|
[e84ab3d] | 53 | void ?{}( coroutine$ & this, const char name[], void * storage, size_t storageSize ); |
---|
| 54 | void ^?{}( coroutine$ & this ); |
---|
[de6319f] | 55 | |
---|
[e84ab3d] | 56 | static inline void ?{}( coroutine$ & this) { this{ "Anonymous Coroutine", 0p, 0 }; } |
---|
| 57 | static inline void ?{}( coroutine$ & this, size_t stackSize) { this{ "Anonymous Coroutine", 0p, stackSize }; } |
---|
| 58 | static inline void ?{}( coroutine$ & this, void * storage, size_t storageSize ) { this{ "Anonymous Coroutine", storage, storageSize }; } |
---|
| 59 | static inline void ?{}( coroutine$ & this, const char name[]) { this{ name, 0p, 0 }; } |
---|
| 60 | static inline void ?{}( coroutine$ & this, const char name[], size_t stackSize ) { this{ name, 0p, stackSize }; } |
---|
[6a3d2e7] | 61 | |
---|
| 62 | //----------------------------------------------------------------------------- |
---|
| 63 | // Public coroutine API |
---|
[c3b9d639] | 64 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); }) |
---|
[83a071f9] | 65 | void prime(T & cor); |
---|
[6a3d2e7] | 66 | |
---|
[e84ab3d] | 67 | static inline struct coroutine$ * active_coroutine() { return active_thread()->curr_cor; } |
---|
[d4e68a6] | 68 | |
---|
[6a3d2e7] | 69 | //----------------------------------------------------------------------------- |
---|
| 70 | // PRIVATE exposed because of inline |
---|
| 71 | |
---|
| 72 | // Start coroutine routines |
---|
| 73 | extern "C" { |
---|
[c7a900a] | 74 | void __cfactx_invoke_coroutine(void (*main)(void *), void * this); |
---|
[6a3d2e7] | 75 | |
---|
[fd54fef] | 76 | forall(T &) |
---|
[e84ab3d] | 77 | void __cfactx_start(void (*main)(T &), struct coroutine$ * cor, T & this, void (*invoke)(void (*main)(void *), void *)); |
---|
[3c06bba] | 78 | |
---|
[e84ab3d] | 79 | extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct coroutine$ *) __attribute__ ((__noreturn__)); |
---|
[3c06bba] | 80 | |
---|
[c7a900a] | 81 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch"); |
---|
[6a3d2e7] | 82 | } |
---|
| 83 | |
---|
| 84 | // Private wrappers for context switch and stack creation |
---|
[3c06bba] | 85 | // Wrapper for co |
---|
[e84ab3d] | 86 | static inline void $ctx_switch( coroutine$ * src, coroutine$ * dst ) __attribute__((nonnull (1, 2))) { |
---|
[3c06bba] | 87 | // set state of current coroutine to inactive |
---|
[ae7be7a] | 88 | src->state = src->state == Halted ? Halted : Blocked; |
---|
[3c06bba] | 89 | |
---|
[ab5baab] | 90 | // get the active thread once |
---|
[e84ab3d] | 91 | thread$ * athrd = active_thread(); |
---|
[ab5baab] | 92 | |
---|
| 93 | // Mark the coroutine |
---|
| 94 | /* paranoid */ verify( !athrd->corctx_flag ); |
---|
| 95 | athrd->corctx_flag = true; |
---|
| 96 | |
---|
[3c06bba] | 97 | // set new coroutine that task is executing |
---|
[ab5baab] | 98 | athrd->curr_cor = dst; |
---|
[3c06bba] | 99 | |
---|
| 100 | // context switch to specified coroutine |
---|
[ab5baab] | 101 | /* paranoid */ verify( dst->context.SP ); |
---|
[c7a900a] | 102 | __cfactx_switch( &src->context, &dst->context ); |
---|
| 103 | // when __cfactx_switch returns we are back in the src coroutine |
---|
[3c06bba] | 104 | |
---|
[ab5baab] | 105 | /* paranoid */ verify( athrd->corctx_flag ); |
---|
| 106 | athrd->corctx_flag = false; |
---|
| 107 | |
---|
[3c06bba] | 108 | // set state of new coroutine to active |
---|
| 109 | src->state = Active; |
---|
| 110 | |
---|
[121be3e] | 111 | if( unlikely(src->cancellation != 0p) ) { |
---|
[c7a900a] | 112 | __cfactx_coroutine_unwind(src->cancellation, src); |
---|
[3c06bba] | 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
[bfcf6b9] | 116 | extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */); |
---|
[6a3d2e7] | 117 | |
---|
| 118 | // Suspend implementation inlined for performance |
---|
[427854b] | 119 | extern "C" { |
---|
| 120 | static inline void __cfactx_suspend(void) { |
---|
| 121 | // optimization : read TLS once and reuse it |
---|
| 122 | // Safety note: this is preemption safe since if |
---|
| 123 | // preemption occurs after this line, the pointer |
---|
| 124 | // will also migrate which means this value will |
---|
| 125 | // stay in syn with the TLS |
---|
[e84ab3d] | 126 | coroutine$ * src = active_coroutine(); |
---|
[427854b] | 127 | |
---|
| 128 | assertf( src->last != 0, |
---|
| 129 | "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n" |
---|
| 130 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", |
---|
| 131 | src->name, src ); |
---|
| 132 | assertf( src->last->state != Halted, |
---|
| 133 | "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n" |
---|
| 134 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
| 135 | src->name, src, src->last->name, src->last ); |
---|
[6a3d2e7] | 136 | |
---|
[427854b] | 137 | $ctx_switch( src, src->last ); |
---|
| 138 | } |
---|
[6a3d2e7] | 139 | } |
---|
| 140 | |
---|
[fd54fef] | 141 | forall(T & | is_coroutine(T)) |
---|
[b583113] | 142 | void __cfaehm_cancelled_coroutine( |
---|
[c3b9d639] | 143 | T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled(T)) ); |
---|
[1c01c58] | 144 | |
---|
[6a3d2e7] | 145 | // Resume implementation inlined for performance |
---|
[c3b9d639] | 146 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); }) |
---|
[aa00626] | 147 | static inline T & resume(T & cor) { |
---|
[14a61b5] | 148 | // optimization : read TLS once and reuse it |
---|
| 149 | // Safety note: this is preemption safe since if |
---|
| 150 | // preemption occurs after this line, the pointer |
---|
| 151 | // will also migrate which means this value will |
---|
| 152 | // stay in syn with the TLS |
---|
[e84ab3d] | 153 | coroutine$ * src = active_coroutine(); |
---|
| 154 | coroutine$ * dst = get_coroutine(cor); |
---|
[6a3d2e7] | 155 | |
---|
[121be3e] | 156 | if( unlikely(dst->context.SP == 0p) ) { |
---|
[eaf269d] | 157 | __stack_prepare(&dst->stack, DEFAULT_STACK_SIZE); |
---|
[c7a900a] | 158 | __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine); |
---|
[6a3d2e7] | 159 | } |
---|
| 160 | |
---|
[4aa2fb2] | 161 | // not resuming self ? |
---|
[6a3d2e7] | 162 | if ( src != dst ) { |
---|
[ee897e4b] | 163 | assertf( dst->state != Halted , |
---|
[6a3d2e7] | 164 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" |
---|
| 165 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
| 166 | src->name, src, dst->name, dst ); |
---|
| 167 | |
---|
[4aa2fb2] | 168 | // set last resumer |
---|
[6a3d2e7] | 169 | dst->last = src; |
---|
[b462670] | 170 | dst->starter = dst->starter ? dst->starter : src; |
---|
[14a61b5] | 171 | } |
---|
[6a3d2e7] | 172 | |
---|
[4aa2fb2] | 173 | // always done for performance testing |
---|
[ac2b598] | 174 | $ctx_switch( src, dst ); |
---|
[1c01c58] | 175 | if ( unlikely(dst->cancellation) ) { |
---|
[b583113] | 176 | __cfaehm_cancelled_coroutine( cor, dst, _default_vtable ); |
---|
[1c01c58] | 177 | } |
---|
[aa00626] | 178 | |
---|
| 179 | return cor; |
---|
[6a3d2e7] | 180 | } |
---|
| 181 | |
---|
[e84ab3d] | 182 | static inline void resume( coroutine$ * dst ) __attribute__((nonnull (1))) { |
---|
[14a61b5] | 183 | // optimization : read TLS once and reuse it |
---|
| 184 | // Safety note: this is preemption safe since if |
---|
| 185 | // preemption occurs after this line, the pointer |
---|
| 186 | // will also migrate which means this value will |
---|
| 187 | // stay in syn with the TLS |
---|
[e84ab3d] | 188 | coroutine$ * src = active_coroutine(); |
---|
[77e6fcb] | 189 | |
---|
[4aa2fb2] | 190 | // not resuming self ? |
---|
[77e6fcb] | 191 | if ( src != dst ) { |
---|
[ee897e4b] | 192 | assertf( dst->state != Halted , |
---|
[77e6fcb] | 193 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" |
---|
| 194 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
| 195 | src->name, src, dst->name, dst ); |
---|
| 196 | |
---|
[4aa2fb2] | 197 | // set last resumer |
---|
[77e6fcb] | 198 | dst->last = src; |
---|
[14a61b5] | 199 | } |
---|
[77e6fcb] | 200 | |
---|
[4aa2fb2] | 201 | // always done for performance testing |
---|
[ac2b598] | 202 | $ctx_switch( src, dst ); |
---|
[77e6fcb] | 203 | } |
---|
| 204 | |
---|
[6a3d2e7] | 205 | // Local Variables: // |
---|
| 206 | // mode: c // |
---|
| 207 | // tab-width: 4 // |
---|
| 208 | // End: // |
---|