source: libcfa/src/concurrency/coroutine.cfa @ 69c5c00

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since 69c5c00 was 69c5c00, checked in by Andrew Beach <ajbeach@…>, 14 months ago

Rework exceptions mark_exception -> get_exception_vtable and the needed follow up.

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