Changeset b2f6113 for libcfa/src/concurrency/coroutine.cfa
- Timestamp:
- Apr 4, 2019, 3:37:55 PM (7 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum, stuck-waitfor-destruct
- Children:
- 8c01e1b
- Parents:
- 2fabdc02
- File:
-
- 1 edited
-
libcfa/src/concurrency/coroutine.cfa (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/coroutine.cfa
r2fabdc02 rb2f6113 35 35 36 36 extern "C" { 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 }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 } 42 42 } 43 43 … … 47 47 // minimum feasible stack size in bytes 48 48 #define MinStackSize 1000 49 static size_t pageSize = 0; // architecture pagesize HACK, should go in proper runtime singleton 49 extern size_t __page_size; // architecture pagesize HACK, should go in proper runtime singleton 50 51 void __stack_prepare( __stack_info_t * this, size_t create_size ); 50 52 51 53 //----------------------------------------------------------------------------- 52 54 // Coroutine ctors and dtors 53 void ?{}( coStack_t & this, void * storage, size_t storageSize ) with( this ) { 54 size = storageSize == 0 ? 65000 : storageSize; // size of stack 55 this.storage = storage; // pointer to stack 56 limit = NULL; // stack grows towards stack limit 57 base = NULL; // base of stack 58 context = NULL; // address of cfa_context_t 59 top = NULL; // address of top of storage 60 userStack = storage != NULL; 61 } 62 63 void ^?{}(coStack_t & this) { 64 if ( ! this.userStack && this.storage ) { 65 __cfaabi_dbg_debug_do( 66 if ( mprotect( this.storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) { 67 abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) ); 68 } 69 ); 70 free( this.storage ); 71 } 55 void ?{}( __stack_info_t & this, void * storage, size_t storageSize ) { 56 this.storage = (__stack_t *)storage; 57 58 // Did we get a piece of storage ? 59 if (this.storage || storageSize != 0) { 60 // We either got a piece of storage or the user asked for a specific size 61 // Immediately create the stack 62 // (This is slightly unintuitive that non-default sized coroutines create are eagerly created 63 // but it avoids that all coroutines carry an unnecessary size) 64 verify( storageSize != 0 ); 65 __stack_prepare( &this, storageSize ); 66 } 67 } 68 69 void ^?{}(__stack_info_t & this) { 70 if ( ! this.userStack && this.storage ) { 71 void * storage = (char*)(this.storage) - this.storage->size; 72 __cfaabi_dbg_debug_do( 73 storage = (char*)(storage) - __page_size; 74 if ( mprotect( storage, __page_size, PROT_READ | PROT_WRITE ) == -1 ) { 75 abort( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) ); 76 } 77 ); 78 __cfaabi_dbg_print_safe("Kernel : Deleting stack %p\n", storage); 79 free( storage ); 80 } 72 81 } 73 82 74 83 void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize ) with( this ) { 75 (this.stack){storage, storageSize}; 76 this.name = name; 77 errno_ = 0; 78 state = Start; 79 starter = NULL; 80 last = NULL; 81 cancellation = NULL; 84 (this.stack){storage, storageSize}; 85 this.name = name; 86 state = Start; 87 starter = NULL; 88 last = NULL; 89 cancellation = NULL; 82 90 } 83 91 84 92 void ^?{}(coroutine_desc& this) { 85 if(this.state != Halted && this.state != Start) {86 coroutine_desc * src = TL_GET( this_thread )->curr_cor;87 coroutine_desc * dst = &this;88 89 struct _Unwind_Exception storage;90 storage.exception_class = -1;91 storage.exception_cleanup = _CtxCoroutine_UnwindCleanup;92 this.cancellation = &storage;93 this.last = src;94 95 // not resuming self ?96 if ( src == dst ) {97 abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src );98 }99 100 CoroutineCtxSwitch( src, dst );101 }93 if(this.state != Halted && this.state != Start) { 94 coroutine_desc * src = TL_GET( this_thread )->curr_cor; 95 coroutine_desc * dst = &this; 96 97 struct _Unwind_Exception storage; 98 storage.exception_class = -1; 99 storage.exception_cleanup = _CtxCoroutine_UnwindCleanup; 100 this.cancellation = &storage; 101 this.last = src; 102 103 // not resuming self ? 104 if ( src == dst ) { 105 abort( "Attempt by coroutine %.256s (%p) to terminate itself.\n", src->name, src ); 106 } 107 108 CoroutineCtxSwitch( src, dst ); 109 } 102 110 } 103 111 … … 106 114 forall(dtype T | is_coroutine(T)) 107 115 void prime(T& cor) { 108 coroutine_desc* this = get_coroutine(cor);109 assert(this->state == Start);110 111 this->state = Primed;112 resume(cor);116 coroutine_desc* this = get_coroutine(cor); 117 assert(this->state == Start); 118 119 this->state = Primed; 120 resume(cor); 113 121 } 114 122 115 123 // Wrapper for co 116 124 void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { 117 // Safety note : Preemption must be disabled since there is a race condition 118 // kernelTLS.this_thread->curr_cor and $rsp/$rbp must agree at all times 119 verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate ); 120 disable_interrupts(); 121 122 // set state of current coroutine to inactive 123 src->state = src->state == Halted ? Halted : Inactive; 124 125 // set new coroutine that task is executing 126 TL_GET( this_thread )->curr_cor = dst; 127 128 // context switch to specified coroutine 129 assert( src->stack.context ); 130 CtxSwitch( src->stack.context, dst->stack.context ); 131 // when CtxSwitch returns we are back in the src coroutine 132 133 // set state of new coroutine to active 134 src->state = Active; 135 136 enable_interrupts( __cfaabi_dbg_ctx ); 137 verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate ); 138 139 140 if( unlikely(src->cancellation != NULL) ) { 141 _CtxCoroutine_Unwind(src->cancellation, src); 142 } 143 } //ctxSwitchDirect 144 145 void create_stack( coStack_t* this, unsigned int storageSize ) with( *this ) { 146 //TEMP HACK do this on proper kernel startup 147 if(pageSize == 0ul) pageSize = sysconf( _SC_PAGESIZE ); 148 149 size_t cxtSize = libCeiling( sizeof(machine_context_t), 8 ); // minimum alignment 150 151 if ( !storage ) { 152 __cfaabi_dbg_print_safe("Kernel : Creating stack of size %zu for stack obj %p\n", cxtSize + size + 8, this); 153 154 userStack = false; 155 size = libCeiling( storageSize, 16 ); 156 // use malloc/memalign because "new" raises an exception for out-of-memory 157 158 // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment 159 __cfaabi_dbg_debug_do( storage = memalign( pageSize, cxtSize + size + pageSize ) ); 160 __cfaabi_dbg_no_debug_do( storage = malloc( cxtSize + size + 8 ) ); 161 162 __cfaabi_dbg_debug_do( 163 if ( mprotect( storage, pageSize, PROT_NONE ) == -1 ) { 164 abort( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) ); 165 } // if 166 ); 167 168 if ( (intptr_t)storage == 0 ) { 169 abort( "Attempt to allocate %zd bytes of storage for coroutine or task execution-state but insufficient memory available.", size ); 170 } // if 171 172 __cfaabi_dbg_debug_do( limit = (char *)storage + pageSize ); 173 __cfaabi_dbg_no_debug_do( limit = (char *)libCeiling( (unsigned long)storage, 16 ) ); // minimum alignment 174 175 } else { 176 __cfaabi_dbg_print_safe("Kernel : stack obj %p using user stack %p(%u bytes)\n", this, storage, storageSize); 177 178 assertf( ((size_t)storage & (libAlign() - 1)) == 0ul, "Stack storage %p for task/coroutine must be aligned on %d byte boundary.", storage, (int)libAlign() ); 179 userStack = true; 180 size = storageSize - cxtSize; 181 182 if ( size % 16 != 0u ) size -= 8; 183 184 limit = (char *)libCeiling( (unsigned long)storage, 16 ); // minimum alignment 185 } // if 186 assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %d bytes for a stack.", size, MinStackSize ); 187 188 base = (char *)limit + size; 189 context = base; 190 top = (char *)context + cxtSize; 125 // Safety note : Preemption must be disabled since there is a race condition 126 // kernelTLS.this_thread->curr_cor and $rsp/$rbp must agree at all times 127 verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate ); 128 disable_interrupts(); 129 130 // set state of current coroutine to inactive 131 src->state = src->state == Halted ? Halted : Inactive; 132 133 // set new coroutine that task is executing 134 TL_GET( this_thread )->curr_cor = dst; 135 136 // context switch to specified coroutine 137 CtxSwitch( &src->context, &dst->context ); 138 // when CtxSwitch returns we are back in the src coroutine 139 140 // set state of new coroutine to active 141 src->state = Active; 142 143 enable_interrupts( __cfaabi_dbg_ctx ); 144 verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate ); 145 146 if( unlikely(src->cancellation != NULL) ) { 147 _CtxCoroutine_Unwind(src->cancellation, src); 148 } 149 } 150 151 [void *, size_t] __stack_alloc( size_t storageSize ) { 152 static const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment 153 assert(__page_size != 0l); 154 size_t size = libCeiling( storageSize, 16 ) + stack_data_size; 155 156 // If we are running debug, we also need to allocate a guardpage to catch stack overflows. 157 void * storage; 158 __cfaabi_dbg_debug_do( 159 storage = memalign( __page_size, size + __page_size ); 160 ); 161 __cfaabi_dbg_no_debug_do( 162 storage = (void*)malloc(size); 163 ); 164 165 __cfaabi_dbg_print_safe("Kernel : Created stack %p of size %zu\n", storage, size); 166 __cfaabi_dbg_debug_do( 167 if ( mprotect( storage, __page_size, PROT_NONE ) == -1 ) { 168 abort( "__stack_alloc : internal error, mprotect failure, error(%d) %s.", (int)errno, strerror( (int)errno ) ); 169 } 170 storage = (void *)(((intptr_t)storage) + __page_size); 171 ); 172 173 verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul ); 174 return [storage, size]; 175 } 176 177 void __stack_prepare( __stack_info_t * this, size_t create_size ) { 178 static const size_t stack_data_size = libCeiling( sizeof(__stack_t), 16 ); // minimum alignment 179 bool userStack; 180 void * storage; 181 size_t size; 182 if ( !this->storage ) { 183 userStack = false; 184 [storage, size] = __stack_alloc( create_size ); 185 } else { 186 userStack = true; 187 __cfaabi_dbg_print_safe("Kernel : stack obj %p using user stack %p(%zu bytes)\n", this, this->storage, this->storage->size); 188 189 // The stack must be aligned, advance the pointer to the next align data 190 storage = (void*)libCeiling( (intptr_t)this->storage, libAlign()); 191 192 // The size needs to be shrinked to fit all the extra data structure and be aligned 193 ptrdiff_t diff = (intptr_t)storage - (intptr_t)this->storage; 194 size = libFloor(create_size - stack_data_size - diff, libAlign()); 195 } // if 196 assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %d bytes for a stack.", size, MinStackSize ); 197 198 this->storage = (__stack_t *)((intptr_t)storage + size); 199 this->storage->size = size; 200 this->storage->limit = storage; 201 this->storage->base = (void*)((intptr_t)storage + size); 202 this->userStack = userStack; 191 203 } 192 204 … … 194 206 // is not inline (We can't inline Cforall in C) 195 207 extern "C" { 196 void __suspend_internal(void) {197 suspend();198 }199 200 void __leave_coroutine( coroutine_desc * src ) {201 coroutine_desc * starter = src->cancellation != 0 ? src->last : src->starter;202 203 src->state = Halted;204 205 assertf( starter != 0,206 "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n"207 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.",208 src->name, src );209 assertf( starter->state != Halted,210 "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n"211 "Possible cause is terminated coroutine's main routine has already returned.",212 src->name, src, starter->name, starter );213 214 CoroutineCtxSwitch( src, starter );215 }208 void __suspend_internal(void) { 209 suspend(); 210 } 211 212 void __leave_coroutine( coroutine_desc * src ) { 213 coroutine_desc * starter = src->cancellation != 0 ? src->last : src->starter; 214 215 src->state = Halted; 216 217 assertf( starter != 0, 218 "Attempt to suspend/leave coroutine \"%.256s\" (%p) that has never been resumed.\n" 219 "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", 220 src->name, src ); 221 assertf( starter->state != Halted, 222 "Attempt by coroutine \"%.256s\" (%p) to suspend/leave back to terminated coroutine \"%.256s\" (%p).\n" 223 "Possible cause is terminated coroutine's main routine has already returned.", 224 src->name, src, starter->name, starter ); 225 226 CoroutineCtxSwitch( src, starter ); 227 } 216 228 } 217 229
Note:
See TracChangeset
for help on using the changeset viewer.