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