source: libcfa/src/concurrency/coroutine.hfa@ 2b0e754

Last change on this file since 2b0e754 was 6b33e89, checked in by Peter A. Buhr <pabuhr@…>, 5 months ago

change backquote call to regular call

  • Property mode set to 100644
File size: 8.9 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
[6b33e89]12// Last Modified On : Fri Apr 25 06:52:04 2025
13// Update Count : 15
[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
100// Private wrappers for context switch and stack creation
[3c06bba]101// Wrapper for co
[e84ab3d]102static inline void $ctx_switch( coroutine$ * src, coroutine$ * dst ) __attribute__((nonnull (1, 2))) {
[3c06bba]103 // set state of current coroutine to inactive
[ae7be7a]104 src->state = src->state == Halted ? Halted : Blocked;
[3c06bba]105
[ab5baab]106 // get the active thread once
[e84ab3d]107 thread$ * athrd = active_thread();
[ab5baab]108
109 // Mark the coroutine
110 /* paranoid */ verify( !athrd->corctx_flag );
111 athrd->corctx_flag = true;
112
[3c06bba]113 // set new coroutine that task is executing
[ab5baab]114 athrd->curr_cor = dst;
[3c06bba]115
116 // context switch to specified coroutine
[ab5baab]117 /* paranoid */ verify( dst->context.SP );
[c7a900a]118 __cfactx_switch( &src->context, &dst->context );
119 // when __cfactx_switch returns we are back in the src coroutine
[3c06bba]120
[ab5baab]121 /* paranoid */ verify( athrd->corctx_flag );
122 athrd->corctx_flag = false;
123
[3c06bba]124 // set state of new coroutine to active
125 src->state = Active;
126
[4269d1b]127 if( unlikely(src->cancellation != 0p && src->cancellation != 1p) ) {
[c7a900a]128 __cfactx_coroutine_unwind(src->cancellation, src);
[3c06bba]129 }
130}
131
[bfcf6b9]132extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
[6a3d2e7]133
134// Suspend implementation inlined for performance
[427854b]135extern "C" {
136 static inline void __cfactx_suspend(void) {
137 // optimization : read TLS once and reuse it
138 // Safety note: this is preemption safe since if
139 // preemption occurs after this line, the pointer
140 // will also migrate which means this value will
141 // stay in syn with the TLS
[e84ab3d]142 coroutine$ * src = active_coroutine();
[427854b]143
144 assertf( src->last != 0,
145 "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
146 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
147 src->name, src );
148 assertf( src->last->state != Halted,
149 "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
150 "Possible cause is terminated coroutine's main routine has already returned.",
151 src->name, src, src->last->name, src->last );
[6a3d2e7]152
[427854b]153 $ctx_switch( src, src->last );
154 }
[6a3d2e7]155}
156
[fd54fef]157forall(T & | is_coroutine(T))
[b583113]158void __cfaehm_cancelled_coroutine(
[c3b9d639]159 T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled(T)) );
[1c01c58]160
[6a3d2e7]161// Resume implementation inlined for performance
[c3b9d639]162forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); })
[aa00626]163static inline T & resume(T & cor) {
[14a61b5]164 // optimization : read TLS once and reuse it
165 // Safety note: this is preemption safe since if
166 // preemption occurs after this line, the pointer
167 // will also migrate which means this value will
168 // stay in syn with the TLS
[e84ab3d]169 coroutine$ * src = active_coroutine();
170 coroutine$ * dst = get_coroutine(cor);
[6a3d2e7]171
[4269d1b]172 // printf("FROM RES src: %p, dest: %p\n", src, dst);
173
[121be3e]174 if( unlikely(dst->context.SP == 0p) ) {
[eaf269d]175 __stack_prepare(&dst->stack, DEFAULT_STACK_SIZE);
[c7a900a]176 __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine);
[6a3d2e7]177 }
178
[4aa2fb2]179 // not resuming self ?
[6a3d2e7]180 if ( src != dst ) {
[ee897e4b]181 assertf( dst->state != Halted ,
[6a3d2e7]182 "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
183 "Possible cause is terminated coroutine's main routine has already returned.",
184 src->name, src, dst->name, dst );
185
[4aa2fb2]186 // set last resumer
[6a3d2e7]187 dst->last = src;
[b462670]188 dst->starter = dst->starter ? dst->starter : src;
[14a61b5]189 }
[6a3d2e7]190
[4aa2fb2]191 // always done for performance testing
[ac2b598]192 $ctx_switch( src, dst );
[4269d1b]193
194 if ( unlikely(src->cancellation == 1p) ) {
195 src->cancellation = 0p;
196 // we know dst hasn't been deallocated
[b583113]197 __cfaehm_cancelled_coroutine( cor, dst, _default_vtable );
[1c01c58]198 }
[aa00626]199
200 return cor;
[6a3d2e7]201}
202
[e84ab3d]203static inline void resume( coroutine$ * dst ) __attribute__((nonnull (1))) {
[14a61b5]204 // optimization : read TLS once and reuse it
205 // Safety note: this is preemption safe since if
206 // preemption occurs after this line, the pointer
207 // will also migrate which means this value will
208 // stay in syn with the TLS
[e84ab3d]209 coroutine$ * src = active_coroutine();
[77e6fcb]210
[4aa2fb2]211 // not resuming self ?
[77e6fcb]212 if ( src != dst ) {
[ee897e4b]213 assertf( dst->state != Halted ,
[77e6fcb]214 "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
215 "Possible cause is terminated coroutine's main routine has already returned.",
216 src->name, src, dst->name, dst );
217
[4aa2fb2]218 // set last resumer
[77e6fcb]219 dst->last = src;
[14a61b5]220 }
[77e6fcb]221
[4aa2fb2]222 // always done for performance testing
[ac2b598]223 $ctx_switch( src, dst );
[77e6fcb]224}
225
[c3e510b]226// non local ehm and coroutine utility routines
[147a137]227void enable_ehm();
228void disable_ehm();
[c34bb1f]229bool poll( coroutine$ * cor );
230bool poll();
[147a137]231bool checked_poll();
[3318dff]232coroutine$ * resumer();
[4269d1b]233coroutine$ * first_resumer();
[c34bb1f]234
[2fe64ba]235forall(T & | is_coroutine(T)) {
[147a137]236 void enable_ehm( T & cor ); // enable checking non-local exceptions for cor via checked_poll
237 void disable_ehm( T & cor ); // disable checking non-local exceptions for cor via checked_poll
[2fe64ba]238 bool poll( T & cor );
[147a137]239 bool checked_poll( T & cor ); // check for non-local exceptions while respecting enable/disable
[c3e510b]240 coroutine$ * resumer( T & cor );
[4269d1b]241 coroutine$ * first_resumer( T & cor );
[2fe64ba]242}
243
244// trait for exceptions able to be resumed at another coroutine
[3318dff]245forall(exceptT *, T & | is_coroutine(T))
[2fe64ba]246trait ehm_resume_at { void $throwResume(exceptT &); };
247
[c3e510b]248// general resumeAt
[3318dff]249forall(exceptT *, T & | ehm_resume_at( exceptT, T ))
[2fe64ba]250void resumeAt( T & receiver, exceptT & ex );
251
[c3e510b]252// resumeAt for underlying coroutine$ type
[3318dff]253forall(exceptT * | { void $throwResume(exceptT &); })
[c3e510b]254void resumeAt( coroutine$ * receiver, exceptT & ex );
255
[6a3d2e7]256// Local Variables: //
257// mode: c //
258// tab-width: 4 //
259// End: //
Note: See TracBrowser for help on using the repository browser.