source: libcfa/src/concurrency/coroutine.cfa @ ffe2fad

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ffe2fad was ffe2fad, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fixed several warnings in libcfa

  • Property mode set to 100644
File size: 6.9 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
[b10affd]12// Last Modified On : Fri Mar 30 17:20:57 2018
13// Update Count     : 9
[6a3d2e7]14//
15
[58b6d1b]16#include "coroutine.hfa"
[bd98b58]17
[6a3d2e7]18extern "C" {
19#include <stddef.h>
20#include <malloc.h>
21#include <errno.h>
22#include <string.h>
23#include <unistd.h>
[76e069f]24// use this define to make unwind.h play nice, definetely a hack
25#define HIDE_EXPORTS
26#include <unwind.h>
27#undef HIDE_EXPORTS
[6a3d2e7]28#include <sys/mman.h>
29}
30
[73abe95]31#include "kernel_private.hfa"
[6a3d2e7]32
33#define __CFA_INVOKE_PRIVATE__
34#include "invoke.h"
35
[76e069f]36extern "C" {
[b2f6113]37        void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc *) __attribute__ ((__noreturn__));
38        static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) __attribute__ ((__noreturn__));
39        static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) {
40                abort();
41        }
[76e069f]42}
43
[6a3d2e7]44//-----------------------------------------------------------------------------
45// Global state variables
46
47// minimum feasible stack size in bytes
48#define MinStackSize 1000
[b2f6113]49extern size_t __page_size;                              // architecture pagesize HACK, should go in proper runtime singleton
50
51void __stack_prepare( __stack_info_t * this, size_t create_size );
[6a3d2e7]52
53//-----------------------------------------------------------------------------
54// Coroutine ctors and dtors
[b2f6113]55void ?{}( __stack_info_t & this, void * storage, size_t storageSize ) {
56        this.storage   = (__stack_t *)storage;
57
58        // Did we get a piece of storage ?
59        if (this.storage || storageSize != 0) {
60                // We either got a piece of storage or the user asked for a specific size
61                // Immediately create the stack
62                // (This is slightly unintuitive that non-default sized coroutines create are eagerly created
63                // but it avoids that all coroutines carry an unnecessary size)
64                verify( storageSize != 0 );
65                __stack_prepare( &this, storageSize );
66        }
[6a3d2e7]67}
68
[b2f6113]69void ^?{}(__stack_info_t & this) {
[8c01e1b]70        bool userStack = ((intptr_t)this.storage & 0x1) != 0;
71        if ( ! userStack && this.storage ) {
[ffe2fad]72                __attribute__((may_alias)) intptr_t * istorage = (intptr_t *)&this.storage;
73                *istorage &= (intptr_t)-1;
74
[8c01e1b]75                void * storage = this.storage->limit;
[b2f6113]76                __cfaabi_dbg_debug_do(
77                        storage = (char*)(storage) - __page_size;
78                        if ( mprotect( storage, __page_size, PROT_READ | PROT_WRITE ) == -1 ) {
79                                abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
80                        }
81                );
82                __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
83                free( storage );
84        }
[6a3d2e7]85}
86
[de6319f]87void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize ) with( this ) {
[69a61d2]88        (this.context){NULL, NULL};
[b2f6113]89        (this.stack){storage, storageSize};
90        this.name = name;
91        state = Start;
92        starter = NULL;
93        last = NULL;
94        cancellation = NULL;
[6a3d2e7]95}
96
[76e069f]97void ^?{}(coroutine_desc& this) {
[b2f6113]98        if(this.state != Halted && this.state != Start) {
99                coroutine_desc * src = TL_GET( this_thread )->curr_cor;
100                coroutine_desc * dst = &this;
101
102                struct _Unwind_Exception storage;
103                storage.exception_class = -1;
104                storage.exception_cleanup = _CtxCoroutine_UnwindCleanup;
105                this.cancellation = &storage;
106                this.last = src;
107
108                // not resuming self ?
109                if ( src == dst ) {
110                        abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src );
111                }
112
113                CoroutineCtxSwitch( src, dst );
114        }
[76e069f]115}
[6a3d2e7]116
117// Part of the Public API
118// Not inline since only ever called once per coroutine
119forall(dtype T | is_coroutine(T))
[83a071f9]120void prime(T& cor) {
[b2f6113]121        coroutine_desc* this = get_coroutine(cor);
122        assert(this->state == Start);
[6a3d2e7]123
[b2f6113]124        this->state = Primed;
125        resume(cor);
[6a3d2e7]126}
127
[b2f6113]128[void *, size_t] __stack_alloc( size_t storageSize ) {
129        static const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
130        assert(__page_size != 0l);
131        size_t size = libCeiling( storageSize, 16 ) + stack_data_size;
132
133        // If we are running debug, we also need to allocate a guardpage to catch stack overflows.
134        void * storage;
135        __cfaabi_dbg_debug_do(
136                storage = memalign( __page_size, size + __page_size );
137        );
138        __cfaabi_dbg_no_debug_do(
139                storage = (void*)malloc(size);
140        );
141
142        __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size);
143        __cfaabi_dbg_debug_do(
144                if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
145                        abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) );
146                }
147                storage = (void *)(((intptr_t)storage) + __page_size);
148        );
149
150        verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul );
151        return [storage, size];
152}
[6a3d2e7]153
[b2f6113]154void __stack_prepare( __stack_info_t * this, size_t create_size ) {
155        static const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
156        bool userStack;
157        void * storage;
158        size_t size;
159        if ( !this->storage ) {
160                userStack = false;
161                [storage, size] = __stack_alloc( create_size );
162        } else {
163                userStack = true;
[69a61d2]164                __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]165
166                // The stack must be aligned, advance the pointer to the next align data
167                storage = (void*)libCeiling( (intptr_t)this->storage, libAlign());
168
169                // The size needs to be shrinked to fit all the extra data structure and be aligned
170                ptrdiff_t diff = (intptr_t)storage - (intptr_t)this->storage;
171                size = libFloor(create_size - stack_data_size - diff, libAlign());
172        } // if
173        assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %d bytes for a stack.", size, MinStackSize );
174
175        this->storage = (__stack_t *)((intptr_t)storage + size);
176        this->storage->limit = storage;
177        this->storage->base  = (void*)((intptr_t)storage + size);
[ffe2fad]178        __attribute__((may_alias)) intptr_t * istorage = (intptr_t*)&this->storage;
179        *istorage |= userStack ? 0x1 : 0x0;
[6a3d2e7]180}
181
[0c92c9f]182// We need to call suspend from invoke.c, so we expose this wrapper that
183// is not inline (We can't inline Cforall in C)
184extern "C" {
[b2f6113]185        void __suspend_internal(void) {
186                suspend();
187        }
188
189        void __leave_coroutine( coroutine_desc * src ) {
190                coroutine_desc * starter = src->cancellation != 0 ? src->last : src->starter;
191
192                src->state = Halted;
193
194                assertf( starter != 0,
195                        "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"
196                        "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
197                        src->name, src );
198                assertf( starter->state != Halted,
199                        "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"
200                        "Possible cause is terminated coroutine's main routine has already returned.",
201                        src->name, src, starter->name, starter );
202
203                CoroutineCtxSwitch( src, starter );
204        }
[0c92c9f]205}
206
[6a3d2e7]207// Local Variables: //
208// mode: c //
209// tab-width: 4 //
[4aa2fb2]210// End: //
Note: See TracBrowser for help on using the repository browser.