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

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 a33c113 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
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 : Tue Feb 4 12:29:26 2020
13// Update Count : 11
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.
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)) (
32 coroutine_t * the_coroutine;
33 exception_t * the_exception;
34);
35
36forall(T &)
37void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src);
38
39forall(T &)
40const char * msg(CoroutineCancelled(T) *);
41
42//-----------------------------------------------------------------------------
43// Coroutine trait
44// Anything that implements this trait can be resumed.
45// Anything that is resumed is a coroutine.
46trait is_coroutine(T & | IS_RESUMPTION_EXCEPTION(SomeCoroutineCancelled)) {
47 void main(T & this);
48 $coroutine * get_coroutine(T & this);
49};
50
51#define DECL_COROUTINE(X) static inline $coroutine* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
52
53//-----------------------------------------------------------------------------
54// Ctors and dtors
55// void ?{}( coStack_t & this );
56// void ^?{}( coStack_t & this );
57
58void ?{}( $coroutine & this, const char name[], void * storage, size_t storageSize );
59void ^?{}( $coroutine & this );
60
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 }; }
66
67//-----------------------------------------------------------------------------
68// Public coroutine API
69forall(T & | is_coroutine(T))
70void prime(T & cor);
71
72static inline struct $coroutine * active_coroutine() { return active_thread()->curr_cor; }
73
74//-----------------------------------------------------------------------------
75// PRIVATE exposed because of inline
76
77// Start coroutine routines
78extern "C" {
79 void __cfactx_invoke_coroutine(void (*main)(void *), void * this);
80
81 forall(T &)
82 void __cfactx_start(void (*main)(T &), struct $coroutine * cor, T & this, void (*invoke)(void (*main)(void *), void *));
83
84 extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine *) __attribute__ ((__noreturn__));
85
86 extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
87}
88
89// Private wrappers for context switch and stack creation
90// Wrapper for co
91static inline void $ctx_switch( $coroutine * src, $coroutine * dst ) __attribute__((nonnull (1, 2))) {
92 // set state of current coroutine to inactive
93 src->state = src->state == Halted ? Halted : Blocked;
94
95 // set new coroutine that task is executing
96 active_thread()->curr_cor = dst;
97
98 // context switch to specified coroutine
99 verify( dst->context.SP );
100 __cfactx_switch( &src->context, &dst->context );
101 // when __cfactx_switch returns we are back in the src coroutine
102
103 // set state of new coroutine to active
104 src->state = Active;
105
106 if( unlikely(src->cancellation != 0p) ) {
107 __cfactx_coroutine_unwind(src->cancellation, src);
108 }
109}
110
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
114
115// Suspend implementation inlined for performance
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
123 $coroutine * src = active_coroutine();
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 );
133
134 $ctx_switch( src, src->last );
135 }
136}
137
138forall(T & | is_coroutine(T))
139void __cfaehm_cancelled_coroutine( T & cor, $coroutine * desc );
140
141// Resume implementation inlined for performance
142forall(T & | is_coroutine(T))
143static inline T & resume(T & cor) {
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
149 $coroutine * src = active_coroutine();
150 $coroutine * dst = get_coroutine(cor);
151
152 if( unlikely(dst->context.SP == 0p) ) {
153 __stack_prepare(&dst->stack, 65000);
154 __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine);
155 }
156
157 // not resuming self ?
158 if ( src != dst ) {
159 assertf( dst->state != Halted ,
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
164 // set last resumer
165 dst->last = src;
166 dst->starter = dst->starter ? dst->starter : src;
167 }
168
169 // always done for performance testing
170 $ctx_switch( src, dst );
171 if ( unlikely(dst->cancellation) ) {
172 __cfaehm_cancelled_coroutine( cor, dst );
173 }
174
175 return cor;
176}
177
178static inline void resume( $coroutine * dst ) __attribute__((nonnull (1))) {
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
184 $coroutine * src = active_coroutine();
185
186 // not resuming self ?
187 if ( src != dst ) {
188 assertf( dst->state != Halted ,
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
193 // set last resumer
194 dst->last = src;
195 }
196
197 // always done for performance testing
198 $ctx_switch( src, dst );
199}
200
201// Local Variables: //
202// mode: c //
203// tab-width: 4 //
204// End: //
Note: See TracBrowser for help on using the repository browser.