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