| 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 | // | 
|---|
| 7 | // coroutine -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Thierry Delisle | 
|---|
| 10 | // Created On       : Mon Nov 28 12:27:26 2016 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Fri Apr 25 06:52:04 2025 | 
|---|
| 13 | // Update Count     : 15 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <assert.h> | 
|---|
| 19 | #include "invoke.h" | 
|---|
| 20 | #include "../exception.hfa" | 
|---|
| 21 |  | 
|---|
| 22 | //----------------------------------------------------------------------------- | 
|---|
| 23 | // Type used to store and queue nonlocal exceptions on coroutines | 
|---|
| 24 | struct nonlocal_exception { | 
|---|
| 25 | exception_t * the_exception; | 
|---|
| 26 | nonlocal_exception * next; | 
|---|
| 27 | }; | 
|---|
| 28 |  | 
|---|
| 29 | static inline void ?{}( nonlocal_exception & this, exception_t * ex ) with(this) { | 
|---|
| 30 | the_exception = ex; | 
|---|
| 31 | this.next = 0p; | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | static inline nonlocal_exception *& get_next( nonlocal_exception & this ) __attribute__((const)) { | 
|---|
| 35 | return this.next; | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | //----------------------------------------------------------------------------- | 
|---|
| 39 | // Exception thrown from resume when a coroutine stack is cancelled. | 
|---|
| 40 | forall(coroutine_t &) | 
|---|
| 41 | exception CoroutineCancelled { | 
|---|
| 42 | coroutine_t * the_coroutine; | 
|---|
| 43 | exception_t * the_exception; | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | forall(T &) | 
|---|
| 47 | void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src); | 
|---|
| 48 |  | 
|---|
| 49 | forall(T &) | 
|---|
| 50 | const char * msg(CoroutineCancelled(T) *); | 
|---|
| 51 |  | 
|---|
| 52 | //----------------------------------------------------------------------------- | 
|---|
| 53 | // Coroutine trait | 
|---|
| 54 | // Anything that implements this trait can be resumed. | 
|---|
| 55 | // Anything that is resumed is a coroutine. | 
|---|
| 56 | forall( T & | IS_RESUMPTION_EXCEPTION(CoroutineCancelled(T)) ) | 
|---|
| 57 | trait is_coroutine { | 
|---|
| 58 | void main(T & this); | 
|---|
| 59 | coroutine$ * get_coroutine(T & this); | 
|---|
| 60 | }; | 
|---|
| 61 |  | 
|---|
| 62 | #define DECL_COROUTINE(X) static inline coroutine$* get_coroutine(X& this) { return &this.__cor; } void main(X& this) | 
|---|
| 63 |  | 
|---|
| 64 | //----------------------------------------------------------------------------- | 
|---|
| 65 | // Ctors and dtors | 
|---|
| 66 | // void ?{}( coStack_t & this ); | 
|---|
| 67 | // void ^?{}( coStack_t & this ); | 
|---|
| 68 |  | 
|---|
| 69 | void ?{}( coroutine$ & this, const char name[], void * storage, size_t storageSize ); | 
|---|
| 70 | void ^?{}( coroutine$ & this ); | 
|---|
| 71 |  | 
|---|
| 72 | static inline void ?{}( coroutine$ & this)                                       { this{ "Anonymous Coroutine", 0p, 0 }; } | 
|---|
| 73 | static inline void ?{}( coroutine$ & this, size_t stackSize)                     { this{ "Anonymous Coroutine", 0p, stackSize }; } | 
|---|
| 74 | static inline void ?{}( coroutine$ & this, void * storage, size_t storageSize )  { this{ "Anonymous Coroutine", storage, storageSize }; } | 
|---|
| 75 | static inline void ?{}( coroutine$ & this, const char name[])                    { this{ name, 0p, 0 }; } | 
|---|
| 76 | static inline void ?{}( coroutine$ & this, const char name[], size_t stackSize ) { this{ name, 0p, stackSize }; } | 
|---|
| 77 |  | 
|---|
| 78 | //----------------------------------------------------------------------------- | 
|---|
| 79 | // Public coroutine API | 
|---|
| 80 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); }) | 
|---|
| 81 | void prime(T & cor); | 
|---|
| 82 |  | 
|---|
| 83 | static inline struct coroutine$ * active_coroutine() { return active_thread()->curr_cor; } | 
|---|
| 84 |  | 
|---|
| 85 | //----------------------------------------------------------------------------- | 
|---|
| 86 | // PRIVATE exposed because of inline | 
|---|
| 87 |  | 
|---|
| 88 | // Start coroutine routines | 
|---|
| 89 | extern "C" { | 
|---|
| 90 | void __cfactx_invoke_coroutine(void (*main)(void *), void * this); | 
|---|
| 91 |  | 
|---|
| 92 | forall(T &) | 
|---|
| 93 | void __cfactx_start(void (*main)(T &), struct coroutine$ * cor, T & this, void (*invoke)(void (*main)(void *), void *)); | 
|---|
| 94 |  | 
|---|
| 95 | extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct coroutine$ *) __attribute__ ((__noreturn__)); | 
|---|
| 96 |  | 
|---|
| 97 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch"); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | // Private wrappers for context switch and stack creation | 
|---|
| 101 | // Wrapper for co | 
|---|
| 102 | static inline void $ctx_switch( coroutine$ * src, coroutine$ * dst ) __attribute__((nonnull (1, 2))) { | 
|---|
| 103 | // set state of current coroutine to inactive | 
|---|
| 104 | src->state = src->state == Halted ? Halted : Blocked; | 
|---|
| 105 |  | 
|---|
| 106 | // get the active thread once | 
|---|
| 107 | thread$ * athrd = active_thread(); | 
|---|
| 108 |  | 
|---|
| 109 | // Mark the coroutine | 
|---|
| 110 | /* paranoid */ verify( !athrd->corctx_flag ); | 
|---|
| 111 | athrd->corctx_flag = true; | 
|---|
| 112 |  | 
|---|
| 113 | // set new coroutine that task is executing | 
|---|
| 114 | athrd->curr_cor = dst; | 
|---|
| 115 |  | 
|---|
| 116 | // context switch to specified coroutine | 
|---|
| 117 | /* paranoid */ verify( dst->context.SP ); | 
|---|
| 118 | __cfactx_switch( &src->context, &dst->context ); | 
|---|
| 119 | // when __cfactx_switch returns we are back in the src coroutine | 
|---|
| 120 |  | 
|---|
| 121 | /* paranoid */ verify( athrd->corctx_flag ); | 
|---|
| 122 | athrd->corctx_flag = false; | 
|---|
| 123 |  | 
|---|
| 124 | // set state of new coroutine to active | 
|---|
| 125 | src->state = Active; | 
|---|
| 126 |  | 
|---|
| 127 | if( unlikely(src->cancellation != 0p && src->cancellation != 1p) ) { | 
|---|
| 128 | __cfactx_coroutine_unwind(src->cancellation, src); | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */); | 
|---|
| 133 |  | 
|---|
| 134 | // Suspend implementation inlined for performance | 
|---|
| 135 | extern "C" { | 
|---|
| 136 | static inline void __cfactx_suspend(void) { | 
|---|
| 137 | // optimization : read TLS once and reuse it | 
|---|
| 138 | // Safety note: this is preemption safe since if | 
|---|
| 139 | // preemption occurs after this line, the pointer | 
|---|
| 140 | // will also migrate which means this value will | 
|---|
| 141 | // stay in syn with the TLS | 
|---|
| 142 | coroutine$ * src = active_coroutine(); | 
|---|
| 143 |  | 
|---|
| 144 | assertf( src->last != 0, | 
|---|
| 145 | "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n" | 
|---|
| 146 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", | 
|---|
| 147 | src->name, src ); | 
|---|
| 148 | assertf( src->last->state != Halted, | 
|---|
| 149 | "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n" | 
|---|
| 150 | "Possible cause is terminated coroutine's main routine has already returned.", | 
|---|
| 151 | src->name, src, src->last->name, src->last ); | 
|---|
| 152 |  | 
|---|
| 153 | $ctx_switch( src, src->last ); | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | forall(T & | is_coroutine(T)) | 
|---|
| 158 | void __cfaehm_cancelled_coroutine( | 
|---|
| 159 | T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled(T)) ); | 
|---|
| 160 |  | 
|---|
| 161 | // Resume implementation inlined for performance | 
|---|
| 162 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); }) | 
|---|
| 163 | static inline T & resume(T & cor) { | 
|---|
| 164 | // optimization : read TLS once and reuse it | 
|---|
| 165 | // Safety note: this is preemption safe since if | 
|---|
| 166 | // preemption occurs after this line, the pointer | 
|---|
| 167 | // will also migrate which means this value will | 
|---|
| 168 | // stay in syn with the TLS | 
|---|
| 169 | coroutine$ * src = active_coroutine(); | 
|---|
| 170 | coroutine$ * dst = get_coroutine(cor); | 
|---|
| 171 |  | 
|---|
| 172 | // printf("FROM RES src: %p, dest: %p\n", src, dst); | 
|---|
| 173 |  | 
|---|
| 174 | if( unlikely(dst->context.SP == 0p) ) { | 
|---|
| 175 | __stack_prepare(&dst->stack, DEFAULT_STACK_SIZE); | 
|---|
| 176 | __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | // not resuming self ? | 
|---|
| 180 | if ( src != dst ) { | 
|---|
| 181 | assertf( dst->state != Halted , | 
|---|
| 182 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" | 
|---|
| 183 | "Possible cause is terminated coroutine's main routine has already returned.", | 
|---|
| 184 | src->name, src, dst->name, dst ); | 
|---|
| 185 |  | 
|---|
| 186 | // set last resumer | 
|---|
| 187 | dst->last = src; | 
|---|
| 188 | dst->starter = dst->starter ? dst->starter : src; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | // always done for performance testing | 
|---|
| 192 | $ctx_switch( src, dst ); | 
|---|
| 193 |  | 
|---|
| 194 | if ( unlikely(src->cancellation == 1p) ) { | 
|---|
| 195 | src->cancellation = 0p; | 
|---|
| 196 | // we know dst hasn't been deallocated | 
|---|
| 197 | __cfaehm_cancelled_coroutine( cor, dst, _default_vtable ); | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | return cor; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | static inline void resume( coroutine$ * dst ) __attribute__((nonnull (1))) { | 
|---|
| 204 | // optimization : read TLS once and reuse it | 
|---|
| 205 | // Safety note: this is preemption safe since if | 
|---|
| 206 | // preemption occurs after this line, the pointer | 
|---|
| 207 | // will also migrate which means this value will | 
|---|
| 208 | // stay in syn with the TLS | 
|---|
| 209 | coroutine$ * src = active_coroutine(); | 
|---|
| 210 |  | 
|---|
| 211 | // not resuming self ? | 
|---|
| 212 | if ( src != dst ) { | 
|---|
| 213 | assertf( dst->state != Halted , | 
|---|
| 214 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" | 
|---|
| 215 | "Possible cause is terminated coroutine's main routine has already returned.", | 
|---|
| 216 | src->name, src, dst->name, dst ); | 
|---|
| 217 |  | 
|---|
| 218 | // set last resumer | 
|---|
| 219 | dst->last = src; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | // always done for performance testing | 
|---|
| 223 | $ctx_switch( src, dst ); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | // non local ehm and coroutine utility routines | 
|---|
| 227 | void enable_ehm(); | 
|---|
| 228 | void disable_ehm(); | 
|---|
| 229 | bool poll( coroutine$ * cor ); | 
|---|
| 230 | bool poll(); | 
|---|
| 231 | bool checked_poll(); | 
|---|
| 232 | coroutine$ * resumer(); | 
|---|
| 233 | coroutine$ * first_resumer(); | 
|---|
| 234 |  | 
|---|
| 235 | forall(T & | is_coroutine(T)) { | 
|---|
| 236 | void enable_ehm( T & cor );         // enable checking non-local exceptions for cor via checked_poll | 
|---|
| 237 | void disable_ehm( T & cor );        // disable checking non-local exceptions for cor via checked_poll | 
|---|
| 238 | bool poll( T & cor ); | 
|---|
| 239 | bool checked_poll( T & cor );       // check for non-local exceptions while respecting enable/disable | 
|---|
| 240 | coroutine$ * resumer( T & cor ); | 
|---|
| 241 | coroutine$ * first_resumer( T & cor ); | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | // trait for exceptions able to be resumed at another coroutine | 
|---|
| 245 | forall(exceptT *, T & | is_coroutine(T)) | 
|---|
| 246 | trait ehm_resume_at { void $throwResume(exceptT &); }; | 
|---|
| 247 |  | 
|---|
| 248 | // general resumeAt | 
|---|
| 249 | forall(exceptT *, T & | ehm_resume_at( exceptT, T )) | 
|---|
| 250 | void resumeAt( T & receiver, exceptT & ex ); | 
|---|
| 251 |  | 
|---|
| 252 | // resumeAt for underlying coroutine$ type | 
|---|
| 253 | forall(exceptT * | { void $throwResume(exceptT &); }) | 
|---|
| 254 | void resumeAt( coroutine$ * receiver, exceptT & ex ); | 
|---|
| 255 |  | 
|---|
| 256 | // Local Variables: // | 
|---|
| 257 | // mode: c // | 
|---|
| 258 | // tab-width: 4 // | 
|---|
| 259 | // End: // | 
|---|