[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" |
---|
[6a3d2e7] | 30 | |
---|
| 31 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 32 | #include "invoke.h" |
---|
| 33 | |
---|
[76e069f] | 34 | extern "C" { |
---|
[ac2b598] | 35 | void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct $coroutine *) __attribute__ ((__noreturn__)); |
---|
[b2f6113] | 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 | } |
---|
[f019069] | 40 | |
---|
| 41 | extern void CtxRet( struct __stack_context_t * to ) asm ("CtxRet") __attribute__ ((__noreturn__)); |
---|
[76e069f] | 42 | } |
---|
| 43 | |
---|
[1c01c58] | 44 | //----------------------------------------------------------------------------- |
---|
[69c5c00] | 45 | FORALL_DATA_INSTANCE(CoroutineCancelled, (dtype coroutine_t), (coroutine_t)) |
---|
[1c01c58] | 46 | |
---|
| 47 | forall(dtype T) |
---|
| 48 | void mark_exception(CoroutineCancelled(T) *) {} |
---|
| 49 | |
---|
[69c5c00] | 50 | forall(dtype T) |
---|
[1c01c58] | 51 | void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src) { |
---|
[342be43] | 52 | dst->virtual_table = src->virtual_table; |
---|
[1c01c58] | 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; |
---|
[342be43] | 67 | exception_t * except = __cfaehm_cancellation_exception( desc->cancellation ); |
---|
[1c01c58] | 68 | |
---|
[69c5c00] | 69 | // TODO: Remove explitate vtable set once trac#186 is fixed. |
---|
[1c01c58] | 70 | CoroutineCancelled(T) except; |
---|
[69c5c00] | 71 | except.virtual_table = &get_exception_vtable(&except); |
---|
[1c01c58] | 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 | |
---|
[6a3d2e7] | 81 | //----------------------------------------------------------------------------- |
---|
| 82 | // Global state variables |
---|
| 83 | |
---|
| 84 | // minimum feasible stack size in bytes |
---|
[290553a] | 85 | static const size_t MinStackSize = 1000; |
---|
[b2f6113] | 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 ); |
---|
[6a3d2e7] | 89 | |
---|
| 90 | //----------------------------------------------------------------------------- |
---|
| 91 | // Coroutine ctors and dtors |
---|
[b2f6113] | 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 | } |
---|
[6a3d2e7] | 104 | } |
---|
| 105 | |
---|
[b2f6113] | 106 | void ^?{}(__stack_info_t & this) { |
---|
[8c01e1b] | 107 | bool userStack = ((intptr_t)this.storage & 0x1) != 0; |
---|
| 108 | if ( ! userStack && this.storage ) { |
---|
[ffe2fad] | 109 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t *)&this.storage; |
---|
| 110 | *istorage &= (intptr_t)-1; |
---|
| 111 | |
---|
[8c01e1b] | 112 | void * storage = this.storage->limit; |
---|
[b2f6113] | 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 | } |
---|
[6a3d2e7] | 122 | } |
---|
| 123 | |
---|
[ac2b598] | 124 | void ?{}( $coroutine & this, const char name[], void * storage, size_t storageSize ) with( this ) { |
---|
[524627e] | 125 | (this.context){0p, 0p}; |
---|
[b2f6113] | 126 | (this.stack){storage, storageSize}; |
---|
| 127 | this.name = name; |
---|
| 128 | state = Start; |
---|
[524627e] | 129 | starter = 0p; |
---|
| 130 | last = 0p; |
---|
| 131 | cancellation = 0p; |
---|
[6a3d2e7] | 132 | } |
---|
| 133 | |
---|
[ac2b598] | 134 | void ^?{}($coroutine& this) { |
---|
[3623f9d] | 135 | if(this.state != Halted && this.state != Start && this.state != Primed) { |
---|
[be73f30] | 136 | $coroutine * src = active_coroutine(); |
---|
[ac2b598] | 137 | $coroutine * dst = &this; |
---|
[b2f6113] | 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 | |
---|
[ac2b598] | 150 | $ctx_switch( src, dst ); |
---|
[b2f6113] | 151 | } |
---|
[76e069f] | 152 | } |
---|
[6a3d2e7] | 153 | |
---|
| 154 | // Part of the Public API |
---|
| 155 | // Not inline since only ever called once per coroutine |
---|
| 156 | forall(dtype T | is_coroutine(T)) |
---|
[83a071f9] | 157 | void prime(T& cor) { |
---|
[ac2b598] | 158 | $coroutine* this = get_coroutine(cor); |
---|
[b2f6113] | 159 | assert(this->state == Start); |
---|
[6a3d2e7] | 160 | |
---|
[b2f6113] | 161 | this->state = Primed; |
---|
| 162 | resume(cor); |
---|
[6a3d2e7] | 163 | } |
---|
| 164 | |
---|
[b2f6113] | 165 | [void *, size_t] __stack_alloc( size_t storageSize ) { |
---|
[0030ada3] | 166 | const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment |
---|
[b2f6113] | 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 | } |
---|
[6a3d2e7] | 190 | |
---|
[b2f6113] | 191 | void __stack_prepare( __stack_info_t * this, size_t create_size ) { |
---|
[0030ada3] | 192 | const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment |
---|
[b2f6113] | 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; |
---|
[69a61d2] | 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); |
---|
[b2f6113] | 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 |
---|
[9b0c3ec5] | 210 | assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize ); |
---|
[b2f6113] | 211 | |
---|
| 212 | this->storage = (__stack_t *)((intptr_t)storage + size); |
---|
| 213 | this->storage->limit = storage; |
---|
| 214 | this->storage->base = (void*)((intptr_t)storage + size); |
---|
[1c01c58] | 215 | this->storage->exception_context.top_resume = 0p; |
---|
| 216 | this->storage->exception_context.current_exception = 0p; |
---|
[ffe2fad] | 217 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*)&this->storage; |
---|
| 218 | *istorage |= userStack ? 0x1 : 0x0; |
---|
[6a3d2e7] | 219 | } |
---|
| 220 | |
---|
[0c92c9f] | 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" { |
---|
[ac2b598] | 224 | void __cfactx_cor_leave( struct $coroutine * src ) { |
---|
| 225 | $coroutine * starter = src->cancellation != 0 ? src->last : src->starter; |
---|
[b2f6113] | 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 | |
---|
[ac2b598] | 238 | $ctx_switch( src, starter ); |
---|
[b2f6113] | 239 | } |
---|
[09f357ec] | 240 | |
---|
[ac2b598] | 241 | struct $coroutine * __cfactx_cor_finish(void) { |
---|
[be73f30] | 242 | struct $coroutine * cor = active_coroutine(); |
---|
[09f357ec] | 243 | |
---|
| 244 | if(cor->state == Primed) { |
---|
[427854b] | 245 | __cfactx_suspend(); |
---|
[09f357ec] | 246 | } |
---|
| 247 | |
---|
| 248 | cor->state = Active; |
---|
| 249 | |
---|
| 250 | return cor; |
---|
| 251 | } |
---|
[0c92c9f] | 252 | } |
---|
| 253 | |
---|
[6a3d2e7] | 254 | // Local Variables: // |
---|
| 255 | // mode: c // |
---|
| 256 | // tab-width: 4 // |
---|
[4aa2fb2] | 257 | // End: // |
---|