source: libcfa/src/concurrency/coroutine.hfa @ 4eb3a7c5

Last change on this file since 4eb3a7c5 was 147a137, checked in by caparsons <caparson@…>, 9 months ago

added enable/disable ehm with no params for coroutines

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