| 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 |
|
|---|
| 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", 0p, 0 }; }
|
|---|
| 41 | static inline void ?{}( coroutine_desc & this, size_t stackSize) { this{ "Anonymous Coroutine", 0p, 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, 0p, 0 }; }
|
|---|
| 44 | static inline void ?{}( coroutine_desc & this, const char name[], size_t stackSize ) { this{ name, 0p, stackSize }; }
|
|---|
| 45 |
|
|---|
| 46 | //-----------------------------------------------------------------------------
|
|---|
| 47 | // Public coroutine API
|
|---|
| 48 | static inline void suspend(void);
|
|---|
| 49 |
|
|---|
| 50 | forall(dtype T | is_coroutine(T))
|
|---|
| 51 | static inline T & resume(T & cor);
|
|---|
| 52 |
|
|---|
| 53 | forall(dtype T | is_coroutine(T))
|
|---|
| 54 | void prime(T & cor);
|
|---|
| 55 |
|
|---|
| 56 | static inline struct coroutine_desc * active_coroutine() { return TL_GET( this_thread )->curr_cor; }
|
|---|
| 57 |
|
|---|
| 58 | //-----------------------------------------------------------------------------
|
|---|
| 59 | // PRIVATE exposed because of inline
|
|---|
| 60 |
|
|---|
| 61 | // Start coroutine routines
|
|---|
| 62 | extern "C" {
|
|---|
| 63 | void CtxInvokeCoroutine(void (*main)(void *), void * this);
|
|---|
| 64 |
|
|---|
| 65 | forall(dtype T)
|
|---|
| 66 | void CtxStart(void (*main)(T &), struct coroutine_desc * cor, T & this, void (*invoke)(void (*main)(void *), void *));
|
|---|
| 67 |
|
|---|
| 68 | extern void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc *) __attribute__ ((__noreturn__));
|
|---|
| 69 |
|
|---|
| 70 | extern void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | // Private wrappers for context switch and stack creation
|
|---|
| 74 | // Wrapper for co
|
|---|
| 75 | static inline void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
|
|---|
| 76 | // set state of current coroutine to inactive
|
|---|
| 77 | src->state = src->state == Halted ? Halted : Inactive;
|
|---|
| 78 |
|
|---|
| 79 | // set new coroutine that task is executing
|
|---|
| 80 | TL_GET( this_thread )->curr_cor = dst;
|
|---|
| 81 |
|
|---|
| 82 | // context switch to specified coroutine
|
|---|
| 83 | verify( dst->context.SP );
|
|---|
| 84 | CtxSwitch( &src->context, &dst->context );
|
|---|
| 85 | // when CtxSwitch returns we are back in the src coroutine
|
|---|
| 86 |
|
|---|
| 87 | // set state of new coroutine to active
|
|---|
| 88 | src->state = Active;
|
|---|
| 89 |
|
|---|
| 90 | if( unlikely(src->cancellation != 0p) ) {
|
|---|
| 91 | _CtxCoroutine_Unwind(src->cancellation, src);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | extern void __stack_prepare ( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
|
|---|
| 96 |
|
|---|
| 97 | // Suspend implementation inlined for performance
|
|---|
| 98 | static inline void suspend(void) {
|
|---|
| 99 | // optimization : read TLS once and reuse it
|
|---|
| 100 | // Safety note: this is preemption safe since if
|
|---|
| 101 | // preemption occurs after this line, the pointer
|
|---|
| 102 | // will also migrate which means this value will
|
|---|
| 103 | // stay in syn with the TLS
|
|---|
| 104 | coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
|---|
| 105 |
|
|---|
| 106 | assertf( src->last != 0,
|
|---|
| 107 | "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
|
|---|
| 108 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
|
|---|
| 109 | src->name, src );
|
|---|
| 110 | assertf( src->last->state != Halted,
|
|---|
| 111 | "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
|
|---|
| 112 | "Possible cause is terminated coroutine's main routine has already returned.",
|
|---|
| 113 | src->name, src, src->last->name, src->last );
|
|---|
| 114 |
|
|---|
| 115 | CoroutineCtxSwitch( src, src->last );
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // Resume implementation inlined for performance
|
|---|
| 119 | forall(dtype T | is_coroutine(T))
|
|---|
| 120 | static inline T & resume(T & cor) {
|
|---|
| 121 | // optimization : read TLS once and reuse it
|
|---|
| 122 | // Safety note: this is preemption safe since if
|
|---|
| 123 | // preemption occurs after this line, the pointer
|
|---|
| 124 | // will also migrate which means this value will
|
|---|
| 125 | // stay in syn with the TLS
|
|---|
| 126 | coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
|---|
| 127 | coroutine_desc * dst = get_coroutine(cor);
|
|---|
| 128 |
|
|---|
| 129 | if( unlikely(dst->context.SP == 0p) ) {
|
|---|
| 130 | TL_GET( this_thread )->curr_cor = dst;
|
|---|
| 131 | __stack_prepare(&dst->stack, 65000);
|
|---|
| 132 | CtxStart(main, dst, cor, CtxInvokeCoroutine);
|
|---|
| 133 | TL_GET( this_thread )->curr_cor = src;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | // not resuming self ?
|
|---|
| 137 | if ( src != dst ) {
|
|---|
| 138 | assertf( dst->state != Halted ,
|
|---|
| 139 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
|
|---|
| 140 | "Possible cause is terminated coroutine's main routine has already returned.",
|
|---|
| 141 | src->name, src, dst->name, dst );
|
|---|
| 142 |
|
|---|
| 143 | // set last resumer
|
|---|
| 144 | dst->last = src;
|
|---|
| 145 | dst->starter = dst->starter ? dst->starter : src;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | // always done for performance testing
|
|---|
| 149 | CoroutineCtxSwitch( src, dst );
|
|---|
| 150 |
|
|---|
| 151 | return cor;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | static inline void resume(coroutine_desc * dst) {
|
|---|
| 155 | // optimization : read TLS once and reuse it
|
|---|
| 156 | // Safety note: this is preemption safe since if
|
|---|
| 157 | // preemption occurs after this line, the pointer
|
|---|
| 158 | // will also migrate which means this value will
|
|---|
| 159 | // stay in syn with the TLS
|
|---|
| 160 | coroutine_desc * src = TL_GET( this_thread )->curr_cor;
|
|---|
| 161 |
|
|---|
| 162 | // not resuming self ?
|
|---|
| 163 | if ( src != dst ) {
|
|---|
| 164 | assertf( dst->state != Halted ,
|
|---|
| 165 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
|
|---|
| 166 | "Possible cause is terminated coroutine's main routine has already returned.",
|
|---|
| 167 | src->name, src, dst->name, dst );
|
|---|
| 168 |
|
|---|
| 169 | // set last resumer
|
|---|
| 170 | dst->last = src;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | // always done for performance testing
|
|---|
| 174 | CoroutineCtxSwitch( src, dst );
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | // Local Variables: //
|
|---|
| 178 | // mode: c //
|
|---|
| 179 | // tab-width: 4 //
|
|---|
| 180 | // End: //
|
|---|