| 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 : Tue Feb  4 12:29:26 2020 | 
|---|
| 13 | // Update Count     : 11 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <assert.h> | 
|---|
| 19 | #include "invoke.h" | 
|---|
| 20 | #include "../exception.hfa" | 
|---|
| 21 |  | 
|---|
| 22 | //----------------------------------------------------------------------------- | 
|---|
| 23 | // Exception thrown from resume when a coroutine stack is cancelled. | 
|---|
| 24 | EHM_FORALL_EXCEPTION(CoroutineCancelled, (coroutine_t &), (coroutine_t)) ( | 
|---|
| 25 | coroutine_t * the_coroutine; | 
|---|
| 26 | exception_t * the_exception; | 
|---|
| 27 | ); | 
|---|
| 28 |  | 
|---|
| 29 | forall(T &) | 
|---|
| 30 | void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src); | 
|---|
| 31 |  | 
|---|
| 32 | forall(T &) | 
|---|
| 33 | const char * msg(CoroutineCancelled(T) *); | 
|---|
| 34 |  | 
|---|
| 35 | //----------------------------------------------------------------------------- | 
|---|
| 36 | // Coroutine trait | 
|---|
| 37 | // Anything that implements this trait can be resumed. | 
|---|
| 38 | // Anything that is resumed is a coroutine. | 
|---|
| 39 | trait is_coroutine(T & | IS_RESUMPTION_EXCEPTION(CoroutineCancelled, (T))) { | 
|---|
| 40 | void main(T & this); | 
|---|
| 41 | coroutine$ * get_coroutine(T & this); | 
|---|
| 42 | }; | 
|---|
| 43 |  | 
|---|
| 44 | #define DECL_COROUTINE(X) static inline coroutine$* get_coroutine(X& this) { return &this.__cor; } void main(X& this) | 
|---|
| 45 |  | 
|---|
| 46 | //----------------------------------------------------------------------------- | 
|---|
| 47 | // Ctors and dtors | 
|---|
| 48 | // void ?{}( coStack_t & this ); | 
|---|
| 49 | // void ^?{}( coStack_t & this ); | 
|---|
| 50 |  | 
|---|
| 51 | void  ?{}( coroutine$ & this, const char name[], void * storage, size_t storageSize ); | 
|---|
| 52 | void ^?{}( coroutine$ & this ); | 
|---|
| 53 |  | 
|---|
| 54 | static inline void ?{}( coroutine$ & this)                                       { this{ "Anonymous Coroutine", 0p, 0 }; } | 
|---|
| 55 | static inline void ?{}( coroutine$ & this, size_t stackSize)                     { this{ "Anonymous Coroutine", 0p, stackSize }; } | 
|---|
| 56 | static inline void ?{}( coroutine$ & this, void * storage, size_t storageSize )  { this{ "Anonymous Coroutine", storage, storageSize }; } | 
|---|
| 57 | static inline void ?{}( coroutine$ & this, const char name[])                    { this{ name, 0p, 0 }; } | 
|---|
| 58 | static inline void ?{}( coroutine$ & this, const char name[], size_t stackSize ) { this{ name, 0p, stackSize }; } | 
|---|
| 59 |  | 
|---|
| 60 | //----------------------------------------------------------------------------- | 
|---|
| 61 | // Public coroutine API | 
|---|
| 62 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled, (T)); }) | 
|---|
| 63 | void prime(T & cor); | 
|---|
| 64 |  | 
|---|
| 65 | static inline struct coroutine$ * active_coroutine() { return active_thread()->curr_cor; } | 
|---|
| 66 |  | 
|---|
| 67 | //----------------------------------------------------------------------------- | 
|---|
| 68 | // PRIVATE exposed because of inline | 
|---|
| 69 |  | 
|---|
| 70 | // Start coroutine routines | 
|---|
| 71 | extern "C" { | 
|---|
| 72 | void __cfactx_invoke_coroutine(void (*main)(void *), void * this); | 
|---|
| 73 |  | 
|---|
| 74 | forall(T &) | 
|---|
| 75 | void __cfactx_start(void (*main)(T &), struct coroutine$ * cor, T & this, void (*invoke)(void (*main)(void *), void *)); | 
|---|
| 76 |  | 
|---|
| 77 | extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct coroutine$ *) __attribute__ ((__noreturn__)); | 
|---|
| 78 |  | 
|---|
| 79 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch"); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | // Private wrappers for context switch and stack creation | 
|---|
| 83 | // Wrapper for co | 
|---|
| 84 | static inline void $ctx_switch( coroutine$ * src, coroutine$ * dst ) __attribute__((nonnull (1, 2))) { | 
|---|
| 85 | // set state of current coroutine to inactive | 
|---|
| 86 | src->state = src->state == Halted ? Halted : Blocked; | 
|---|
| 87 |  | 
|---|
| 88 | // get the active thread once | 
|---|
| 89 | thread$ * athrd = active_thread(); | 
|---|
| 90 |  | 
|---|
| 91 | // Mark the coroutine | 
|---|
| 92 | /* paranoid */ verify( !athrd->corctx_flag ); | 
|---|
| 93 | athrd->corctx_flag = true; | 
|---|
| 94 |  | 
|---|
| 95 | // set new coroutine that task is executing | 
|---|
| 96 | athrd->curr_cor = dst; | 
|---|
| 97 |  | 
|---|
| 98 | // context switch to specified coroutine | 
|---|
| 99 | /* paranoid */ verify( dst->context.SP ); | 
|---|
| 100 | __cfactx_switch( &src->context, &dst->context ); | 
|---|
| 101 | // when __cfactx_switch returns we are back in the src coroutine | 
|---|
| 102 |  | 
|---|
| 103 | /* paranoid */ verify( athrd->corctx_flag ); | 
|---|
| 104 | athrd->corctx_flag = false; | 
|---|
| 105 |  | 
|---|
| 106 | // set state of new coroutine to active | 
|---|
| 107 | src->state = Active; | 
|---|
| 108 |  | 
|---|
| 109 | if( unlikely(src->cancellation != 0p) ) { | 
|---|
| 110 | __cfactx_coroutine_unwind(src->cancellation, src); | 
|---|
| 111 | } | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */); | 
|---|
| 115 | extern void __stack_clean  ( __stack_info_t * this ); | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 | // Suspend implementation inlined for performance | 
|---|
| 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 | 
|---|
| 126 | coroutine$ * src = active_coroutine(); | 
|---|
| 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 ); | 
|---|
| 136 |  | 
|---|
| 137 | $ctx_switch( src, src->last ); | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | forall(T & | is_coroutine(T)) | 
|---|
| 142 | void __cfaehm_cancelled_coroutine( | 
|---|
| 143 | T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled, (T)) ); | 
|---|
| 144 |  | 
|---|
| 145 | // Resume implementation inlined for performance | 
|---|
| 146 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled, (T)); }) | 
|---|
| 147 | static inline T & resume(T & cor) { | 
|---|
| 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 | 
|---|
| 153 | coroutine$ * src = active_coroutine(); | 
|---|
| 154 | coroutine$ * dst = get_coroutine(cor); | 
|---|
| 155 |  | 
|---|
| 156 | if( unlikely(dst->context.SP == 0p) ) { | 
|---|
| 157 | __stack_prepare(&dst->stack, 65000); | 
|---|
| 158 | __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | // not resuming self ? | 
|---|
| 162 | if ( src != dst ) { | 
|---|
| 163 | assertf( dst->state != Halted , | 
|---|
| 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 |  | 
|---|
| 168 | // set last resumer | 
|---|
| 169 | dst->last = src; | 
|---|
| 170 | dst->starter = dst->starter ? dst->starter : src; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | // always done for performance testing | 
|---|
| 174 | $ctx_switch( src, dst ); | 
|---|
| 175 | if ( unlikely(dst->cancellation) ) { | 
|---|
| 176 | __cfaehm_cancelled_coroutine( cor, dst, _default_vtable ); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | return cor; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | static inline void resume( coroutine$ * dst ) __attribute__((nonnull (1))) { | 
|---|
| 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 | 
|---|
| 188 | coroutine$ * src = active_coroutine(); | 
|---|
| 189 |  | 
|---|
| 190 | // not resuming self ? | 
|---|
| 191 | if ( src != dst ) { | 
|---|
| 192 | assertf( dst->state != Halted , | 
|---|
| 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 |  | 
|---|
| 197 | // set last resumer | 
|---|
| 198 | dst->last = src; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | // always done for performance testing | 
|---|
| 202 | $ctx_switch( src, dst ); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | // Local Variables: // | 
|---|
| 206 | // mode: c // | 
|---|
| 207 | // tab-width: 4 // | 
|---|
| 208 | // End: // | 
|---|