source: libcfa/src/concurrency/coroutine.hfa@ 89c2a77b

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 89c2a77b was ecfd758, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Major exception update, seperating type-ids from virtual tables. The major interface changes are done. There is a regression of ?Cancelled(T) to Some?Cancelled. There is some bits of code for the new verion of the ?Cancelled(T) interface already there. Not connected yet but I just reached the limit of what I wanted to do in one commit and then spent over a day cleaning up, so it will replace Some?Cancelled in a future commit.

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