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 : Fri Mar 30 18:23:45 2018
|
---|
13 | // Update Count : 8
|
---|
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", 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 }; }
|
---|
45 |
|
---|
46 | //-----------------------------------------------------------------------------
|
---|
47 | // Public coroutine API
|
---|
48 | static inline void suspend();
|
---|
49 |
|
---|
50 | forall(dtype T | is_coroutine(T))
|
---|
51 | static inline void resume(T & cor);
|
---|
52 |
|
---|
53 | forall(dtype T | is_coroutine(T))
|
---|
54 | void prime(T & cor);
|
---|
55 |
|
---|
56 | //-----------------------------------------------------------------------------
|
---|
57 | // PRIVATE exposed because of inline
|
---|
58 |
|
---|
59 | // Start coroutine routines
|
---|
60 | extern "C" {
|
---|
61 | forall(dtype T | is_coroutine(T))
|
---|
62 | void CtxInvokeCoroutine(T * this);
|
---|
63 |
|
---|
64 | forall(dtype T | is_coroutine(T))
|
---|
65 | void CtxStart(T * this, void ( *invoke)(T *));
|
---|
66 | }
|
---|
67 |
|
---|
68 | // Private wrappers for context switch and stack creation
|
---|
69 | extern void CoroutineCtxSwitch(coroutine_desc * src, coroutine_desc * dst);
|
---|
70 | extern void create_stack( coStack_t * this, unsigned int storageSize );
|
---|
71 |
|
---|
72 | // Suspend implementation inlined for performance
|
---|
73 | static inline void suspend() {
|
---|
74 | // optimization : read TLS once and reuse it
|
---|
75 | // Safety note: this is preemption safe since if
|
---|
76 | // preemption occurs after this line, the pointer
|
---|
77 | // will also migrate which means this value will
|
---|
78 | // stay in syn with the TLS
|
---|
79 | coroutine_desc * src = TL_GET( this_coroutine );
|
---|
80 |
|
---|
81 | assertf( src->last != 0,
|
---|
82 | "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
|
---|
83 | "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
|
---|
84 | src->name, src );
|
---|
85 | assertf( src->last->state != Halted,
|
---|
86 | "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
|
---|
87 | "Possible cause is terminated coroutine's main routine has already returned.",
|
---|
88 | src->name, src, src->last->name, src->last );
|
---|
89 |
|
---|
90 | CoroutineCtxSwitch( src, src->last );
|
---|
91 | }
|
---|
92 |
|
---|
93 | // Resume implementation inlined for performance
|
---|
94 | forall(dtype T | is_coroutine(T))
|
---|
95 | static inline void resume(T & cor) {
|
---|
96 | // optimization : read TLS once and reuse it
|
---|
97 | // Safety note: this is preemption safe since if
|
---|
98 | // preemption occurs after this line, the pointer
|
---|
99 | // will also migrate which means this value will
|
---|
100 | // stay in syn with the TLS
|
---|
101 | coroutine_desc * src = TL_GET( this_coroutine );
|
---|
102 | coroutine_desc * dst = get_coroutine(cor);
|
---|
103 |
|
---|
104 | if( unlikely(!dst->stack.base) ) {
|
---|
105 | create_stack(&dst->stack, dst->stack.size);
|
---|
106 | CtxStart(&cor, CtxInvokeCoroutine);
|
---|
107 | }
|
---|
108 |
|
---|
109 | // not resuming self ?
|
---|
110 | if ( src != dst ) {
|
---|
111 | assertf( dst->state != Halted ,
|
---|
112 | "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
|
---|
113 | "Possible cause is terminated coroutine's main routine has already returned.",
|
---|
114 | src->name, src, dst->name, dst );
|
---|
115 |
|
---|
116 | // set last resumer
|
---|
117 | dst->last = src;
|
---|
118 | dst->starter = dst->starter ? dst->starter : src;
|
---|
119 | }
|
---|
120 |
|
---|
121 | // always done for performance testing
|
---|
122 | CoroutineCtxSwitch( src, dst );
|
---|
123 | }
|
---|
124 |
|
---|
125 | static inline void resume(coroutine_desc * dst) {
|
---|
126 | // optimization : read TLS once and reuse it
|
---|
127 | // Safety note: this is preemption safe since if
|
---|
128 | // preemption occurs after this line, the pointer
|
---|
129 | // will also migrate which means this value will
|
---|
130 | // stay in syn with the TLS
|
---|
131 | coroutine_desc * src = TL_GET( this_coroutine );
|
---|
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 | }
|
---|
143 |
|
---|
144 | // always done for performance testing
|
---|
145 | CoroutineCtxSwitch( src, dst );
|
---|
146 | }
|
---|
147 |
|
---|
148 | // Local Variables: //
|
---|
149 | // mode: c //
|
---|
150 | // tab-width: 4 //
|
---|
151 | // End: //
|
---|