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