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