[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
|
---|
[b10affd] | 12 | // Last Modified On : Fri Mar 30 18:23:45 2018
|
---|
| 13 | // Update Count : 8
|
---|
[6a3d2e7] | 14 | //
|
---|
| 15 |
|
---|
[6b0b624] | 16 | #pragma once
|
---|
[6a3d2e7] | 17 |
|
---|
[91c389a] | 18 | #include <assert.h>
|
---|
[6a3d2e7] | 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) {
|
---|
[83a071f9] | 26 | void main(T & this);
|
---|
| 27 | coroutine_desc * get_coroutine(T & this);
|
---|
[6a3d2e7] | 28 | };
|
---|
| 29 |
|
---|
[83a071f9] | 30 | #define DECL_COROUTINE(X) static inline coroutine_desc* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
|
---|
[c84e80a] | 31 |
|
---|
[6a3d2e7] | 32 | //-----------------------------------------------------------------------------
|
---|
| 33 | // Ctors and dtors
|
---|
[de6319f] | 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 }; }
|
---|
[6a3d2e7] | 45 |
|
---|
| 46 | //-----------------------------------------------------------------------------
|
---|
| 47 | // Public coroutine API
|
---|
[76d3ca6] | 48 | static inline void suspend(void);
|
---|
[6a3d2e7] | 49 |
|
---|
| 50 | forall(dtype T | is_coroutine(T))
|
---|
[10248ae0] | 51 | static inline T & resume(T & cor);
|
---|
[6a3d2e7] | 52 |
|
---|
| 53 | forall(dtype T | is_coroutine(T))
|
---|
[83a071f9] | 54 | void prime(T & cor);
|
---|
[6a3d2e7] | 55 |
|
---|
| 56 | //-----------------------------------------------------------------------------
|
---|
| 57 | // PRIVATE exposed because of inline
|
---|
| 58 |
|
---|
| 59 | // Start coroutine routines
|
---|
| 60 | extern "C" {
|
---|
| 61 | forall(dtype T | is_coroutine(T))
|
---|
[8118303] | 62 | void CtxInvokeCoroutine(T * this);
|
---|
[6a3d2e7] | 63 |
|
---|
| 64 | forall(dtype T | is_coroutine(T))
|
---|
[8118303] | 65 | void CtxStart(T * this, void ( *invoke)(T *));
|
---|
[3c06bba] | 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");
|
---|
[6a3d2e7] | 70 | }
|
---|
| 71 |
|
---|
| 72 | // Private wrappers for context switch and stack creation
|
---|
[3c06bba] | 73 | // Wrapper for co
|
---|
| 74 | static inline void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
|
---|
| 75 | // set state of current coroutine to inactive
|
---|
| 76 | src->state = src->state == Halted ? Halted : Inactive;
|
---|
| 77 |
|
---|
| 78 | // set new coroutine that task is executing
|
---|
| 79 | TL_GET( this_thread )->curr_cor = dst;
|
---|
| 80 |
|
---|
| 81 | // context switch to specified coroutine
|
---|
| 82 | verify( dst->context.SP );
|
---|
| 83 | CtxSwitch( &src->context, &dst->context );
|
---|
| 84 | // when CtxSwitch returns we are back in the src coroutine
|
---|
| 85 |
|
---|
| 86 | // set state of new coroutine to active
|
---|
| 87 | src->state = Active;
|
---|
| 88 |
|
---|
| 89 | if( unlikely(src->cancellation != NULL) ) {
|
---|
| 90 | _CtxCoroutine_Unwind(src->cancellation, src);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[b2f6113] | 94 | extern void __stack_prepare ( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
|
---|
[6a3d2e7] | 95 |
|
---|
| 96 | // Suspend implementation inlined for performance
|
---|
[76d3ca6] | 97 | static inline void suspend(void) {
|
---|
[14a61b5] | 98 | // optimization : read TLS once and reuse it
|
---|
| 99 | // Safety note: this is preemption safe since if
|
---|
| 100 | // preemption occurs after this line, the pointer
|
---|
| 101 | // will also migrate which means this value will
|
---|
| 102 | // stay in syn with the TLS
|
---|
[212c2187] | 103 | coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
---|
[6a3d2e7] | 104 |
|
---|
| 105 | assertf( src->last != 0,
|
---|
[8def349] | 106 | "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
|
---|
[6a3d2e7] | 107 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
|
---|
| 108 | src->name, src );
|
---|
[ee897e4b] | 109 | assertf( src->last->state != Halted,
|
---|
[8def349] | 110 | "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
|
---|
[6a3d2e7] | 111 | "Possible cause is terminated coroutine's main routine has already returned.",
|
---|
| 112 | src->name, src, src->last->name, src->last );
|
---|
| 113 |
|
---|
[0c92c9f] | 114 | CoroutineCtxSwitch( src, src->last );
|
---|
[6a3d2e7] | 115 | }
|
---|
| 116 |
|
---|
| 117 | // Resume implementation inlined for performance
|
---|
| 118 | forall(dtype T | is_coroutine(T))
|
---|
[aa00626] | 119 | static inline T & resume(T & cor) {
|
---|
[14a61b5] | 120 | // optimization : read TLS once and reuse it
|
---|
| 121 | // Safety note: this is preemption safe since if
|
---|
| 122 | // preemption occurs after this line, the pointer
|
---|
| 123 | // will also migrate which means this value will
|
---|
| 124 | // stay in syn with the TLS
|
---|
[212c2187] | 125 | coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
---|
[c3acb841] | 126 | coroutine_desc * dst = get_coroutine(cor);
|
---|
[6a3d2e7] | 127 |
|
---|
[8c01e1b] | 128 | if( unlikely(dst->context.SP == NULL) ) {
|
---|
[b2f6113] | 129 | __stack_prepare(&dst->stack, 65000);
|
---|
[83a071f9] | 130 | CtxStart(&cor, CtxInvokeCoroutine);
|
---|
[6a3d2e7] | 131 | }
|
---|
| 132 |
|
---|
[4aa2fb2] | 133 | // not resuming self ?
|
---|
[6a3d2e7] | 134 | if ( src != dst ) {
|
---|
[ee897e4b] | 135 | assertf( dst->state != Halted ,
|
---|
[6a3d2e7] | 136 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
|
---|
| 137 | "Possible cause is terminated coroutine's main routine has already returned.",
|
---|
| 138 | src->name, src, dst->name, dst );
|
---|
| 139 |
|
---|
[4aa2fb2] | 140 | // set last resumer
|
---|
[6a3d2e7] | 141 | dst->last = src;
|
---|
[b462670] | 142 | dst->starter = dst->starter ? dst->starter : src;
|
---|
[14a61b5] | 143 | }
|
---|
[6a3d2e7] | 144 |
|
---|
[4aa2fb2] | 145 | // always done for performance testing
|
---|
[0c92c9f] | 146 | CoroutineCtxSwitch( src, dst );
|
---|
[aa00626] | 147 |
|
---|
| 148 | return cor;
|
---|
[6a3d2e7] | 149 | }
|
---|
| 150 |
|
---|
[c3acb841] | 151 | static inline void resume(coroutine_desc * dst) {
|
---|
[14a61b5] | 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
|
---|
[212c2187] | 157 | coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
---|
[77e6fcb] | 158 |
|
---|
[4aa2fb2] | 159 | // not resuming self ?
|
---|
[77e6fcb] | 160 | if ( src != dst ) {
|
---|
[ee897e4b] | 161 | assertf( dst->state != Halted ,
|
---|
[77e6fcb] | 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 |
|
---|
[4aa2fb2] | 166 | // set last resumer
|
---|
[77e6fcb] | 167 | dst->last = src;
|
---|
[14a61b5] | 168 | }
|
---|
[77e6fcb] | 169 |
|
---|
[4aa2fb2] | 170 | // always done for performance testing
|
---|
[77e6fcb] | 171 | CoroutineCtxSwitch( src, dst );
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[6a3d2e7] | 174 | // Local Variables: //
|
---|
| 175 | // mode: c //
|
---|
| 176 | // tab-width: 4 //
|
---|
| 177 | // End: //
|
---|