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