source: libcfa/src/concurrency/coroutine.hfa @ 6fbe9a5

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6fbe9a5 was 69c5c00, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Rework exceptions mark_exception -> get_exception_vtable and the needed follow up.

  • Property mode set to 100644
File size: 7.0 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
[e3fea42]12// Last Modified On : Tue Feb  4 12:29:26 2020
13// Update Count     : 11
[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
22//-----------------------------------------------------------------------------
23// Exception thrown from resume when a coroutine stack is cancelled.
[69c5c00]24FORALL_DATA_EXCEPTION(CoroutineCancelled, (dtype coroutine_t), (coroutine_t)) (
[1c01c58]25        coroutine_t * the_coroutine;
26        exception_t * the_exception;
27);
28
29forall(dtype T)
30void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src);
31
32forall(dtype T)
33const char * msg(CoroutineCancelled(T) *);
[6a3d2e7]34
35//-----------------------------------------------------------------------------
36// Coroutine trait
37// Anything that implements this trait can be resumed.
38// Anything that is resumed is a coroutine.
[69c5c00]39trait is_coroutine(dtype T
40                | is_resumption_exception(CoroutineCancelled(T),
41                        CoroutineCancelled_vtable(T))) {
[1c01c58]42        void main(T & this);
43        $coroutine * get_coroutine(T & this);
[6a3d2e7]44};
45
[ac2b598]46#define DECL_COROUTINE(X) static inline $coroutine* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
[c84e80a]47
[6a3d2e7]48//-----------------------------------------------------------------------------
49// Ctors and dtors
[de6319f]50// void ?{}( coStack_t & this );
51// void ^?{}( coStack_t & this );
52
[ac2b598]53void  ?{}( $coroutine & this, const char name[], void * storage, size_t storageSize );
54void ^?{}( $coroutine & this );
[de6319f]55
[ac2b598]56static inline void ?{}( $coroutine & this)                                       { this{ "Anonymous Coroutine", 0p, 0 }; }
57static inline void ?{}( $coroutine & this, size_t stackSize)                     { this{ "Anonymous Coroutine", 0p, stackSize }; }
58static inline void ?{}( $coroutine & this, void * storage, size_t storageSize )  { this{ "Anonymous Coroutine", storage, storageSize }; }
59static inline void ?{}( $coroutine & this, const char name[])                    { this{ name, 0p, 0 }; }
60static inline void ?{}( $coroutine & this, const char name[], size_t stackSize ) { this{ name, 0p, stackSize }; }
[6a3d2e7]61
62//-----------------------------------------------------------------------------
63// Public coroutine API
64forall(dtype T | is_coroutine(T))
[83a071f9]65void prime(T & cor);
[6a3d2e7]66
[ac2b598]67static inline struct $coroutine * active_coroutine() { return TL_GET( this_thread )->curr_cor; }
[d4e68a6]68
[6a3d2e7]69//-----------------------------------------------------------------------------
70// PRIVATE exposed because of inline
71
72// Start coroutine routines
73extern "C" {
[c7a900a]74        void __cfactx_invoke_coroutine(void (*main)(void *), void * this);
[6a3d2e7]75
[09f357ec]76        forall(dtype T)
[ac2b598]77        void __cfactx_start(void (*main)(T &), struct $coroutine * cor, T & this, void (*invoke)(void (*main)(void *), void *));
[3c06bba]78
[ac2b598]79        extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine *) __attribute__ ((__noreturn__));
[3c06bba]80
[c7a900a]81        extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
[6a3d2e7]82}
83
84// Private wrappers for context switch and stack creation
[3c06bba]85// Wrapper for co
[ac2b598]86static inline void $ctx_switch( $coroutine * src, $coroutine * dst ) __attribute__((nonnull (1, 2))) {
[3c06bba]87        // set state of current coroutine to inactive
[ae7be7a]88        src->state = src->state == Halted ? Halted : Blocked;
[3c06bba]89
90        // set new coroutine that task is executing
91        TL_GET( this_thread )->curr_cor = dst;
92
93        // context switch to specified coroutine
94        verify( dst->context.SP );
[c7a900a]95        __cfactx_switch( &src->context, &dst->context );
96        // when __cfactx_switch returns we are back in the src coroutine
[3c06bba]97
98        // set state of new coroutine to active
99        src->state = Active;
100
[121be3e]101        if( unlikely(src->cancellation != 0p) ) {
[c7a900a]102                __cfactx_coroutine_unwind(src->cancellation, src);
[3c06bba]103        }
104}
105
[b2f6113]106extern void __stack_prepare   ( __stack_info_t * this, size_t size /* ignored if storage already allocated */);
[6a3d2e7]107
108// Suspend implementation inlined for performance
[427854b]109extern "C" {
110        static inline void __cfactx_suspend(void) {
111                // optimization : read TLS once and reuse it
112                // Safety note: this is preemption safe since if
113                // preemption occurs after this line, the pointer
114                // will also migrate which means this value will
115                // stay in syn with the TLS
116                $coroutine * src = TL_GET( this_thread )->curr_cor;
117
118                assertf( src->last != 0,
119                        "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n"
120                        "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
121                        src->name, src );
122                assertf( src->last->state != Halted,
123                        "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n"
124                        "Possible cause is terminated coroutine's main routine has already returned.",
125                        src->name, src, src->last->name, src->last );
[6a3d2e7]126
[427854b]127                $ctx_switch( src, src->last );
128        }
[6a3d2e7]129}
130
[1c01c58]131forall(dtype T | is_coroutine(T))
132void __cfaehm_cancelled_coroutine( T & cor, $coroutine * desc );
133
[6a3d2e7]134// Resume implementation inlined for performance
135forall(dtype T | is_coroutine(T))
[aa00626]136static inline T & resume(T & cor) {
[14a61b5]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
[ac2b598]142        $coroutine * src = TL_GET( this_thread )->curr_cor;
143        $coroutine * dst = get_coroutine(cor);
[6a3d2e7]144
[121be3e]145        if( unlikely(dst->context.SP == 0p) ) {
[09f357ec]146                TL_GET( this_thread )->curr_cor = dst;
[b2f6113]147                __stack_prepare(&dst->stack, 65000);
[c7a900a]148                __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine);
[09f357ec]149                TL_GET( this_thread )->curr_cor = src;
[6a3d2e7]150        }
151
[4aa2fb2]152        // not resuming self ?
[6a3d2e7]153        if ( src != dst ) {
[ee897e4b]154                assertf( dst->state != Halted ,
[6a3d2e7]155                        "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
156                        "Possible cause is terminated coroutine's main routine has already returned.",
157                        src->name, src, dst->name, dst );
158
[4aa2fb2]159                // set last resumer
[6a3d2e7]160                dst->last = src;
[b462670]161                dst->starter = dst->starter ? dst->starter : src;
[14a61b5]162        }
[6a3d2e7]163
[4aa2fb2]164        // always done for performance testing
[ac2b598]165        $ctx_switch( src, dst );
[1c01c58]166        if ( unlikely(dst->cancellation) ) {
167                __cfaehm_cancelled_coroutine( cor, dst );
168        }
[aa00626]169
170        return cor;
[6a3d2e7]171}
172
[ac2b598]173static inline void resume( $coroutine * dst ) __attribute__((nonnull (1))) {
[14a61b5]174        // optimization : read TLS once and reuse it
175        // Safety note: this is preemption safe since if
176        // preemption occurs after this line, the pointer
177        // will also migrate which means this value will
178        // stay in syn with the TLS
[ac2b598]179        $coroutine * src = TL_GET( this_thread )->curr_cor;
[77e6fcb]180
[4aa2fb2]181        // not resuming self ?
[77e6fcb]182        if ( src != dst ) {
[ee897e4b]183                assertf( dst->state != Halted ,
[77e6fcb]184                        "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n"
185                        "Possible cause is terminated coroutine's main routine has already returned.",
186                        src->name, src, dst->name, dst );
187
[4aa2fb2]188                // set last resumer
[77e6fcb]189                dst->last = src;
[14a61b5]190        }
[77e6fcb]191
[4aa2fb2]192        // always done for performance testing
[ac2b598]193        $ctx_switch( src, dst );
[77e6fcb]194}
195
[6a3d2e7]196// Local Variables: //
197// mode: c //
198// tab-width: 4 //
199// End: //
Note: See TracBrowser for help on using the repository browser.