source: libcfa/src/concurrency/coroutine.hfa @ 7bb6bd8

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 7bb6bd8 was 212c2187, checked in by tdelisle <tdelisle@…>, 6 years ago

Removed kernelTLS.this_coroutine which was redundant and some preleminary work for breaking into 2 step the context-switch

  • Property mode set to 100644
File size: 7.7 KB
Line 
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.
25trait 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
37void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize );
38void ^?{}( coroutine_desc & this );
39
40static inline void ?{}( coroutine_desc & this)                                       { this{ "Anonymous Coroutine", NULL, 0 }; }
41static inline void ?{}( coroutine_desc & this, size_t stackSize)                     { this{ "Anonymous Coroutine", NULL, stackSize }; }
42static inline void ?{}( coroutine_desc & this, void * storage, size_t storageSize )  { this{ "Anonymous Coroutine", storage, storageSize }; }
43static inline void ?{}( coroutine_desc & this, const char * name)                    { this{ name, NULL, 0 }; }
44static inline void ?{}( coroutine_desc & this, const char * name, size_t stackSize ) { this{ name, NULL, stackSize }; }
45
46//-----------------------------------------------------------------------------
47// Public coroutine API
48static inline void suspend(void);
49
50forall(dtype T | is_coroutine(T))
51static inline void resume(T & cor);
52
53forall(dtype T | is_coroutine(T))
54void prime(T & cor);
55
56//-----------------------------------------------------------------------------
57// PRIVATE exposed because of inline
58
59// Start coroutine routines
60extern "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
69extern void CoroutineCtxSwitch(coroutine_desc * src, coroutine_desc * dst);
70extern void create_stack( coStack_t * this, unsigned int storageSize );
71
72// Suspend implementation inlined for performance
73static inline void suspend(void) {
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_thread )->curr_cor;
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
94forall(dtype T | is_coroutine(T))
95static 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_thread )->curr_cor;
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
125static 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_thread )->curr_cor;
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
149
150// static inline bool suspend_checkpoint(void) {
151//      // optimization : read TLS once and reuse it
152//      // Safety note: this is preemption safe since if
153//      // preemption occurs after this line, the pointer
154//      // will also migrate which means this value will
155//      // stay in syn with the TLS
156//      // set state of current coroutine to inactive
157//       this->state = Checkpoint;
158
159//       // context switch to specified coroutine
160//       assert( src->stack.context );
161
162//       CtxStore(src->stack.context);
163
164//      bool ret = this->state == Checkpoint;
165
166//       // set state of new coroutine to active
167//       src->state = Active;
168
169//       enable_interrupts( __cfaabi_dbg_ctx );
170//       // Safety note : This could cause some false positives due to preemption
171//       verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate );
172
173//       if( unlikely(src->cancellation != NULL) ) {
174//             _CtxCoroutine_Unwind(src->cancellation);
175//       }
176
177//      return ret;
178// }
179
180// static inline void suspend_return(void) {
181//      // optimization : read TLS once and reuse it
182//      // Safety note: this is preemption safe since if
183//      // preemption occurs after this line, the pointer
184//      // will also migrate which means this value will
185//      // stay in syn with the TLS
186//      coroutine_desc * src = TL_GET( this_thread )->curr_cor;
187
188//      assertf( src->last != 0,
189//              "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
190//              "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
191//              src->name, src );
192//      assertf( src->last->state != Halted,
193//              "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
194//              "Possible cause is terminated coroutine's main routine has already returned.",
195//              src->name, src, src->last->name, src->last );
196
197//      // Safety note : Preemption must be disabled here since kernelTLS.this_coroutine must always be up to date
198//       verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate );
199//       disable_interrupts();
200
201//       // set state of current coroutine to inactive
202//       src->state = src->state == Halted ? Halted : Inactive;
203
204//       // set new coroutine that task is executing
205//       kernelTLS.this_coroutine = dst;
206
207//       // context switch to specified coroutine
208//       assert( src->stack.context );
209//      CtxRet( src->stack.context );
210
211//      abort();
212// }
213
214// Local Variables: //
215// mode: c //
216// tab-width: 4 //
217// End: //
Note: See TracBrowser for help on using the repository browser.