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