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