| 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 : Fri Mar 30 18:23:45 2018
|
|---|
| 13 | // Update Count : 8
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 |
|
|---|
| 18 | #include <assert.h>
|
|---|
| 19 | #include "invoke.h"
|
|---|
| 20 |
|
|---|
| 21 | //-----------------------------------------------------------------------------
|
|---|
| 22 | // Coroutine trait
|
|---|
| 23 | // Anything that implements this trait can be resumed.
|
|---|
| 24 | // Anything that is resumed is a coroutine.
|
|---|
| 25 | trait is_coroutine(dtype T) {
|
|---|
| 26 | void main(T & this);
|
|---|
| 27 | coroutine_desc * get_coroutine(T & this);
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | #define DECL_COROUTINE(X) static inline coroutine_desc* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
|
|---|
| 31 |
|
|---|
| 32 | //-----------------------------------------------------------------------------
|
|---|
| 33 | // Ctors and dtors
|
|---|
| 34 | // void ?{}( coStack_t & this );
|
|---|
| 35 | // void ^?{}( coStack_t & this );
|
|---|
| 36 |
|
|---|
| 37 | void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize );
|
|---|
| 38 | void ^?{}( coroutine_desc & this );
|
|---|
| 39 |
|
|---|
| 40 | static inline void ?{}( coroutine_desc & this) { this{ "Anonymous Coroutine", NULL, 0 }; }
|
|---|
| 41 | static inline void ?{}( coroutine_desc & this, size_t stackSize) { this{ "Anonymous Coroutine", NULL, stackSize }; }
|
|---|
| 42 | static inline void ?{}( coroutine_desc & this, void * storage, size_t storageSize ) { this{ "Anonymous Coroutine", storage, storageSize }; }
|
|---|
| 43 | static inline void ?{}( coroutine_desc & this, const char * name) { this{ name, NULL, 0 }; }
|
|---|
| 44 | static inline void ?{}( coroutine_desc & this, const char * name, size_t stackSize ) { this{ name, NULL, stackSize }; }
|
|---|
| 45 |
|
|---|
| 46 | //-----------------------------------------------------------------------------
|
|---|
| 47 | // Public coroutine API
|
|---|
| 48 | static inline void suspend(void);
|
|---|
| 49 |
|
|---|
| 50 | forall(dtype T | is_coroutine(T))
|
|---|
| 51 | static inline void resume(T & cor);
|
|---|
| 52 |
|
|---|
| 53 | forall(dtype T | is_coroutine(T))
|
|---|
| 54 | void prime(T & cor);
|
|---|
| 55 |
|
|---|
| 56 | //-----------------------------------------------------------------------------
|
|---|
| 57 | // PRIVATE exposed because of inline
|
|---|
| 58 |
|
|---|
| 59 | // Start coroutine routines
|
|---|
| 60 | extern "C" {
|
|---|
| 61 | forall(dtype T | is_coroutine(T))
|
|---|
| 62 | void CtxInvokeCoroutine(T * this);
|
|---|
| 63 |
|
|---|
| 64 | forall(dtype T | is_coroutine(T))
|
|---|
| 65 | void CtxStart(T * this, void ( *invoke)(T *));
|
|---|
| 66 |
|
|---|
| 67 | extern void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc *) __attribute__ ((__noreturn__));
|
|---|
| 68 |
|
|---|
| 69 | extern void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch");
|
|---|
| 70 | // void CtxStore ( void * this ) asm ("CtxStore");
|
|---|
| 71 | // void CtxRet ( void * dst ) asm ("CtxRet");
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | // Private wrappers for context switch and stack creation
|
|---|
| 75 | // Wrapper for co
|
|---|
| 76 | static inline void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
|
|---|
| 77 | // set state of current coroutine to inactive
|
|---|
| 78 | src->state = src->state == Halted ? Halted : Inactive;
|
|---|
| 79 |
|
|---|
| 80 | // set new coroutine that task is executing
|
|---|
| 81 | TL_GET( this_thread )->curr_cor = dst;
|
|---|
| 82 |
|
|---|
| 83 | // context switch to specified coroutine
|
|---|
| 84 | verify( dst->context.SP );
|
|---|
| 85 | CtxSwitch( &src->context, &dst->context );
|
|---|
| 86 | // when CtxSwitch returns we are back in the src coroutine
|
|---|
| 87 |
|
|---|
| 88 | // set state of new coroutine to active
|
|---|
| 89 | src->state = Active;
|
|---|
| 90 |
|
|---|
| 91 | if( unlikely(src->cancellation != NULL) ) {
|
|---|
| 92 | _CtxCoroutine_Unwind(src->cancellation, src);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | extern void __stack_prepare ( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
|
|---|
| 97 |
|
|---|
| 98 | // Suspend implementation inlined for performance
|
|---|
| 99 | static inline void suspend(void) {
|
|---|
| 100 | // optimization : read TLS once and reuse it
|
|---|
| 101 | // Safety note: this is preemption safe since if
|
|---|
| 102 | // preemption occurs after this line, the pointer
|
|---|
| 103 | // will also migrate which means this value will
|
|---|
| 104 | // stay in syn with the TLS
|
|---|
| 105 | coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
|---|
| 106 |
|
|---|
| 107 | assertf( src->last != 0,
|
|---|
| 108 | "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
|
|---|
| 109 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
|
|---|
| 110 | src->name, src );
|
|---|
| 111 | assertf( src->last->state != Halted,
|
|---|
| 112 | "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
|
|---|
| 113 | "Possible cause is terminated coroutine's main routine has already returned.",
|
|---|
| 114 | src->name, src, src->last->name, src->last );
|
|---|
| 115 |
|
|---|
| 116 | CoroutineCtxSwitch( src, src->last );
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | // Resume implementation inlined for performance
|
|---|
| 120 | forall(dtype T | is_coroutine(T))
|
|---|
| 121 | static inline void resume(T & cor) {
|
|---|
| 122 | // optimization : read TLS once and reuse it
|
|---|
| 123 | // Safety note: this is preemption safe since if
|
|---|
| 124 | // preemption occurs after this line, the pointer
|
|---|
| 125 | // will also migrate which means this value will
|
|---|
| 126 | // stay in syn with the TLS
|
|---|
| 127 | coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
|---|
| 128 | coroutine_desc * dst = get_coroutine(cor);
|
|---|
| 129 |
|
|---|
| 130 | if( unlikely(dst->context.SP == NULL) ) {
|
|---|
| 131 | __stack_prepare(&dst->stack, 65000);
|
|---|
| 132 | CtxStart(&cor, CtxInvokeCoroutine);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | // not resuming self ?
|
|---|
| 136 | if ( src != dst ) {
|
|---|
| 137 | assertf( dst->state != Halted ,
|
|---|
| 138 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
|
|---|
| 139 | "Possible cause is terminated coroutine's main routine has already returned.",
|
|---|
| 140 | src->name, src, dst->name, dst );
|
|---|
| 141 |
|
|---|
| 142 | // set last resumer
|
|---|
| 143 | dst->last = src;
|
|---|
| 144 | dst->starter = dst->starter ? dst->starter : src;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | // always done for performance testing
|
|---|
| 148 | CoroutineCtxSwitch( src, dst );
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | static inline void resume(coroutine_desc * dst) {
|
|---|
| 152 | // optimization : read TLS once and reuse it
|
|---|
| 153 | // Safety note: this is preemption safe since if
|
|---|
| 154 | // preemption occurs after this line, the pointer
|
|---|
| 155 | // will also migrate which means this value will
|
|---|
| 156 | // stay in syn with the TLS
|
|---|
| 157 | coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
|---|
| 158 |
|
|---|
| 159 | // not resuming self ?
|
|---|
| 160 | if ( src != dst ) {
|
|---|
| 161 | assertf( dst->state != Halted ,
|
|---|
| 162 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
|
|---|
| 163 | "Possible cause is terminated coroutine's main routine has already returned.",
|
|---|
| 164 | src->name, src, dst->name, dst );
|
|---|
| 165 |
|
|---|
| 166 | // set last resumer
|
|---|
| 167 | dst->last = src;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | // always done for performance testing
|
|---|
| 171 | CoroutineCtxSwitch( src, dst );
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 | // static inline bool suspend_checkpoint(void) {
|
|---|
| 177 | // // optimization : read TLS once and reuse it
|
|---|
| 178 | // // Safety note: this is preemption safe since if
|
|---|
| 179 | // // preemption occurs after this line, the pointer
|
|---|
| 180 | // // will also migrate which means this value will
|
|---|
| 181 | // // stay in syn with the TLS
|
|---|
| 182 | // // set state of current coroutine to inactive
|
|---|
| 183 | // this->state = Checkpoint;
|
|---|
| 184 |
|
|---|
| 185 | // // context switch to specified coroutine
|
|---|
| 186 | // assert( src->stack.context );
|
|---|
| 187 |
|
|---|
| 188 | // CtxStore(src->stack.context);
|
|---|
| 189 |
|
|---|
| 190 | // bool ret = this->state == Checkpoint;
|
|---|
| 191 |
|
|---|
| 192 | // // set state of new coroutine to active
|
|---|
| 193 | // src->state = Active;
|
|---|
| 194 |
|
|---|
| 195 | // enable_interrupts( __cfaabi_dbg_ctx );
|
|---|
| 196 | // // Safety note : This could cause some false positives due to preemption
|
|---|
| 197 | // verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate );
|
|---|
| 198 |
|
|---|
| 199 | // if( unlikely(src->cancellation != NULL) ) {
|
|---|
| 200 | // _CtxCoroutine_Unwind(src->cancellation);
|
|---|
| 201 | // }
|
|---|
| 202 |
|
|---|
| 203 | // return ret;
|
|---|
| 204 | // }
|
|---|
| 205 |
|
|---|
| 206 | // static inline void suspend_return(void) {
|
|---|
| 207 | // // optimization : read TLS once and reuse it
|
|---|
| 208 | // // Safety note: this is preemption safe since if
|
|---|
| 209 | // // preemption occurs after this line, the pointer
|
|---|
| 210 | // // will also migrate which means this value will
|
|---|
| 211 | // // stay in syn with the TLS
|
|---|
| 212 | // coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
|---|
| 213 |
|
|---|
| 214 | // assertf( src->last != 0,
|
|---|
| 215 | // "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
|
|---|
| 216 | // "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
|
|---|
| 217 | // src->name, src );
|
|---|
| 218 | // assertf( src->last->state != Halted,
|
|---|
| 219 | // "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
|
|---|
| 220 | // "Possible cause is terminated coroutine's main routine has already returned.",
|
|---|
| 221 | // src->name, src, src->last->name, src->last );
|
|---|
| 222 |
|
|---|
| 223 | // // Safety note : Preemption must be disabled here since kernelTLS.this_coroutine must always be up to date
|
|---|
| 224 | // verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate );
|
|---|
| 225 | // disable_interrupts();
|
|---|
| 226 |
|
|---|
| 227 | // // set state of current coroutine to inactive
|
|---|
| 228 | // src->state = src->state == Halted ? Halted : Inactive;
|
|---|
| 229 |
|
|---|
| 230 | // // set new coroutine that task is executing
|
|---|
| 231 | // kernelTLS.this_coroutine = dst;
|
|---|
| 232 |
|
|---|
| 233 | // // context switch to specified coroutine
|
|---|
| 234 | // assert( src->stack.context );
|
|---|
| 235 | // CtxRet( src->stack.context );
|
|---|
| 236 |
|
|---|
| 237 | // abort();
|
|---|
| 238 | // }
|
|---|
| 239 |
|
|---|
| 240 | // Local Variables: //
|
|---|
| 241 | // mode: c //
|
|---|
| 242 | // tab-width: 4 //
|
|---|
| 243 | // End: //
|
|---|