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