[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 | |
---|
[2fe64ba] | 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 | |
---|
[1c01c58] | 37 | //----------------------------------------------------------------------------- |
---|
| 38 | // Exception thrown from resume when a coroutine stack is cancelled. |
---|
[c715e5f] | 39 | forall(coroutine_t &) |
---|
| 40 | exception CoroutineCancelled { |
---|
[1c01c58] | 41 | coroutine_t * the_coroutine; |
---|
| 42 | exception_t * the_exception; |
---|
[c715e5f] | 43 | }; |
---|
[1c01c58] | 44 | |
---|
[fd54fef] | 45 | forall(T &) |
---|
[1c01c58] | 46 | void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src); |
---|
| 47 | |
---|
[fd54fef] | 48 | forall(T &) |
---|
[1c01c58] | 49 | const char * msg(CoroutineCancelled(T) *); |
---|
[6a3d2e7] | 50 | |
---|
| 51 | //----------------------------------------------------------------------------- |
---|
| 52 | // Coroutine trait |
---|
| 53 | // Anything that implements this trait can be resumed. |
---|
| 54 | // Anything that is resumed is a coroutine. |
---|
[8a97248] | 55 | forall( T & | IS_RESUMPTION_EXCEPTION(CoroutineCancelled(T)) ) |
---|
| 56 | trait is_coroutine { |
---|
[1c01c58] | 57 | void main(T & this); |
---|
[e84ab3d] | 58 | coroutine$ * get_coroutine(T & this); |
---|
[6a3d2e7] | 59 | }; |
---|
| 60 | |
---|
[e84ab3d] | 61 | #define DECL_COROUTINE(X) static inline coroutine$* get_coroutine(X& this) { return &this.__cor; } void main(X& this) |
---|
[c84e80a] | 62 | |
---|
[6a3d2e7] | 63 | //----------------------------------------------------------------------------- |
---|
| 64 | // Ctors and dtors |
---|
[de6319f] | 65 | // void ?{}( coStack_t & this ); |
---|
| 66 | // void ^?{}( coStack_t & this ); |
---|
| 67 | |
---|
[e84ab3d] | 68 | void ?{}( coroutine$ & this, const char name[], void * storage, size_t storageSize ); |
---|
| 69 | void ^?{}( coroutine$ & this ); |
---|
[de6319f] | 70 | |
---|
[e84ab3d] | 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 }; } |
---|
[6a3d2e7] | 76 | |
---|
| 77 | //----------------------------------------------------------------------------- |
---|
| 78 | // Public coroutine API |
---|
[c3b9d639] | 79 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); }) |
---|
[83a071f9] | 80 | void prime(T & cor); |
---|
[6a3d2e7] | 81 | |
---|
[e84ab3d] | 82 | static inline struct coroutine$ * active_coroutine() { return active_thread()->curr_cor; } |
---|
[d4e68a6] | 83 | |
---|
[6a3d2e7] | 84 | //----------------------------------------------------------------------------- |
---|
| 85 | // PRIVATE exposed because of inline |
---|
| 86 | |
---|
| 87 | // Start coroutine routines |
---|
| 88 | extern "C" { |
---|
[c7a900a] | 89 | void __cfactx_invoke_coroutine(void (*main)(void *), void * this); |
---|
[6a3d2e7] | 90 | |
---|
[fd54fef] | 91 | forall(T &) |
---|
[e84ab3d] | 92 | void __cfactx_start(void (*main)(T &), struct coroutine$ * cor, T & this, void (*invoke)(void (*main)(void *), void *)); |
---|
[3c06bba] | 93 | |
---|
[e84ab3d] | 94 | extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct coroutine$ *) __attribute__ ((__noreturn__)); |
---|
[3c06bba] | 95 | |
---|
[c7a900a] | 96 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch"); |
---|
[6a3d2e7] | 97 | } |
---|
| 98 | |
---|
| 99 | // Private wrappers for context switch and stack creation |
---|
[3c06bba] | 100 | // Wrapper for co |
---|
[e84ab3d] | 101 | static inline void $ctx_switch( coroutine$ * src, coroutine$ * dst ) __attribute__((nonnull (1, 2))) { |
---|
[3c06bba] | 102 | // set state of current coroutine to inactive |
---|
[ae7be7a] | 103 | src->state = src->state == Halted ? Halted : Blocked; |
---|
[3c06bba] | 104 | |
---|
[ab5baab] | 105 | // get the active thread once |
---|
[e84ab3d] | 106 | thread$ * athrd = active_thread(); |
---|
[ab5baab] | 107 | |
---|
| 108 | // Mark the coroutine |
---|
| 109 | /* paranoid */ verify( !athrd->corctx_flag ); |
---|
| 110 | athrd->corctx_flag = true; |
---|
| 111 | |
---|
[3c06bba] | 112 | // set new coroutine that task is executing |
---|
[ab5baab] | 113 | athrd->curr_cor = dst; |
---|
[3c06bba] | 114 | |
---|
| 115 | // context switch to specified coroutine |
---|
[ab5baab] | 116 | /* paranoid */ verify( dst->context.SP ); |
---|
[c7a900a] | 117 | __cfactx_switch( &src->context, &dst->context ); |
---|
| 118 | // when __cfactx_switch returns we are back in the src coroutine |
---|
[3c06bba] | 119 | |
---|
[ab5baab] | 120 | /* paranoid */ verify( athrd->corctx_flag ); |
---|
| 121 | athrd->corctx_flag = false; |
---|
| 122 | |
---|
[3c06bba] | 123 | // set state of new coroutine to active |
---|
| 124 | src->state = Active; |
---|
| 125 | |
---|
[121be3e] | 126 | if( unlikely(src->cancellation != 0p) ) { |
---|
[c7a900a] | 127 | __cfactx_coroutine_unwind(src->cancellation, src); |
---|
[3c06bba] | 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
[bfcf6b9] | 131 | extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */); |
---|
[6a3d2e7] | 132 | |
---|
| 133 | // Suspend implementation inlined for performance |
---|
[427854b] | 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 |
---|
[e84ab3d] | 141 | coroutine$ * src = active_coroutine(); |
---|
[427854b] | 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 ); |
---|
[6a3d2e7] | 151 | |
---|
[427854b] | 152 | $ctx_switch( src, src->last ); |
---|
| 153 | } |
---|
[6a3d2e7] | 154 | } |
---|
| 155 | |
---|
[fd54fef] | 156 | forall(T & | is_coroutine(T)) |
---|
[b583113] | 157 | void __cfaehm_cancelled_coroutine( |
---|
[c3b9d639] | 158 | T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled(T)) ); |
---|
[1c01c58] | 159 | |
---|
[6a3d2e7] | 160 | // Resume implementation inlined for performance |
---|
[c3b9d639] | 161 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); }) |
---|
[aa00626] | 162 | static inline T & resume(T & cor) { |
---|
[14a61b5] | 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 |
---|
[e84ab3d] | 168 | coroutine$ * src = active_coroutine(); |
---|
| 169 | coroutine$ * dst = get_coroutine(cor); |
---|
[6a3d2e7] | 170 | |
---|
[121be3e] | 171 | if( unlikely(dst->context.SP == 0p) ) { |
---|
[eaf269d] | 172 | __stack_prepare(&dst->stack, DEFAULT_STACK_SIZE); |
---|
[c7a900a] | 173 | __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine); |
---|
[6a3d2e7] | 174 | } |
---|
| 175 | |
---|
[4aa2fb2] | 176 | // not resuming self ? |
---|
[6a3d2e7] | 177 | if ( src != dst ) { |
---|
[ee897e4b] | 178 | assertf( dst->state != Halted , |
---|
[6a3d2e7] | 179 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" |
---|
| 180 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
| 181 | src->name, src, dst->name, dst ); |
---|
| 182 | |
---|
[4aa2fb2] | 183 | // set last resumer |
---|
[6a3d2e7] | 184 | dst->last = src; |
---|
[b462670] | 185 | dst->starter = dst->starter ? dst->starter : src; |
---|
[14a61b5] | 186 | } |
---|
[6a3d2e7] | 187 | |
---|
[4aa2fb2] | 188 | // always done for performance testing |
---|
[ac2b598] | 189 | $ctx_switch( src, dst ); |
---|
[1c01c58] | 190 | if ( unlikely(dst->cancellation) ) { |
---|
[b583113] | 191 | __cfaehm_cancelled_coroutine( cor, dst, _default_vtable ); |
---|
[1c01c58] | 192 | } |
---|
[aa00626] | 193 | |
---|
| 194 | return cor; |
---|
[6a3d2e7] | 195 | } |
---|
| 196 | |
---|
[e84ab3d] | 197 | static inline void resume( coroutine$ * dst ) __attribute__((nonnull (1))) { |
---|
[14a61b5] | 198 | // optimization : read TLS once and reuse it |
---|
| 199 | // Safety note: this is preemption safe since if |
---|
| 200 | // preemption occurs after this line, the pointer |
---|
| 201 | // will also migrate which means this value will |
---|
| 202 | // stay in syn with the TLS |
---|
[e84ab3d] | 203 | coroutine$ * src = active_coroutine(); |
---|
[77e6fcb] | 204 | |
---|
[4aa2fb2] | 205 | // not resuming self ? |
---|
[77e6fcb] | 206 | if ( src != dst ) { |
---|
[ee897e4b] | 207 | assertf( dst->state != Halted , |
---|
[77e6fcb] | 208 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" |
---|
| 209 | "Possible cause is terminated coroutine's main routine has already returned.", |
---|
| 210 | src->name, src, dst->name, dst ); |
---|
| 211 | |
---|
[4aa2fb2] | 212 | // set last resumer |
---|
[77e6fcb] | 213 | dst->last = src; |
---|
[14a61b5] | 214 | } |
---|
[77e6fcb] | 215 | |
---|
[4aa2fb2] | 216 | // always done for performance testing |
---|
[ac2b598] | 217 | $ctx_switch( src, dst ); |
---|
[77e6fcb] | 218 | } |
---|
| 219 | |
---|
[c3e510b] | 220 | // non local ehm and coroutine utility routines |
---|
[2fe64ba] | 221 | forall(T & | is_coroutine(T)) { |
---|
| 222 | void enable_ehm( T & cor ); |
---|
| 223 | void disable_ehm( T & cor ); |
---|
| 224 | bool poll( T & cor ); |
---|
| 225 | bool checked_poll( T & cor ); |
---|
[c3e510b] | 226 | coroutine$ * resumer( T & cor ); |
---|
[2fe64ba] | 227 | } |
---|
| 228 | |
---|
| 229 | // trait for exceptions able to be resumed at another coroutine |
---|
| 230 | forall(exceptT &, T & | is_coroutine(T)) |
---|
| 231 | trait ehm_resume_at { void $throwResume(exceptT &); }; |
---|
| 232 | |
---|
[c3e510b] | 233 | // general resumeAt |
---|
[2fe64ba] | 234 | forall(exceptT &, T & | ehm_resume_at( exceptT, T )) |
---|
| 235 | void resumeAt( T & receiver, exceptT & ex ); |
---|
| 236 | |
---|
[c3e510b] | 237 | // resumeAt for underlying coroutine$ type |
---|
| 238 | forall(exceptT & | { void $throwResume(exceptT &); }) |
---|
| 239 | void resumeAt( coroutine$ * receiver, exceptT & ex ); |
---|
| 240 | |
---|
[6a3d2e7] | 241 | // Local Variables: // |
---|
| 242 | // mode: c // |
---|
| 243 | // tab-width: 4 // |
---|
| 244 | // End: // |
---|