[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"
|
---|
| 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);
|
---|
[ac2b598] | 27 | $coroutine * get_coroutine(T & this);
|
---|
[6a3d2e7] | 28 | };
|
---|
| 29 |
|
---|
[ac2b598] | 30 | #define DECL_COROUTINE(X) static inline $coroutine* 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 |
|
---|
[ac2b598] | 37 | void ?{}( $coroutine & this, const char name[], void * storage, size_t storageSize );
|
---|
| 38 | void ^?{}( $coroutine & this );
|
---|
[de6319f] | 39 |
|
---|
[ac2b598] | 40 | static inline void ?{}( $coroutine & this) { this{ "Anonymous Coroutine", 0p, 0 }; }
|
---|
| 41 | static inline void ?{}( $coroutine & this, size_t stackSize) { this{ "Anonymous Coroutine", 0p, stackSize }; }
|
---|
| 42 | static inline void ?{}( $coroutine & this, void * storage, size_t storageSize ) { this{ "Anonymous Coroutine", storage, storageSize }; }
|
---|
| 43 | static inline void ?{}( $coroutine & this, const char name[]) { this{ name, 0p, 0 }; }
|
---|
| 44 | static inline void ?{}( $coroutine & this, const char name[], size_t stackSize ) { this{ name, 0p, 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 |
|
---|
[ac2b598] | 56 | static inline struct $coroutine * active_coroutine() { return TL_GET( this_thread )->curr_cor; }
|
---|
[d4e68a6] | 57 |
|
---|
[6a3d2e7] | 58 | //-----------------------------------------------------------------------------
|
---|
| 59 | // PRIVATE exposed because of inline
|
---|
| 60 |
|
---|
| 61 | // Start coroutine routines
|
---|
| 62 | extern "C" {
|
---|
[c7a900a] | 63 | void __cfactx_invoke_coroutine(void (*main)(void *), void * this);
|
---|
[6a3d2e7] | 64 |
|
---|
[09f357ec] | 65 | forall(dtype T)
|
---|
[ac2b598] | 66 | void __cfactx_start(void (*main)(T &), struct $coroutine * cor, T & this, void (*invoke)(void (*main)(void *), void *));
|
---|
[3c06bba] | 67 |
|
---|
[ac2b598] | 68 | extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine *) __attribute__ ((__noreturn__));
|
---|
[3c06bba] | 69 |
|
---|
[c7a900a] | 70 | extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
|
---|
[6a3d2e7] | 71 | }
|
---|
| 72 |
|
---|
| 73 | // Private wrappers for context switch and stack creation
|
---|
[3c06bba] | 74 | // Wrapper for co
|
---|
[ac2b598] | 75 | static inline void $ctx_switch( $coroutine * src, $coroutine * dst ) __attribute__((nonnull (1, 2))) {
|
---|
[3c06bba] | 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 );
|
---|
[c7a900a] | 84 | __cfactx_switch( &src->context, &dst->context );
|
---|
| 85 | // when __cfactx_switch returns we are back in the src coroutine
|
---|
[3c06bba] | 86 |
|
---|
| 87 | // set state of new coroutine to active
|
---|
| 88 | src->state = Active;
|
---|
| 89 |
|
---|
[121be3e] | 90 | if( unlikely(src->cancellation != 0p) ) {
|
---|
[c7a900a] | 91 | __cfactx_coroutine_unwind(src->cancellation, src);
|
---|
[3c06bba] | 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[b2f6113] | 95 | extern void __stack_prepare ( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
|
---|
[6a3d2e7] | 96 |
|
---|
| 97 | // Suspend implementation inlined for performance
|
---|
[76d3ca6] | 98 | static inline void suspend(void) {
|
---|
[14a61b5] | 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
|
---|
[ac2b598] | 104 | $coroutine * src = TL_GET( this_thread )->curr_cor;
|
---|
[6a3d2e7] | 105 |
|
---|
| 106 | assertf( src->last != 0,
|
---|
[8def349] | 107 | "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
|
---|
[6a3d2e7] | 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 );
|
---|
[ee897e4b] | 110 | assertf( src->last->state != Halted,
|
---|
[8def349] | 111 | "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
|
---|
[6a3d2e7] | 112 | "Possible cause is terminated coroutine's main routine has already returned.",
|
---|
| 113 | src->name, src, src->last->name, src->last );
|
---|
| 114 |
|
---|
[ac2b598] | 115 | $ctx_switch( src, src->last );
|
---|
[6a3d2e7] | 116 | }
|
---|
| 117 |
|
---|
| 118 | // Resume implementation inlined for performance
|
---|
| 119 | forall(dtype T | is_coroutine(T))
|
---|
[aa00626] | 120 | static inline T & resume(T & cor) {
|
---|
[14a61b5] | 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
|
---|
[ac2b598] | 126 | $coroutine * src = TL_GET( this_thread )->curr_cor;
|
---|
| 127 | $coroutine * dst = get_coroutine(cor);
|
---|
[6a3d2e7] | 128 |
|
---|
[121be3e] | 129 | if( unlikely(dst->context.SP == 0p) ) {
|
---|
[09f357ec] | 130 | TL_GET( this_thread )->curr_cor = dst;
|
---|
[b2f6113] | 131 | __stack_prepare(&dst->stack, 65000);
|
---|
[c7a900a] | 132 | __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine);
|
---|
[09f357ec] | 133 | TL_GET( this_thread )->curr_cor = src;
|
---|
[6a3d2e7] | 134 | }
|
---|
| 135 |
|
---|
[4aa2fb2] | 136 | // not resuming self ?
|
---|
[6a3d2e7] | 137 | if ( src != dst ) {
|
---|
[ee897e4b] | 138 | assertf( dst->state != Halted ,
|
---|
[6a3d2e7] | 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 |
|
---|
[4aa2fb2] | 143 | // set last resumer
|
---|
[6a3d2e7] | 144 | dst->last = src;
|
---|
[b462670] | 145 | dst->starter = dst->starter ? dst->starter : src;
|
---|
[14a61b5] | 146 | }
|
---|
[6a3d2e7] | 147 |
|
---|
[4aa2fb2] | 148 | // always done for performance testing
|
---|
[ac2b598] | 149 | $ctx_switch( src, dst );
|
---|
[aa00626] | 150 |
|
---|
| 151 | return cor;
|
---|
[6a3d2e7] | 152 | }
|
---|
| 153 |
|
---|
[ac2b598] | 154 | static inline void resume( $coroutine * dst ) __attribute__((nonnull (1))) {
|
---|
[14a61b5] | 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
|
---|
[ac2b598] | 160 | $coroutine * src = TL_GET( this_thread )->curr_cor;
|
---|
[77e6fcb] | 161 |
|
---|
[4aa2fb2] | 162 | // not resuming self ?
|
---|
[77e6fcb] | 163 | if ( src != dst ) {
|
---|
[ee897e4b] | 164 | assertf( dst->state != Halted ,
|
---|
[77e6fcb] | 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 |
|
---|
[4aa2fb2] | 169 | // set last resumer
|
---|
[77e6fcb] | 170 | dst->last = src;
|
---|
[14a61b5] | 171 | }
|
---|
[77e6fcb] | 172 |
|
---|
[4aa2fb2] | 173 | // always done for performance testing
|
---|
[ac2b598] | 174 | $ctx_switch( src, dst );
|
---|
[77e6fcb] | 175 | }
|
---|
| 176 |
|
---|
[6a3d2e7] | 177 | // Local Variables: //
|
---|
| 178 | // mode: c //
|
---|
| 179 | // tab-width: 4 //
|
---|
| 180 | // End: //
|
---|