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