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

ADTast-experimental
Last change on this file since b797d978 was c3b9d639, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Clean-up the exception interface. It should be slightly more like the final - non-macro - interface.

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