source: libcfa/src/concurrency/coroutine.hfa@ 54c01bb

Last change on this file since 54c01bb was 0957f62, checked in by Peter A. Buhr <pabuhr@…>, 3 days ago

add routines stack_verify and stack_pointer, in debug mode call stack_verify on front-side of context switch and time-slicing

  • Property mode set to 100644
File size: 9.2 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 : Sun Mar 1 17:44:11 2026
13// Update Count : 43
14//
15
16#pragma once
17
18#include <assert.h>
19#include "invoke.h"
20#include "../exception.hfa"
21
22//-----------------------------------------------------------------------------
23// Type used to store and queue nonlocal exceptions on coroutines
24struct nonlocal_exception {
25 exception_t * the_exception;
26 nonlocal_exception * next;
27};
28
29static inline void ?{}( nonlocal_exception & this, exception_t * ex ) with(this) {
30 the_exception = ex;
31 this.next = 0p;
32}
33
34static inline nonlocal_exception *& get_next( nonlocal_exception & this ) __attribute__((const)) {
35 return this.next;
36}
37
38//-----------------------------------------------------------------------------
39// Exception thrown from resume when a coroutine stack is cancelled.
40forall(coroutine_t &)
41exception CoroutineCancelled {
42 coroutine_t * the_coroutine;
43 exception_t * the_exception;
44};
45
46forall(T &)
47void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src);
48
49forall(T &)
50const char * msg(CoroutineCancelled(T) *);
51
52//-----------------------------------------------------------------------------
53// Coroutine trait
54// Anything that implements this trait can be resumed.
55// Anything that is resumed is a coroutine.
56forall( T & | IS_RESUMPTION_EXCEPTION(CoroutineCancelled(T)) )
57trait is_coroutine {
58 void main(T & this);
59 coroutine$ * get_coroutine(T & this);
60};
61
62#define DECL_COROUTINE(X) static inline coroutine$* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
63
64//-----------------------------------------------------------------------------
65// Ctors and dtors
66// void ?{}( coStack_t & this );
67// void ^?{}( coStack_t & this );
68
69void ?{}( coroutine$ & this, const char name[], void * storage, size_t storageSize );
70void ^?{}( coroutine$ & this );
71
72static inline void ?{}( coroutine$ & this) { this{ "Anonymous Coroutine", 0p, 0 }; }
73static inline void ?{}( coroutine$ & this, size_t stackSize) { this{ "Anonymous Coroutine", 0p, stackSize }; }
74static inline void ?{}( coroutine$ & this, void * storage, size_t storageSize ) { this{ "Anonymous Coroutine", storage, storageSize }; }
75static inline void ?{}( coroutine$ & this, const char name[]) { this{ name, 0p, 0 }; }
76static inline void ?{}( coroutine$ & this, const char name[], size_t stackSize ) { this{ name, 0p, stackSize }; }
77
78//-----------------------------------------------------------------------------
79// Public coroutine API
80forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); })
81void prime(T & cor);
82
83static inline struct coroutine$ * active_coroutine() { return active_thread()->curr_cor; }
84
85//-----------------------------------------------------------------------------
86// PRIVATE exposed because of inline
87
88// Start coroutine routines
89extern "C" {
90 void __cfactx_invoke_coroutine(void (*main)(void *), void * this);
91
92 forall(T &)
93 void __cfactx_start(void (*main)(T &), struct coroutine$ * cor, T & this, void (*invoke)(void (*main)(void *), void *));
94
95 extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct coroutine$ *) __attribute__ ((__noreturn__));
96
97 extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
98}
99
100void stack_verify( coroutine$ * cor );
101void stack_verify();
102
103// Private wrappers for context switch and stack creation
104static inline void $ctx_switch( coroutine$ * src, coroutine$ * dst ) __attribute__((nonnull (1, 2))) {
105 // set state of current coroutine to inactive
106 src->state = src->state == Halted ? Halted : Blocked;
107
108 // get the active thread once
109 thread$ * athrd = active_thread();
110
111 // Mark the coroutine
112 /* paranoid */ verify( !athrd->corctx_flag );
113 athrd->corctx_flag = true;
114
115 #if defined( __CFA_DEBUG__ )
116 stack_verify( src ); // test on front side of context switch, backside is too late.
117 #endif // __CFA_DEBUG__
118
119 // set new coroutine that task is executing
120 athrd->curr_cor = dst;
121
122 // context switch to specified coroutine
123 /* paranoid */ verify( dst->context.SP );
124 __cfactx_switch( &src->context, &dst->context );
125 // when __cfactx_switch returns we are back in the src coroutine
126
127 /* paranoid */ verify( athrd->corctx_flag );
128 athrd->corctx_flag = false;
129
130 // set state of new coroutine to active
131 src->state = Active;
132
133 if( unlikely(src->cancellation != 0p && src->cancellation != 1p) ) {
134 __cfactx_coroutine_unwind(src->cancellation, src);
135 }
136}
137
138extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
139
140// Suspend implementation inlined for performance
141extern "C" {
142 static inline void __cfactx_suspend(void) {
143 // optimization : read TLS once and reuse it
144 // Safety note: this is preemption safe since if
145 // preemption occurs after this line, the pointer
146 // will also migrate which means this value will
147 // stay in syn with the TLS
148 coroutine$ * src = active_coroutine();
149
150 assertf( src->last != 0,
151 "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
152 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
153 src->name, src );
154 assertf( src->last->state != Halted,
155 "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
156 "Possible cause is terminated coroutine's main routine has already returned.",
157 src->name, src, src->last->name, src->last );
158
159 $ctx_switch( src, src->last );
160 }
161}
162
163forall(T & | is_coroutine(T))
164void __cfaehm_cancelled_coroutine(
165 T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled(T)) );
166
167// Resume implementation inlined for performance
168forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); })
169static inline T & resume(T & cor) {
170 // optimization : read TLS once and reuse it
171 // Safety note: this is preemption safe since if
172 // preemption occurs after this line, the pointer
173 // will also migrate which means this value will
174 // stay in syn with the TLS
175 coroutine$ * src = active_coroutine();
176 coroutine$ * dst = get_coroutine(cor);
177
178 // printf("FROM RES src: %p, dest: %p\n", src, dst);
179
180 if( unlikely(dst->context.SP == 0p) ) {
181 __stack_prepare(&dst->stack, DEFAULT_STACK_SIZE);
182 __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine);
183 }
184
185 // not resuming self ?
186 if ( src != dst ) {
187 assertf( dst->state != Halted ,
188 "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
189 "Possible cause is terminated coroutine's main routine has already returned.",
190 src->name, src, dst->name, dst );
191
192 // set last resumer
193 dst->last = src;
194 dst->starter = dst->starter ? dst->starter : src;
195 }
196
197 // always done for performance testing
198 $ctx_switch( src, dst );
199
200 if ( unlikely(src->cancellation == 1p) ) {
201 src->cancellation = 0p;
202 // we know dst hasn't been deallocated
203 __cfaehm_cancelled_coroutine( cor, dst, _default_vtable );
204 }
205
206 return cor;
207}
208
209static inline void resume( coroutine$ * dst ) __attribute__((nonnull (1))) {
210 // optimization : read TLS once and reuse it
211 // Safety note: this is preemption safe since if
212 // preemption occurs after this line, the pointer
213 // will also migrate which means this value will
214 // stay in syn with the TLS
215 coroutine$ * src = active_coroutine();
216
217 // not resuming self ?
218 if ( src != dst ) {
219 assertf( dst->state != Halted ,
220 "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
221 "Possible cause is terminated coroutine's main routine has already returned.",
222 src->name, src, dst->name, dst );
223
224 // set last resumer
225 dst->last = src;
226 }
227
228 // always done for performance testing
229 $ctx_switch( src, dst );
230}
231
232// non local ehm and coroutine utility routines
233void * stack_pointer( coroutine$ * cor );
234void * stack_pointer();
235void enable_ehm();
236void disable_ehm();
237bool poll( coroutine$ * cor );
238bool poll();
239bool checked_poll();
240coroutine$ * resumer();
241coroutine$ * first_resumer();
242
243forall(T & | is_coroutine(T)) {
244 void * stack_pointer( T & cor );
245 void stack_verify( T & cor );
246 void enable_ehm( T & cor ); // enable checking non-local exceptions for cor via checked_poll
247 void disable_ehm( T & cor ); // disable checking non-local exceptions for cor via checked_poll
248 bool poll( T & cor );
249 bool checked_poll( T & cor ); // check for non-local exceptions while respecting enable/disable
250 coroutine$ * resumer( T & cor );
251 coroutine$ * first_resumer( T & cor );
252}
253
254// trait for exceptions able to be resumed at another coroutine
255forall(exceptT *, T & | is_coroutine(T))
256trait ehm_resume_at { void $throwResume(exceptT &); };
257
258// general resumeAt
259forall(exceptT *, T & | ehm_resume_at( exceptT, T ))
260void resumeAt( T & receiver, exceptT & ex );
261
262// resumeAt for underlying coroutine$ type
263forall(exceptT * | { void $throwResume(exceptT &); })
264void resumeAt( coroutine$ * receiver, exceptT & ex );
265
266// Local Variables: //
267// mode: c //
268// tab-width: 4 //
269// End: //
Note: See TracBrowser for help on using the repository browser.