source: libcfa/src/concurrency/coroutine.cfa@ 3318dff

Last change on this file since 3318dff was 3318dff, checked in by caparsons <caparson@…>, 2 years ago

fixed non-local ehm issue and added no arg resumer routine

  • Property mode set to 100644
File size: 12.7 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.c --
[6a3d2e7]8//
9// Author : Thierry Delisle
10// Created On : Mon Nov 28 12:27:26 2016
[6b0b624]11// Last Modified By : Peter A. Buhr
[f5f2768]12// Last Modified On : Thu Feb 16 15:34:46 2023
13// Update Count : 24
[6a3d2e7]14//
15
[2026bb6]16#define __cforall_thread__
17
[58b6d1b]18#include "coroutine.hfa"
[bd98b58]19
[6a3d2e7]20#include <stddef.h>
21#include <malloc.h>
22#include <errno.h>
23#include <string.h>
24#include <unistd.h>
[ada0246d]25#include <sys/mman.h> // mprotect
[76e069f]26#include <unwind.h>
[6a3d2e7]27
[708ae38]28#include "kernel/private.hfa"
[c960331]29#include "exception.hfa"
[3318dff]30#include "exception.h"
[bfcf6b9]31#include "math.hfa"
[6a3d2e7]32
[97229d6]33#define CFA_COROUTINE_USE_MMAP 0
34
[6a3d2e7]35#define __CFA_INVOKE_PRIVATE__
36#include "invoke.h"
37
[76e069f]38extern "C" {
[e84ab3d]39 void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine$ *) __attribute__ ((__noreturn__));
[b2f6113]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 }
[f019069]44
45 extern void CtxRet( struct __stack_context_t * to ) asm ("CtxRet") __attribute__ ((__noreturn__));
[76e069f]46}
47
[1c01c58]48//-----------------------------------------------------------------------------
[fd54fef]49forall(T &)
[c18bf9e]50void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src) libcfa_public {
[342be43]51 dst->virtual_table = src->virtual_table;
[1c01c58]52 dst->the_coroutine = src->the_coroutine;
53 dst->the_exception = src->the_exception;
54}
55
[fd54fef]56forall(T &)
[c18bf9e]57const char * msg(CoroutineCancelled(T) *) libcfa_public {
[1c01c58]58 return "CoroutineCancelled(...)";
59}
60
61// This code should not be inlined. It is the error path on resume.
[fd54fef]62forall(T & | is_coroutine(T))
[b583113]63void __cfaehm_cancelled_coroutine(
[c3b9d639]64 T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled(T)) ) libcfa_public {
[1c01c58]65 verify( desc->cancellation );
66 desc->state = Cancelled;
[342be43]67 exception_t * except = __cfaehm_cancellation_exception( desc->cancellation );
[1c01c58]68
[69c5c00]69 // TODO: Remove explitate vtable set once trac#186 is fixed.
[b583113]70 CoroutineCancelled(T) except;
71 except.virtual_table = &_default_vtable;
[1c01c58]72 except.the_coroutine = &cor;
73 except.the_exception = except;
[ecfd758]74 // Why does this need a cast?
[b583113]75 throwResume (CoroutineCancelled(T) &)except;
[1c01c58]76
77 except->virtual_table->free( except );
78 free( desc->cancellation );
79 desc->cancellation = 0p;
80}
81
[6a3d2e7]82//-----------------------------------------------------------------------------
83// Global state variables
84
85// minimum feasible stack size in bytes
[290553a]86static const size_t MinStackSize = 1000;
[b2f6113]87extern size_t __page_size; // architecture pagesize HACK, should go in proper runtime singleton
[dd92fe9]88extern int __map_prot;
[b2f6113]89
90void __stack_prepare( __stack_info_t * this, size_t create_size );
[c18bf9e]91static void __stack_clean ( __stack_info_t * this );
[6a3d2e7]92
93//-----------------------------------------------------------------------------
94// Coroutine ctors and dtors
[b2f6113]95void ?{}( __stack_info_t & this, void * storage, size_t storageSize ) {
96 this.storage = (__stack_t *)storage;
97
98 // Did we get a piece of storage ?
99 if (this.storage || storageSize != 0) {
100 // We either got a piece of storage or the user asked for a specific size
101 // Immediately create the stack
102 // (This is slightly unintuitive that non-default sized coroutines create are eagerly created
103 // but it avoids that all coroutines carry an unnecessary size)
104 verify( storageSize != 0 );
105 __stack_prepare( &this, storageSize );
106 }
[6a3d2e7]107}
108
[b2f6113]109void ^?{}(__stack_info_t & this) {
[8c01e1b]110 bool userStack = ((intptr_t)this.storage & 0x1) != 0;
111 if ( ! userStack && this.storage ) {
[bfcf6b9]112 __stack_clean( &this );
[b2f6113]113 }
[6a3d2e7]114}
115
[c18bf9e]116void ?{}( coroutine$ & this, const char name[], void * storage, size_t storageSize ) libcfa_public with( this ) {
[524627e]117 (this.context){0p, 0p};
[b2f6113]118 (this.stack){storage, storageSize};
119 this.name = name;
120 state = Start;
[524627e]121 starter = 0p;
122 last = 0p;
123 cancellation = 0p;
[2fe64ba]124 ehm_state.ehm_buffer{};
125 ehm_state.buffer_lock{};
126 ehm_state.ehm_enabled = false;
[6a3d2e7]127}
128
[c18bf9e]129void ^?{}(coroutine$& this) libcfa_public {
[3623f9d]130 if(this.state != Halted && this.state != Start && this.state != Primed) {
[e84ab3d]131 coroutine$ * src = active_coroutine();
132 coroutine$ * dst = &this;
[b2f6113]133
134 struct _Unwind_Exception storage;
135 storage.exception_class = -1;
136 storage.exception_cleanup = _CtxCoroutine_UnwindCleanup;
137 this.cancellation = &storage;
138 this.last = src;
139
140 // not resuming self ?
141 if ( src == dst ) {
142 abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src );
143 }
144
[ac2b598]145 $ctx_switch( src, dst );
[b2f6113]146 }
[76e069f]147}
[6a3d2e7]148
149// Part of the Public API
150// Not inline since only ever called once per coroutine
[c3b9d639]151forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); })
[c18bf9e]152void prime(T& cor) libcfa_public {
[e84ab3d]153 coroutine$* this = get_coroutine(cor);
[b2f6113]154 assert(this->state == Start);
[6a3d2e7]155
[b2f6113]156 this->state = Primed;
157 resume(cor);
[6a3d2e7]158}
159
[c18bf9e]160static [void *, size_t] __stack_alloc( size_t storageSize ) {
[0030ada3]161 const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
[b2f6113]162 assert(__page_size != 0l);
163 size_t size = libCeiling( storageSize, 16 ) + stack_data_size;
[bfcf6b9]164 size = ceiling(size, __page_size);
[b2f6113]165
166 // If we are running debug, we also need to allocate a guardpage to catch stack overflows.
167 void * storage;
[97229d6]168 #if CFA_COROUTINE_USE_MMAP
169 storage = mmap(0p, size + __page_size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
170 if(storage == ((void*)-1)) {
171 abort( "coroutine stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
172 }
173 if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
174 abort( "coroutine stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
175 } // if
176 storage = (void *)(((intptr_t)storage) + __page_size);
177 #else
178 __cfaabi_dbg_debug_do(
179 storage = memalign( __page_size, size + __page_size );
180 );
181 __cfaabi_dbg_no_debug_do(
182 storage = (void*)malloc(size);
183 );
184
185 __cfaabi_dbg_debug_do(
186 if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
187 abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) );
188 }
189 storage = (void *)(((intptr_t)storage) + __page_size);
190 );
191 #endif
192 __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size);
[b2f6113]193
194 verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul );
195 return [storage, size];
196}
[6a3d2e7]197
[c18bf9e]198static void __stack_clean ( __stack_info_t * this ) {
[bfcf6b9]199 void * storage = this->storage->limit;
200
[97229d6]201 #if CFA_COROUTINE_USE_MMAP
[85ac70e8]202 size_t size = ((intptr_t)this->storage->base) - ((intptr_t)this->storage->limit) + sizeof(__stack_t);
[97229d6]203 storage = (void *)(((intptr_t)storage) - __page_size);
204 if(munmap(storage, size + __page_size) == -1) {
205 abort( "coroutine stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
206 }
207 #else
208 __cfaabi_dbg_debug_do(
209 storage = (char*)(storage) - __page_size;
[dd92fe9]210 if ( mprotect( storage, __page_size, __map_prot ) == -1 ) {
[97229d6]211 abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
212 }
213 );
214
215 free( storage );
216 #endif
217 __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
[bfcf6b9]218}
219
[c18bf9e]220void __stack_prepare( __stack_info_t * this, size_t create_size ) libcfa_public {
[0030ada3]221 const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
[b2f6113]222 bool userStack;
223 void * storage;
224 size_t size;
225 if ( !this->storage ) {
226 userStack = false;
227 [storage, size] = __stack_alloc( create_size );
228 } else {
229 userStack = true;
[69a61d2]230 __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);
[b2f6113]231
232 // The stack must be aligned, advance the pointer to the next align data
233 storage = (void*)libCeiling( (intptr_t)this->storage, libAlign());
234
235 // The size needs to be shrinked to fit all the extra data structure and be aligned
236 ptrdiff_t diff = (intptr_t)storage - (intptr_t)this->storage;
237 size = libFloor(create_size - stack_data_size - diff, libAlign());
238 } // if
[9b0c3ec5]239 assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize );
[b2f6113]240
[bfcf6b9]241 this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t));
[b2f6113]242 this->storage->limit = storage;
[bfcf6b9]243 this->storage->base = (void*)((intptr_t)storage + size - sizeof(__stack_t));
[1c01c58]244 this->storage->exception_context.top_resume = 0p;
245 this->storage->exception_context.current_exception = 0p;
[ffe2fad]246 __attribute__((may_alias)) intptr_t * istorage = (intptr_t*)&this->storage;
247 *istorage |= userStack ? 0x1 : 0x0;
[6a3d2e7]248}
249
[0c92c9f]250// We need to call suspend from invoke.c, so we expose this wrapper that
251// is not inline (We can't inline Cforall in C)
252extern "C" {
[e84ab3d]253 void __cfactx_cor_leave( struct coroutine$ * src ) {
254 coroutine$ * starter = src->cancellation != 0 ? src->last : src->starter;
[b2f6113]255
256 src->state = Halted;
257
258 assertf( starter != 0,
259 "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"
260 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
261 src->name, src );
262 assertf( starter->state != Halted,
263 "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"
264 "Possible cause is terminated coroutine's main routine has already returned.",
265 src->name, src, starter->name, starter );
266
[ac2b598]267 $ctx_switch( src, starter );
[b2f6113]268 }
[09f357ec]269
[e84ab3d]270 struct coroutine$ * __cfactx_cor_finish(void) {
271 struct coroutine$ * cor = active_coroutine();
[09f357ec]272
[ab5baab]273 // get the active thread once
[e84ab3d]274 thread$ * athrd = active_thread();
[ab5baab]275
276 /* paranoid */ verify( athrd->corctx_flag );
277 athrd->corctx_flag = false;
278
[09f357ec]279 if(cor->state == Primed) {
[427854b]280 __cfactx_suspend();
[09f357ec]281 }
282
283 cor->state = Active;
284
285 return cor;
286 }
[0c92c9f]287}
288
[2fe64ba]289
290////////////////////////////////////////////////////////////////////////////////////////////////////
291// non local ehm routines
292
293// helper for popping from coroutine's ehm buffer
294inline nonlocal_exception * pop_ehm_head( coroutine$ * this ) {
295 lock( this->ehm_state.buffer_lock __cfaabi_dbg_ctx2 );
296 nonlocal_exception * nl_ex = pop_head( this->ehm_state.ehm_buffer );
297 unlock( this->ehm_state.buffer_lock );
298 return nl_ex;
299}
300
[3318dff]301void defaultResumeAtHandler( exception_t * except ) {
302 __cfaehm_allocate_exception( except );
303 free( except );
304 __cfaehm_begin_unwind( (void(*)(exception_t *))defaultTerminationHandler );
305}
306
[c34bb1f]307bool poll( coroutine$ * cor ) libcfa_public {
308 nonlocal_exception * nl_ex = pop_ehm_head( cor );
309
310 // if no exceptions return false
311 if ( nl_ex == 0p ) return false;
312
313 // otherwise loop and throwResume all pending exceptions
314 while ( nl_ex != 0p ){
315 exception_t * ex = nl_ex->the_exception;
316 free( nl_ex );
[3318dff]317 __cfaehm_throw_resume( ex, defaultResumeAtHandler );
318
319 // only reached if resumption handled. other dealloc handled in defaultResumeAtHandler
320 free( ex );
[c34bb1f]321 nl_ex = pop_ehm_head( cor );
322 }
323
324 return true;
325}
326
327bool poll() libcfa_public { return poll( active_coroutine() ); }
[3318dff]328coroutine$ * resumer() libcfa_public { return active_coroutine()->last; }
[c34bb1f]329
[2fe64ba]330// user facing ehm operations
331forall(T & | is_coroutine(T)) {
332 // enable/disable non-local exceptions
333 void enable_ehm( T & cor ) libcfa_public { get_coroutine( cor )->ehm_state.ehm_enabled = true; }
334 void disable_ehm( T & cor ) libcfa_public { get_coroutine( cor )->ehm_state.ehm_enabled = false; }
335
336 // poll for non-local exceptions
[c34bb1f]337 bool poll( T & cor ) libcfa_public { return poll( get_coroutine( cor ) ); }
[2fe64ba]338
339 // poll iff nonlocal ehm is enabled
340 bool checked_poll( T & cor ) libcfa_public { return get_coroutine( cor )->ehm_state.ehm_enabled ? poll( cor ) : false; }
[c3e510b]341
342 coroutine$ * resumer( T & cor ) libcfa_public { return get_coroutine( cor )->last; }
[2fe64ba]343}
344
345// resume non local exception at receiver (i.e. enqueue in ehm buffer)
[3318dff]346forall(exceptT *, T & | ehm_resume_at( exceptT, T ))
[2fe64ba]347void resumeAt( T & receiver, exceptT & ex ) libcfa_public {
348 coroutine$ * cor = get_coroutine( receiver );
349 nonlocal_exception * nl_ex = alloc();
[3318dff]350 exceptT * ex_copy = alloc();
351 memcpy( ex_copy, &ex, sizeof(exceptT) );
352 (*nl_ex){ (exception_t *)ex_copy };
[2fe64ba]353 lock( cor->ehm_state.buffer_lock __cfaabi_dbg_ctx2 );
354 append( cor->ehm_state.ehm_buffer, nl_ex );
355 unlock( cor->ehm_state.buffer_lock );
356}
357
[3318dff]358forall(exceptT * | { void $throwResume(exceptT &); })
[c3e510b]359void resumeAt( coroutine$ * receiver, exceptT & ex ) libcfa_public {
360 nonlocal_exception * nl_ex = alloc();
[3318dff]361 exceptT * ex_copy = alloc();
362 memcpy( ex_copy, &ex, sizeof(exceptT) );
363 (*nl_ex){ (exception_t *)ex_copy };
[c3e510b]364 lock( receiver->ehm_state.buffer_lock __cfaabi_dbg_ctx2 );
365 append( receiver->ehm_state.ehm_buffer, nl_ex );
366 unlock( receiver->ehm_state.buffer_lock );
367}
368
[6a3d2e7]369// Local Variables: //
370// mode: c //
371// tab-width: 4 //
[4aa2fb2]372// End: //
Note: See TracBrowser for help on using the repository browser.