| 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 : Thu Feb 16 15:34:46 2023
 | 
|---|
| 13 | // Update Count     : 24
 | 
|---|
| 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 | #include "math.hfa"
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #define CFA_COROUTINE_USE_MMAP 0
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #define __CFA_INVOKE_PRIVATE__
 | 
|---|
| 35 | #include "invoke.h"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | extern "C" {
 | 
|---|
| 38 |         void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine$ *) __attribute__ ((__noreturn__));
 | 
|---|
| 39 |         static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) __attribute__ ((__noreturn__));
 | 
|---|
| 40 |         static void _CtxCoroutine_UnwindCleanup(_Unwind_Reason_Code, struct _Unwind_Exception *) {
 | 
|---|
| 41 |                 abort();
 | 
|---|
| 42 |         }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         extern void CtxRet( struct __stack_context_t * to ) asm ("CtxRet") __attribute__ ((__noreturn__));
 | 
|---|
| 45 | }
 | 
|---|
| 46 | 
 | 
|---|
| 47 | //-----------------------------------------------------------------------------
 | 
|---|
| 48 | forall(T &)
 | 
|---|
| 49 | void copy(CoroutineCancelled(T) * dst, CoroutineCancelled(T) * src) libcfa_public {
 | 
|---|
| 50 |         dst->virtual_table = src->virtual_table;
 | 
|---|
| 51 |         dst->the_coroutine = src->the_coroutine;
 | 
|---|
| 52 |         dst->the_exception = src->the_exception;
 | 
|---|
| 53 | }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | forall(T &)
 | 
|---|
| 56 | const char * msg(CoroutineCancelled(T) *) libcfa_public {
 | 
|---|
| 57 |         return "CoroutineCancelled(...)";
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | // This code should not be inlined. It is the error path on resume.
 | 
|---|
| 61 | forall(T & | is_coroutine(T))
 | 
|---|
| 62 | void __cfaehm_cancelled_coroutine(
 | 
|---|
| 63 |                 T & cor, coroutine$ * desc, EHM_DEFAULT_VTABLE(CoroutineCancelled(T)) ) libcfa_public {
 | 
|---|
| 64 |         verify( desc->cancellation );
 | 
|---|
| 65 |         desc->state = Cancelled;
 | 
|---|
| 66 |         exception_t * except = __cfaehm_cancellation_exception( desc->cancellation );
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         // TODO: Remove explitate vtable set once trac#186 is fixed.
 | 
|---|
| 69 |         CoroutineCancelled(T) except;
 | 
|---|
| 70 |         except.virtual_table = &_default_vtable;
 | 
|---|
| 71 |         except.the_coroutine = &cor;
 | 
|---|
| 72 |         except.the_exception = except;
 | 
|---|
| 73 |         // Why does this need a cast?
 | 
|---|
| 74 |         throwResume (CoroutineCancelled(T) &)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 | extern int __map_prot;
 | 
|---|
| 88 | 
 | 
|---|
| 89 | void __stack_prepare( __stack_info_t * this, size_t create_size );
 | 
|---|
| 90 | static void __stack_clean  ( __stack_info_t * this );
 | 
|---|
| 91 | 
 | 
|---|
| 92 | //-----------------------------------------------------------------------------
 | 
|---|
| 93 | // Coroutine ctors and dtors
 | 
|---|
| 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 |         }
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | void ^?{}(__stack_info_t & this) {
 | 
|---|
| 109 |         bool userStack = ((intptr_t)this.storage & 0x1) != 0;
 | 
|---|
| 110 |         if ( ! userStack && this.storage ) {
 | 
|---|
| 111 |                 __stack_clean( &this );
 | 
|---|
| 112 |         }
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | void ?{}( coroutine$ & this, const char name[], void * storage, size_t storageSize ) libcfa_public with( this ) {
 | 
|---|
| 116 |         (this.context){0p, 0p};
 | 
|---|
| 117 |         (this.stack){storage, storageSize};
 | 
|---|
| 118 |         this.name = name;
 | 
|---|
| 119 |         state = Start;
 | 
|---|
| 120 |         starter = 0p;
 | 
|---|
| 121 |         last = 0p;
 | 
|---|
| 122 |         cancellation = 0p;
 | 
|---|
| 123 |     ehm_state.ehm_buffer{};
 | 
|---|
| 124 |     ehm_state.buffer_lock{};
 | 
|---|
| 125 |     ehm_state.ehm_enabled = false;
 | 
|---|
| 126 | }
 | 
|---|
| 127 | 
 | 
|---|
| 128 | void ^?{}(coroutine$& this) libcfa_public {
 | 
|---|
| 129 |         if(this.state != Halted && this.state != Start && this.state != Primed) {
 | 
|---|
| 130 |                 coroutine$ * src = active_coroutine();
 | 
|---|
| 131 |                 coroutine$ * dst = &this;
 | 
|---|
| 132 | 
 | 
|---|
| 133 |                 struct _Unwind_Exception storage;
 | 
|---|
| 134 |                 storage.exception_class = -1;
 | 
|---|
| 135 |                 storage.exception_cleanup = _CtxCoroutine_UnwindCleanup;
 | 
|---|
| 136 |                 this.cancellation = &storage;
 | 
|---|
| 137 |                 this.last = src;
 | 
|---|
| 138 | 
 | 
|---|
| 139 |                 // not resuming self ?
 | 
|---|
| 140 |                 if ( src == dst ) {
 | 
|---|
| 141 |                         abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src );
 | 
|---|
| 142 |                 }
 | 
|---|
| 143 | 
 | 
|---|
| 144 |                 $ctx_switch( src, dst );
 | 
|---|
| 145 |         }
 | 
|---|
| 146 | }
 | 
|---|
| 147 | 
 | 
|---|
| 148 | // Part of the Public API
 | 
|---|
| 149 | // Not inline since only ever called once per coroutine
 | 
|---|
| 150 | forall(T & | is_coroutine(T) | { EHM_DEFAULT_VTABLE(CoroutineCancelled(T)); })
 | 
|---|
| 151 | void prime(T& cor) libcfa_public {
 | 
|---|
| 152 |         coroutine$* this = get_coroutine(cor);
 | 
|---|
| 153 |         assert(this->state == Start);
 | 
|---|
| 154 | 
 | 
|---|
| 155 |         this->state = Primed;
 | 
|---|
| 156 |         resume(cor);
 | 
|---|
| 157 | }
 | 
|---|
| 158 | 
 | 
|---|
| 159 | static [void *, size_t] __stack_alloc( size_t storageSize ) {
 | 
|---|
| 160 |         const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
 | 
|---|
| 161 |         assert(__page_size != 0l);
 | 
|---|
| 162 |         size_t size = libCeiling( storageSize, 16 ) + stack_data_size;
 | 
|---|
| 163 |         size = ceiling(size, __page_size);
 | 
|---|
| 164 | 
 | 
|---|
| 165 |         // If we are running debug, we also need to allocate a guardpage to catch stack overflows.
 | 
|---|
| 166 |         void * storage;
 | 
|---|
| 167 |         #if CFA_COROUTINE_USE_MMAP
 | 
|---|
| 168 |                 storage = mmap(0p, size + __page_size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
 | 
|---|
| 169 |                 if(storage == ((void*)-1)) {
 | 
|---|
| 170 |                         abort( "coroutine stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
 | 
|---|
| 171 |                 }
 | 
|---|
| 172 |                 if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) {
 | 
|---|
| 173 |                         abort( "coroutine stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
 | 
|---|
| 174 |                 } // if
 | 
|---|
| 175 |                 storage = (void *)(((intptr_t)storage) + __page_size);
 | 
|---|
| 176 |         #else
 | 
|---|
| 177 |                 __cfaabi_dbg_debug_do(
 | 
|---|
| 178 |                         storage = memalign( __page_size, size + __page_size );
 | 
|---|
| 179 |                 );
 | 
|---|
| 180 |                 __cfaabi_dbg_no_debug_do(
 | 
|---|
| 181 |                         storage = (void*)malloc(size);
 | 
|---|
| 182 |                 );
 | 
|---|
| 183 | 
 | 
|---|
| 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 |         #endif
 | 
|---|
| 191 |         __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size);
 | 
|---|
| 192 | 
 | 
|---|
| 193 |         verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul );
 | 
|---|
| 194 |         return [storage, size];
 | 
|---|
| 195 | }
 | 
|---|
| 196 | 
 | 
|---|
| 197 | static void __stack_clean  ( __stack_info_t * this ) {
 | 
|---|
| 198 |         void * storage = this->storage->limit;
 | 
|---|
| 199 | 
 | 
|---|
| 200 |         #if CFA_COROUTINE_USE_MMAP
 | 
|---|
| 201 |                 size_t size = ((intptr_t)this->storage->base) - ((intptr_t)this->storage->limit) + sizeof(__stack_t);
 | 
|---|
| 202 |                 storage = (void *)(((intptr_t)storage) - __page_size);
 | 
|---|
| 203 |                 if(munmap(storage, size + __page_size) == -1) {
 | 
|---|
| 204 |                         abort( "coroutine stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
 | 
|---|
| 205 |                 }
 | 
|---|
| 206 |         #else
 | 
|---|
| 207 |                 __cfaabi_dbg_debug_do(
 | 
|---|
| 208 |                         storage = (char*)(storage) - __page_size;
 | 
|---|
| 209 |                         if ( mprotect( storage, __page_size, __map_prot ) == -1 ) {
 | 
|---|
| 210 |                                 abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
 | 
|---|
| 211 |                         }
 | 
|---|
| 212 |                 );
 | 
|---|
| 213 | 
 | 
|---|
| 214 |                 free( storage );
 | 
|---|
| 215 |         #endif
 | 
|---|
| 216 |         __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage);
 | 
|---|
| 217 | }
 | 
|---|
| 218 | 
 | 
|---|
| 219 | void __stack_prepare( __stack_info_t * this, size_t create_size ) libcfa_public {
 | 
|---|
| 220 |         const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment
 | 
|---|
| 221 |         bool userStack;
 | 
|---|
| 222 |         void * storage;
 | 
|---|
| 223 |         size_t size;
 | 
|---|
| 224 |         if ( !this->storage ) {
 | 
|---|
| 225 |                 userStack = false;
 | 
|---|
| 226 |                 [storage, size] = __stack_alloc( create_size );
 | 
|---|
| 227 |         } else {
 | 
|---|
| 228 |                 userStack = true;
 | 
|---|
| 229 |                 __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);
 | 
|---|
| 230 | 
 | 
|---|
| 231 |                 // The stack must be aligned, advance the pointer to the next align data
 | 
|---|
| 232 |                 storage = (void*)libCeiling( (intptr_t)this->storage, libAlign());
 | 
|---|
| 233 | 
 | 
|---|
| 234 |                 // The size needs to be shrinked to fit all the extra data structure and be aligned
 | 
|---|
| 235 |                 ptrdiff_t diff = (intptr_t)storage - (intptr_t)this->storage;
 | 
|---|
| 236 |                 size = libFloor(create_size - stack_data_size - diff, libAlign());
 | 
|---|
| 237 |         } // if
 | 
|---|
| 238 |         assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize );
 | 
|---|
| 239 | 
 | 
|---|
| 240 |         this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t));
 | 
|---|
| 241 |         this->storage->limit = storage;
 | 
|---|
| 242 |         this->storage->base  = (void*)((intptr_t)storage + size - sizeof(__stack_t));
 | 
|---|
| 243 |         this->storage->exception_context.top_resume = 0p;
 | 
|---|
| 244 |         this->storage->exception_context.current_exception = 0p;
 | 
|---|
| 245 |         __attribute__((may_alias)) intptr_t * istorage = (intptr_t*)&this->storage;
 | 
|---|
| 246 |         *istorage |= userStack ? 0x1 : 0x0;
 | 
|---|
| 247 | }
 | 
|---|
| 248 | 
 | 
|---|
| 249 | // We need to call suspend from invoke.c, so we expose this wrapper that
 | 
|---|
| 250 | // is not inline (We can't inline Cforall in C)
 | 
|---|
| 251 | extern "C" {
 | 
|---|
| 252 |         void __cfactx_cor_leave( struct coroutine$ * src ) {
 | 
|---|
| 253 |                 coroutine$ * starter = src->cancellation != 0 ? src->last : src->starter;
 | 
|---|
| 254 | 
 | 
|---|
| 255 |                 src->state = Halted;
 | 
|---|
| 256 | 
 | 
|---|
| 257 |                 assertf( starter != 0,
 | 
|---|
| 258 |                         "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"
 | 
|---|
| 259 |                         "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",
 | 
|---|
| 260 |                         src->name, src );
 | 
|---|
| 261 |                 assertf( starter->state != Halted,
 | 
|---|
| 262 |                         "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"
 | 
|---|
| 263 |                         "Possible cause is terminated coroutine's main routine has already returned.",
 | 
|---|
| 264 |                         src->name, src, starter->name, starter );
 | 
|---|
| 265 | 
 | 
|---|
| 266 |                 $ctx_switch( src, starter );
 | 
|---|
| 267 |         }
 | 
|---|
| 268 | 
 | 
|---|
| 269 |         struct coroutine$ * __cfactx_cor_finish(void) {
 | 
|---|
| 270 |                 struct coroutine$ * cor = active_coroutine();
 | 
|---|
| 271 | 
 | 
|---|
| 272 |                 // get the active thread once
 | 
|---|
| 273 |                 thread$ * athrd = active_thread();
 | 
|---|
| 274 | 
 | 
|---|
| 275 |                 /* paranoid */ verify( athrd->corctx_flag );
 | 
|---|
| 276 |                 athrd->corctx_flag = false;
 | 
|---|
| 277 | 
 | 
|---|
| 278 |                 if(cor->state == Primed) {
 | 
|---|
| 279 |                         __cfactx_suspend();
 | 
|---|
| 280 |                 }
 | 
|---|
| 281 | 
 | 
|---|
| 282 |                 cor->state = Active;
 | 
|---|
| 283 | 
 | 
|---|
| 284 |                 return cor;
 | 
|---|
| 285 |         }
 | 
|---|
| 286 | }
 | 
|---|
| 287 | 
 | 
|---|
| 288 | 
 | 
|---|
| 289 | ////////////////////////////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 290 | // non local ehm routines
 | 
|---|
| 291 | 
 | 
|---|
| 292 | // helper for popping from coroutine's ehm buffer
 | 
|---|
| 293 | inline nonlocal_exception * pop_ehm_head( coroutine$ * this ) {
 | 
|---|
| 294 |     lock( this->ehm_state.buffer_lock __cfaabi_dbg_ctx2 );
 | 
|---|
| 295 |     nonlocal_exception * nl_ex = pop_head( this->ehm_state.ehm_buffer );
 | 
|---|
| 296 |     unlock( this->ehm_state.buffer_lock );
 | 
|---|
| 297 |     return nl_ex;
 | 
|---|
| 298 | }
 | 
|---|
| 299 | 
 | 
|---|
| 300 | // user facing ehm operations
 | 
|---|
| 301 | forall(T & | is_coroutine(T)) {
 | 
|---|
| 302 |     // enable/disable non-local exceptions
 | 
|---|
| 303 |     void enable_ehm( T & cor ) libcfa_public { get_coroutine( cor )->ehm_state.ehm_enabled = true; }
 | 
|---|
| 304 |     void disable_ehm( T & cor ) libcfa_public { get_coroutine( cor )->ehm_state.ehm_enabled = false; }
 | 
|---|
| 305 | 
 | 
|---|
| 306 |     // poll for non-local exceptions
 | 
|---|
| 307 |     bool poll( T & cor ) libcfa_public {
 | 
|---|
| 308 |         coroutine$ * base_cor = get_coroutine( cor );
 | 
|---|
| 309 |         nonlocal_exception * nl_ex = pop_ehm_head( base_cor );
 | 
|---|
| 310 | 
 | 
|---|
| 311 |         // if no exceptions return false
 | 
|---|
| 312 |         if ( nl_ex == 0p ) return false;
 | 
|---|
| 313 |         
 | 
|---|
| 314 |         // otherwise loop and throwResume all pending exceptions
 | 
|---|
| 315 |         while ( nl_ex != 0p ){
 | 
|---|
| 316 |             exception_t * ex = nl_ex->the_exception;
 | 
|---|
| 317 |             free( nl_ex );
 | 
|---|
| 318 |             throwResume *ex;
 | 
|---|
| 319 |             nl_ex = pop_ehm_head( base_cor );
 | 
|---|
| 320 |         }
 | 
|---|
| 321 |         
 | 
|---|
| 322 |         return true;
 | 
|---|
| 323 |     }
 | 
|---|
| 324 | 
 | 
|---|
| 325 |     // poll iff nonlocal ehm is enabled
 | 
|---|
| 326 |     bool checked_poll( T & cor ) libcfa_public { return get_coroutine( cor )->ehm_state.ehm_enabled ? poll( cor ) : false; }
 | 
|---|
| 327 | }
 | 
|---|
| 328 | 
 | 
|---|
| 329 | // resume non local exception at receiver (i.e. enqueue in ehm buffer)
 | 
|---|
| 330 | forall(exceptT &, T & | ehm_resume_at( exceptT, T ))
 | 
|---|
| 331 | void resumeAt( T & receiver, exceptT & ex )  libcfa_public {
 | 
|---|
| 332 |     coroutine$ * cor = get_coroutine( receiver );
 | 
|---|
| 333 |     nonlocal_exception * nl_ex = alloc();
 | 
|---|
| 334 |     (*nl_ex){ (exception_t *)&ex };
 | 
|---|
| 335 |     lock( cor->ehm_state.buffer_lock __cfaabi_dbg_ctx2 );
 | 
|---|
| 336 |     append( cor->ehm_state.ehm_buffer, nl_ex ); 
 | 
|---|
| 337 |     unlock( cor->ehm_state.buffer_lock );
 | 
|---|
| 338 | }
 | 
|---|
| 339 | 
 | 
|---|
| 340 | // Local Variables: //
 | 
|---|
| 341 | // mode: c //
 | 
|---|
| 342 | // tab-width: 4 //
 | 
|---|
| 343 | // End: //
 | 
|---|