| [8118303] | 1 | //                              - *- Mode: CFA - *-
 | 
|---|
| [6a3d2e7] | 2 | //
 | 
|---|
 | 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
 | 
|---|
 | 4 | //
 | 
|---|
 | 5 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
 | 6 | // file "LICENCE" distributed with Cforall.
 | 
|---|
 | 7 | //
 | 
|---|
| [75a17f1] | 8 | // coroutine --
 | 
|---|
| [6a3d2e7] | 9 | //
 | 
|---|
 | 10 | // Author           : Thierry Delisle
 | 
|---|
 | 11 | // Created On       : Mon Nov 28 12:27:26 2016
 | 
|---|
| [91c389a] | 12 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [6b0b624] | 13 | // Last Modified On : Sat Jul 22 09:57:17 2017
 | 
|---|
 | 14 | // Update Count     : 2
 | 
|---|
| [6a3d2e7] | 15 | //
 | 
|---|
 | 16 | 
 | 
|---|
| [6b0b624] | 17 | #pragma once
 | 
|---|
| [6a3d2e7] | 18 | 
 | 
|---|
| [91c389a] | 19 | #include <assert.h>
 | 
|---|
| [6a3d2e7] | 20 | #include "invoke.h"
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | //-----------------------------------------------------------------------------
 | 
|---|
 | 23 | // Coroutine trait
 | 
|---|
 | 24 | // Anything that implements this trait can be resumed.
 | 
|---|
 | 25 | // Anything that is resumed is a coroutine.
 | 
|---|
 | 26 | trait is_coroutine(dtype T) {
 | 
|---|
| [7fbe450] | 27 |       void main(T * this);
 | 
|---|
| [c3acb841] | 28 |       coroutine_desc * get_coroutine(T * this);
 | 
|---|
| [6a3d2e7] | 29 | };
 | 
|---|
 | 30 | 
 | 
|---|
| [17af7d1] | 31 | #define DECL_COROUTINE(X) static inline coroutine_desc* get_coroutine(X* this) { return &this->__cor; } void main(X* this)
 | 
|---|
| [c84e80a] | 32 | 
 | 
|---|
| [6a3d2e7] | 33 | //-----------------------------------------------------------------------------
 | 
|---|
 | 34 | // Ctors and dtors
 | 
|---|
| [8118303] | 35 | void ?{}(coStack_t * this);
 | 
|---|
| [c3acb841] | 36 | void ?{}(coroutine_desc * this);
 | 
|---|
 | 37 | void ?{}(coroutine_desc * this, const char * name);
 | 
|---|
| [8118303] | 38 | void ^?{}(coStack_t * this);
 | 
|---|
| [c3acb841] | 39 | void ^?{}(coroutine_desc * this);
 | 
|---|
| [6a3d2e7] | 40 | 
 | 
|---|
 | 41 | //-----------------------------------------------------------------------------
 | 
|---|
 | 42 | // Public coroutine API
 | 
|---|
 | 43 | static inline void suspend();
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | forall(dtype T | is_coroutine(T))
 | 
|---|
| [8118303] | 46 | static inline void resume(T * cor);
 | 
|---|
| [6a3d2e7] | 47 | 
 | 
|---|
 | 48 | forall(dtype T | is_coroutine(T))
 | 
|---|
| [8118303] | 49 | void prime(T * cor);
 | 
|---|
| [6a3d2e7] | 50 | 
 | 
|---|
 | 51 | //-----------------------------------------------------------------------------
 | 
|---|
 | 52 | // PRIVATE exposed because of inline
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | // Start coroutine routines
 | 
|---|
 | 55 | extern "C" {
 | 
|---|
 | 56 |       forall(dtype T | is_coroutine(T))
 | 
|---|
| [8118303] | 57 |       void CtxInvokeCoroutine(T * this);
 | 
|---|
| [6a3d2e7] | 58 | 
 | 
|---|
 | 59 |       forall(dtype T | is_coroutine(T))
 | 
|---|
| [8118303] | 60 |       void CtxStart(T * this, void ( *invoke)(T *));
 | 
|---|
| [6a3d2e7] | 61 | }
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | // Get current coroutine
 | 
|---|
| [9cc0472] | 64 | extern thread_local coroutine_desc * volatile this_coroutine;
 | 
|---|
| [6a3d2e7] | 65 | 
 | 
|---|
 | 66 | // Private wrappers for context switch and stack creation
 | 
|---|
| [c3acb841] | 67 | extern void CoroutineCtxSwitch(coroutine_desc * src, coroutine_desc * dst);
 | 
|---|
| [8118303] | 68 | extern void create_stack( coStack_t * this, unsigned int storageSize );
 | 
|---|
| [6a3d2e7] | 69 | 
 | 
|---|
 | 70 | // Suspend implementation inlined for performance
 | 
|---|
 | 71 | static inline void suspend() {
 | 
|---|
| [1c273d0] | 72 |         coroutine_desc * src = this_coroutine;          // optimization
 | 
|---|
| [6a3d2e7] | 73 | 
 | 
|---|
 | 74 |         assertf( src->last != 0,
 | 
|---|
| [8def349] | 75 |                 "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
 | 
|---|
| [6a3d2e7] | 76 |                 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
 | 
|---|
 | 77 |                 src->name, src );
 | 
|---|
| [ee897e4b] | 78 |         assertf( src->last->state != Halted,
 | 
|---|
| [8def349] | 79 |                 "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
 | 
|---|
| [6a3d2e7] | 80 |                 "Possible cause is terminated coroutine's main routine has already returned.",
 | 
|---|
 | 81 |                 src->name, src, src->last->name, src->last );
 | 
|---|
 | 82 | 
 | 
|---|
| [0c92c9f] | 83 |         CoroutineCtxSwitch( src, src->last );
 | 
|---|
| [6a3d2e7] | 84 | }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | // Resume implementation inlined for performance
 | 
|---|
 | 87 | forall(dtype T | is_coroutine(T))
 | 
|---|
| [8118303] | 88 | static inline void resume(T * cor) {
 | 
|---|
| [1c273d0] | 89 |         coroutine_desc * src = this_coroutine;          // optimization
 | 
|---|
| [c3acb841] | 90 |         coroutine_desc * dst = get_coroutine(cor);
 | 
|---|
| [6a3d2e7] | 91 | 
 | 
|---|
| [4aa2fb2] | 92 |         if( unlikely(!dst->stack.base) ) {
 | 
|---|
| [6a3d2e7] | 93 |                 create_stack(&dst->stack, dst->stack.size);
 | 
|---|
 | 94 |                 CtxStart(cor, CtxInvokeCoroutine);
 | 
|---|
 | 95 |         }
 | 
|---|
 | 96 | 
 | 
|---|
| [4aa2fb2] | 97 |         // not resuming self ?
 | 
|---|
| [6a3d2e7] | 98 |         if ( src != dst ) {
 | 
|---|
| [ee897e4b] | 99 |                 assertf( dst->state != Halted ,
 | 
|---|
| [6a3d2e7] | 100 |                         "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
 | 
|---|
 | 101 |                         "Possible cause is terminated coroutine's main routine has already returned.",
 | 
|---|
 | 102 |                         src->name, src, dst->name, dst );
 | 
|---|
 | 103 | 
 | 
|---|
| [4aa2fb2] | 104 |                 // set last resumer
 | 
|---|
| [6a3d2e7] | 105 |                 dst->last = src;
 | 
|---|
 | 106 |         } // if
 | 
|---|
 | 107 | 
 | 
|---|
| [4aa2fb2] | 108 |         // always done for performance testing
 | 
|---|
| [0c92c9f] | 109 |         CoroutineCtxSwitch( src, dst );
 | 
|---|
| [6a3d2e7] | 110 | }
 | 
|---|
 | 111 | 
 | 
|---|
| [c3acb841] | 112 | static inline void resume(coroutine_desc * dst) {
 | 
|---|
| [1c273d0] | 113 |         coroutine_desc * src = this_coroutine;          // optimization
 | 
|---|
| [77e6fcb] | 114 | 
 | 
|---|
| [4aa2fb2] | 115 |         // not resuming self ?
 | 
|---|
| [77e6fcb] | 116 |         if ( src != dst ) {
 | 
|---|
| [ee897e4b] | 117 |                 assertf( dst->state != Halted ,
 | 
|---|
| [77e6fcb] | 118 |                         "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
 | 
|---|
 | 119 |                         "Possible cause is terminated coroutine's main routine has already returned.",
 | 
|---|
 | 120 |                         src->name, src, dst->name, dst );
 | 
|---|
 | 121 | 
 | 
|---|
| [4aa2fb2] | 122 |                 // set last resumer
 | 
|---|
| [77e6fcb] | 123 |                 dst->last = src;
 | 
|---|
 | 124 |         } // if
 | 
|---|
 | 125 | 
 | 
|---|
| [4aa2fb2] | 126 |         // always done for performance testing
 | 
|---|
| [77e6fcb] | 127 |         CoroutineCtxSwitch( src, dst );
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | 
 | 
|---|
| [6a3d2e7] | 130 | // Local Variables: //
 | 
|---|
 | 131 | // mode: c //
 | 
|---|
 | 132 | // tab-width: 4 //
 | 
|---|
 | 133 | // End: //
 | 
|---|