source: libcfa/src/concurrency/coroutine.hfa@ c979afa

Last change on this file since c979afa was 0957f62, checked in by Peter A. Buhr <pabuhr@…>, 6 weeks 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
RevLine 
[6a3d2e7]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//
[75a17f1]7// coroutine --
[6a3d2e7]8//
9// Author : Thierry Delisle
10// Created On : Mon Nov 28 12:27:26 2016
[91c389a]11// Last Modified By : Peter A. Buhr
[0957f62]12// Last Modified On : Sun Mar 1 17:44:11 2026
13// Update Count : 43
[6a3d2e7]14//
15
[6b0b624]16#pragma once
[6a3d2e7]17
[91c389a]18#include <assert.h>
[6a3d2e7]19#include "invoke.h"
[1c01c58]20#include "../exception.hfa"
21
[2fe64ba]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};
[6b33e89]28
29static inline void ?{}( nonlocal_exception & this, exception_t * ex ) with(this) {
[2fe64ba]30 the_exception = ex;
[6b33e89]31 this.next = 0p;
[2fe64ba]32}
33
34static inline nonlocal_exception *& get_next( nonlocal_exception & this ) __attribute__((const)) {
35 return this.next;
36}
37
[1c01c58]38//-----------------------------------------------------------------------------
39// Exception thrown from resume when a coroutine stack is cancelled.
[c715e5f]40forall(coroutine_t &)
41exception CoroutineCancelled {
[1c01c58]42 coroutine_t * the_coroutine;
43 exception_t * the_exception;
[c715e5f]44};
[1c01c58]45
[fd54fef]46forall(T &)
[1c01c58]47void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src);
48
[fd54fef]49forall(T &)
[1c01c58]50const char * msg(CoroutineCancelled(T) *);
[6a3d2e7]51
52//-----------------------------------------------------------------------------
53// Coroutine trait
54// Anything that implements this trait can be resumed.
55// Anything that is resumed is a coroutine.
[8a97248]56forall( T & | IS_RESUMPTION_EXCEPTION(CoroutineCancelled(T)) )
57trait is_coroutine {
[1c01c58]58 void main(T & this);
[e84ab3d]59 coroutine$ * get_coroutine(T & this);
[6a3d2e7]60};
61
[e84ab3d]62#define DECL_COROUTINE(X) static inline coroutine$* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
[c84e80a]63
[6a3d2e7]64//-----------------------------------------------------------------------------
65// Ctors and dtors
[de6319f]66// void ?{}( coStack_t & this );
67// void ^?{}( coStack_t & this );
68
[6b33e89]69void ?{}( coroutine$ & this, const char name[], void * storage, size_t storageSize );
[e84ab3d]70void ^?{}( coroutine$ & this );
[de6319f]71
[e84ab3d]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 }; }
[6a3d2e7]77
78//-----------------------------------------------------------------------------
79// Public coroutine API
[c3b9d639]80forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); })
[83a071f9]81void prime(T & cor);
[6a3d2e7]82
[e84ab3d]83static inline struct coroutine$ * active_coroutine() { return active_thread()->curr_cor; }
[d4e68a6]84
[6a3d2e7]85//-----------------------------------------------------------------------------
86// PRIVATE exposed because of inline
87
88// Start coroutine routines
89extern "C" {
[c7a900a]90 void __cfactx_invoke_coroutine(void (*main)(void *), void * this);
[6a3d2e7]91
[fd54fef]92 forall(T &)
[e84ab3d]93 void __cfactx_start(void (*main)(T &), struct coroutine$ * cor, T & this, void (*invoke)(void (*main)(void *), void *));
[3c06bba]94
[e84ab3d]95 extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct coroutine$ *) __attribute__ ((__noreturn__));
[3c06bba]96
[c7a900a]97 extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
[6a3d2e7]98}
99
[0957f62]100void stack_verify( coroutine$ * cor );
101void stack_verify();
102
[6a3d2e7]103// Private wrappers for context switch and stack creation
[e84ab3d]104static inline void $ctx_switch( coroutine$ * src, coroutine$ * dst ) __attribute__((nonnull (1, 2))) {
[3c06bba]105 // set state of current coroutine to inactive
[ae7be7a]106 src->state = src->state == Halted ? Halted : Blocked;
[3c06bba]107
[ab5baab]108 // get the active thread once
[e84ab3d]109 thread$ * athrd = active_thread();
[ab5baab]110
111 // Mark the coroutine
112 /* paranoid */ verify( !athrd->corctx_flag );
113 athrd->corctx_flag = true;
114
[0957f62]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
[3c06bba]119 // set new coroutine that task is executing
[ab5baab]120 athrd->curr_cor = dst;
[3c06bba]121
122 // context switch to specified coroutine
[ab5baab]123 /* paranoid */ verify( dst->context.SP );
[c7a900a]124 __cfactx_switch( &src->context, &dst->context );
125 // when __cfactx_switch returns we are back in the src coroutine
[3c06bba]126
[ab5baab]127 /* paranoid */ verify( athrd->corctx_flag );
128 athrd->corctx_flag = false;
129
[3c06bba]130 // set state of new coroutine to active
131 src->state = Active;
132
[4269d1b]133 if( unlikely(src->cancellation != 0p && src->cancellation != 1p) ) {
[c7a900a]134 __cfactx_coroutine_unwind(src->cancellation, src);
[3c06bba]135 }
136}
137
[bfcf6b9]138extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
[6a3d2e7]139
140// Suspend implementation inlined for performance
[427854b]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
[e84ab3d]148 coroutine$ * src = active_coroutine();
[427854b]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 );
[6a3d2e7]158
[427854b]159 $ctx_switch( src, src->last );
160 }
[6a3d2e7]161}
162
[fd54fef]163forall(T & | is_coroutine(T))
[b583113]164void __cfaehm_cancelled_coroutine(
[c3b9d639]165 T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled(T)) );
[1c01c58]166
[6a3d2e7]167// Resume implementation inlined for performance
[c3b9d639]168forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); })
[aa00626]169static inline T & resume(T & cor) {
[14a61b5]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
[e84ab3d]175 coroutine$ * src = active_coroutine();
176 coroutine$ * dst = get_coroutine(cor);
[6a3d2e7]177
[4269d1b]178 // printf("FROM RES src: %p, dest: %p\n", src, dst);
179
[121be3e]180 if( unlikely(dst->context.SP == 0p) ) {
[eaf269d]181 __stack_prepare(&dst->stack, DEFAULT_STACK_SIZE);
[c7a900a]182 __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine);
[6a3d2e7]183 }
184
[4aa2fb2]185 // not resuming self ?
[6a3d2e7]186 if ( src != dst ) {
[ee897e4b]187 assertf( dst->state != Halted ,
[6a3d2e7]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
[4aa2fb2]192 // set last resumer
[6a3d2e7]193 dst->last = src;
[b462670]194 dst->starter = dst->starter ? dst->starter : src;
[14a61b5]195 }
[6a3d2e7]196
[4aa2fb2]197 // always done for performance testing
[ac2b598]198 $ctx_switch( src, dst );
[4269d1b]199
200 if ( unlikely(src->cancellation == 1p) ) {
201 src->cancellation = 0p;
202 // we know dst hasn't been deallocated
[b583113]203 __cfaehm_cancelled_coroutine( cor, dst, _default_vtable );
[1c01c58]204 }
[aa00626]205
206 return cor;
[6a3d2e7]207}
208
[e84ab3d]209static inline void resume( coroutine$ * dst ) __attribute__((nonnull (1))) {
[14a61b5]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
[e84ab3d]215 coroutine$ * src = active_coroutine();
[77e6fcb]216
[4aa2fb2]217 // not resuming self ?
[77e6fcb]218 if ( src != dst ) {
[ee897e4b]219 assertf( dst->state != Halted ,
[77e6fcb]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
[4aa2fb2]224 // set last resumer
[77e6fcb]225 dst->last = src;
[14a61b5]226 }
[77e6fcb]227
[4aa2fb2]228 // always done for performance testing
[ac2b598]229 $ctx_switch( src, dst );
[77e6fcb]230}
231
[c3e510b]232// non local ehm and coroutine utility routines
[0957f62]233void * stack_pointer( coroutine$ * cor );
234void * stack_pointer();
[147a137]235void enable_ehm();
236void disable_ehm();
[c34bb1f]237bool poll( coroutine$ * cor );
238bool poll();
[147a137]239bool checked_poll();
[3318dff]240coroutine$ * resumer();
[4269d1b]241coroutine$ * first_resumer();
[c34bb1f]242
[2fe64ba]243forall(T & | is_coroutine(T)) {
[0957f62]244 void * stack_pointer( T & cor );
245 void stack_verify( T & cor );
[147a137]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
[2fe64ba]248 bool poll( T & cor );
[147a137]249 bool checked_poll( T & cor ); // check for non-local exceptions while respecting enable/disable
[c3e510b]250 coroutine$ * resumer( T & cor );
[4269d1b]251 coroutine$ * first_resumer( T & cor );
[2fe64ba]252}
253
254// trait for exceptions able to be resumed at another coroutine
[3318dff]255forall(exceptT *, T & | is_coroutine(T))
[2fe64ba]256trait ehm_resume_at { void $throwResume(exceptT &); };
257
[c3e510b]258// general resumeAt
[3318dff]259forall(exceptT *, T & | ehm_resume_at( exceptT, T ))
[2fe64ba]260void resumeAt( T & receiver, exceptT & ex );
261
[c3e510b]262// resumeAt for underlying coroutine$ type
[3318dff]263forall(exceptT * | { void $throwResume(exceptT &); })
[c3e510b]264void resumeAt( coroutine$ * receiver, exceptT & ex );
265
[6a3d2e7]266// Local Variables: //
267// mode: c //
268// tab-width: 4 //
269// End: //
Note: See TracBrowser for help on using the repository browser.