source: libcfa/src/concurrency/coroutine.cfa@ 5d2db68

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 5d2db68 was 342be43, checked in by Andrew Beach <ajbeach@…>, 5 years ago

Some exception clean-up that did not require any drastic changes.

  • Property mode set to 100644
File size: 8.4 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.c --
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 : Fri Oct 23 23:05:24 2020
13// Update Count : 22
14//
15
16#define __cforall_thread__
17
18#include "coroutine.hfa"
19
20#include <stddef.h>
21#include <malloc.h>
22#include <errno.h>
23#include <string.h>
24#include <unistd.h>
25#include <sys/mman.h> // mprotect
26extern "C" {
27// use this define to make unwind.h play nice, definitely a hack
28#define HIDE_EXPORTS
29#include <unwind.h>
30#undef HIDE_EXPORTS
31}
32
33#include "kernel_private.hfa"
34
35#define __CFA_INVOKE_PRIVATE__
36#include "invoke.h"
37
38extern "C" {
39 void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct $coroutine *) __attribute__ ((__noreturn__));
40 static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) __attribute__ ((__noreturn__));
41 static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) {
42 abort();
43 }
44
45 extern void CtxRet( struct __stack_context_t * to ) asm ("CtxRet") __attribute__ ((__noreturn__));
46}
47
48//-----------------------------------------------------------------------------
49FORALL_DATA_INSTANCE(CoroutineCancelled, (dtype coroutine_t), (coroutine_t))
50
51forall(dtype T)
52void mark_exception(CoroutineCancelled(T) *) {}
53
54forall(dtype T)
55void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src) {
56 dst->virtual_table = src->virtual_table;
57 dst->the_coroutine = src->the_coroutine;
58 dst->the_exception = src->the_exception;
59}
60
61forall(dtype T)
62const char * msg(CoroutineCancelled(T) *) {
63 return "CoroutineCancelled(...)";
64}
65
66// This code should not be inlined. It is the error path on resume.
67forall(dtype T | is_coroutine(T))
68void __cfaehm_cancelled_coroutine( T & cor, $coroutine * desc ) {
69 verify( desc->cancellation );
70 desc->state = Cancelled;
71 exception_t * except = __cfaehm_cancellation_exception( desc->cancellation );
72
73 // TODO: Remove explitate vtable set once trac#186 is fixed.
74 CoroutineCancelled(T) except;
75 except.virtual_table = &get_exception_vtable(&except);
76 except.the_coroutine = &cor;
77 except.the_exception = except;
78 throwResume except;
79
80 except->virtual_table->free( except );
81 free( desc->cancellation );
82 desc->cancellation = 0p;
83}
84
85//-----------------------------------------------------------------------------
86// Global state variables
87
88// minimum feasible stack size in bytes
89static const size_t MinStackSize = 1000;
90extern size_t __page_size; // architecture pagesize HACK, should go in proper runtime singleton
91
92void __stack_prepare( __stack_info_t * this, size_t create_size );
93
94//-----------------------------------------------------------------------------
95// Coroutine ctors and dtors
96void ?{}( __stack_info_t & this, void * storage, size_t storageSize ) {
97 this.storage = (__stack_t *)storage;
98
99 // Did we get a piece of storage ?
100 if (this.storage || storageSize != 0) {
101 // We either got a piece of storage or the user asked for a specific size
102 // Immediately create the stack
103 // (This is slightly unintuitive that non-default sized coroutines create are eagerly created
104 // but it avoids that all coroutines carry an unnecessary size)
105 verify( storageSize != 0 );
106 __stack_prepare( &this, storageSize );
107 }
108}
109
110void ^?{}(__stack_info_t & this) {
111 bool userStack = ((intptr_t)this.storage & 0x1) != 0;
112 if ( ! userStack && this.storage ) {
113 __attribute__((may_alias)) intptr_t * istorage = (intptr_t *)&this.storage;
114 *istorage &= (intptr_t)-1;
115
116 void * storage = this.storage->limit;
117 __cfaabi_dbg_debug_do(
118 storage = (char*)(storage) - __page_size;
119 if ( mprotect( storage, __page_size, PROT_READ | PROT_WRITE ) == -1 ) {
120 abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
121 }
122 );
123 __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
124 free( storage );
125 }
126}
127
128void ?{}( $coroutine & this, const char name[], void * storage, size_t storageSize ) with( this ) {
129 (this.context){0p, 0p};
130 (this.stack){storage, storageSize};
131 this.name = name;
132 state = Start;
133 starter = 0p;
134 last = 0p;
135 cancellation = 0p;
136}
137
138void ^?{}($coroutine& this) {
139 if(this.state != Halted && this.state != Start && this.state != Primed) {
140 $coroutine * src = TL_GET( this_thread )->curr_cor;
141 $coroutine * dst = &this;
142
143 struct _Unwind_Exception storage;
144 storage.exception_class = -1;
145 storage.exception_cleanup = _CtxCoroutine_UnwindCleanup;
146 this.cancellation = &storage;
147 this.last = src;
148
149 // not resuming self ?
150 if ( src == dst ) {
151 abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src );
152 }
153
154 $ctx_switch( src, dst );
155 }
156}
157
158// Part of the Public API
159// Not inline since only ever called once per coroutine
160forall(dtype T | is_coroutine(T))
161void prime(T& cor) {
162 $coroutine* this = get_coroutine(cor);
163 assert(this->state == Start);
164
165 this->state = Primed;
166 resume(cor);
167}
168
169[void *, size_t] __stack_alloc( size_t storageSize ) {
170 const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
171 assert(__page_size != 0l);
172 size_t size = libCeiling( storageSize, 16 ) + stack_data_size;
173
174 // If we are running debug, we also need to allocate a guardpage to catch stack overflows.
175 void * storage;
176 __cfaabi_dbg_debug_do(
177 storage = memalign( __page_size, size + __page_size );
178 );
179 __cfaabi_dbg_no_debug_do(
180 storage = (void*)malloc(size);
181 );
182
183 __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size);
184 __cfaabi_dbg_debug_do(
185 if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
186 abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) );
187 }
188 storage = (void *)(((intptr_t)storage) + __page_size);
189 );
190
191 verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul );
192 return [storage, size];
193}
194
195void __stack_prepare( __stack_info_t * this, size_t create_size ) {
196 const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
197 bool userStack;
198 void * storage;
199 size_t size;
200 if ( !this->storage ) {
201 userStack = false;
202 [storage, size] = __stack_alloc( create_size );
203 } else {
204 userStack = true;
205 __cfaabi_dbg_print_safe("Kernel : stack obj %p using user stack %p(%zd bytes)\n", this, this->storage, (intptr_t)this->storage->limit - (intptr_t)this->storage->base);
206
207 // The stack must be aligned, advance the pointer to the next align data
208 storage = (void*)libCeiling( (intptr_t)this->storage, libAlign());
209
210 // The size needs to be shrinked to fit all the extra data structure and be aligned
211 ptrdiff_t diff = (intptr_t)storage - (intptr_t)this->storage;
212 size = libFloor(create_size - stack_data_size - diff, libAlign());
213 } // if
214 assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize );
215
216 this->storage = (__stack_t *)((intptr_t)storage + size);
217 this->storage->limit = storage;
218 this->storage->base = (void*)((intptr_t)storage + size);
219 this->storage->exception_context.top_resume = 0p;
220 this->storage->exception_context.current_exception = 0p;
221 __attribute__((may_alias)) intptr_t * istorage = (intptr_t*)&this->storage;
222 *istorage |= userStack ? 0x1 : 0x0;
223}
224
225// We need to call suspend from invoke.c, so we expose this wrapper that
226// is not inline (We can't inline Cforall in C)
227extern "C" {
228 void __cfactx_cor_leave( struct $coroutine * src ) {
229 $coroutine * starter = src->cancellation != 0 ? src->last : src->starter;
230
231 src->state = Halted;
232
233 assertf( starter != 0,
234 "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"
235 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
236 src->name, src );
237 assertf( starter->state != Halted,
238 "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"
239 "Possible cause is terminated coroutine's main routine has already returned.",
240 src->name, src, starter->name, starter );
241
242 $ctx_switch( src, starter );
243 }
244
245 struct $coroutine * __cfactx_cor_finish(void) {
246 struct $coroutine * cor = kernelTLS.this_thread->curr_cor;
247
248 if(cor->state == Primed) {
249 __cfactx_suspend();
250 }
251
252 cor->state = Active;
253
254 return cor;
255 }
256}
257
258// Local Variables: //
259// mode: c //
260// tab-width: 4 //
261// End: //
Note: See TracBrowser for help on using the repository browser.