Changeset bfcf6b9 for libcfa/src/concurrency
- Timestamp:
- Dec 10, 2020, 3:20:33 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 297cf18
- Parents:
- c920317
- Location:
- libcfa/src/concurrency
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/coroutine.cfa
rc920317 rbfcf6b9 28 28 #include "kernel_private.hfa" 29 29 #include "exception.hfa" 30 #include "math.hfa" 30 31 31 32 #define __CFA_INVOKE_PRIVATE__ … … 87 88 88 89 void __stack_prepare( __stack_info_t * this, size_t create_size ); 90 void __stack_clean ( __stack_info_t * this ); 89 91 90 92 //----------------------------------------------------------------------------- … … 107 109 bool userStack = ((intptr_t)this.storage & 0x1) != 0; 108 110 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 ); 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 ); 121 124 } 122 125 } … … 167 170 assert(__page_size != 0l); 168 171 size_t size = libCeiling( storageSize, 16 ) + stack_data_size; 172 size = ceiling(size, __page_size); 169 173 170 174 // If we are running debug, we also need to allocate a guardpage to catch stack overflows. 171 175 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 ); 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); 186 198 187 199 verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul ); 188 200 return [storage, size]; 201 } 202 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 } 189 211 } 190 212 … … 210 232 assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize ); 211 233 212 this->storage = (__stack_t *)((intptr_t)storage + size );234 this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t)); 213 235 this->storage->limit = storage; 214 this->storage->base = (void*)((intptr_t)storage + size );236 this->storage->base = (void*)((intptr_t)storage + size - sizeof(__stack_t)); 215 237 this->storage->exception_context.top_resume = 0p; 216 238 this->storage->exception_context.current_exception = 0p; -
libcfa/src/concurrency/coroutine.hfa
rc920317 rbfcf6b9 102 102 } 103 103 104 extern void __stack_prepare ( __stack_info_t * this, size_t size /* ignored if storage already allocated */); 104 extern void __stack_prepare( __stack_info_t * this, size_t size /* ignored if storage already allocated */); 105 extern void __stack_clean ( __stack_info_t * this ); 106 105 107 106 108 // Suspend implementation inlined for performance -
libcfa/src/concurrency/io/setup.cfa
rc920317 rbfcf6b9 132 132 // Wait for the io poller thread to finish 133 133 134 pthread_join( iopoll.thrd, 0p ); 135 free( iopoll.stack ); 134 __destroy_pthread( iopoll.thrd, iopoll.stack, 0p ); 136 135 137 136 int ret = close(iopoll.epollfd); -
libcfa/src/concurrency/kernel/startup.cfa
rc920317 rbfcf6b9 29 29 #include "kernel_private.hfa" 30 30 #include "startup.hfa" // STARTUP_PRIORITY_XXX 31 #include "math.hfa" 31 32 32 33 //----------------------------------------------------------------------------- … … 539 540 } 540 541 542 extern size_t __page_size; 541 543 void ^?{}(processor & this) with( this ){ 542 544 if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) { … … 550 552 } 551 553 552 int err = pthread_join( kernel_thread, 0p ); 553 if( err != 0 ) abort("KERNEL ERROR: joining processor %p caused error %s\n", &this, strerror(err)); 554 555 free( this.stack ); 554 __destroy_pthread( kernel_thread, this.stack, 0p ); 556 555 557 556 disable_interrupts(); … … 678 677 679 678 void * stack; 680 __cfaabi_dbg_debug_do( 681 stack = memalign( __page_size, stacksize + __page_size ); 682 // pthread has no mechanism to create the guard page in user supplied stack. 683 if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { 684 abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); 685 } // if 686 ); 687 __cfaabi_dbg_no_debug_do( 688 stack = malloc( stacksize ); 689 ); 679 #warning due to the thunk problem, stack creation uses mmap, revert to malloc once this goes away 680 // __cfaabi_dbg_debug_do( 681 // stack = memalign( __page_size, stacksize + __page_size ); 682 // // pthread has no mechanism to create the guard page in user supplied stack. 683 // if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { 684 // abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); 685 // } // if 686 // ); 687 // __cfaabi_dbg_no_debug_do( 688 // stack = malloc( stacksize ); 689 // ); 690 stacksize = ceiling( stacksize, __page_size ) + __page_size; 691 stack = mmap(0p, stacksize, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); 692 if(stack == ((void*)-1)) { 693 abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) ); 694 } 695 if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { 696 abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); 697 } // if 690 698 691 699 check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" ); … … 694 702 return stack; 695 703 } 704 705 void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) { 706 int err = pthread_join( pthread, retval ); 707 if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err)); 708 709 pthread_attr_t attr; 710 711 check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute 712 713 size_t stacksize; 714 // default stack size, normally defined by shell limit 715 check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" ); 716 assert( stacksize >= PTHREAD_STACK_MIN ); 717 stacksize += __page_size; 718 719 if(munmap(stack, stacksize) == -1) { 720 abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) ); 721 } 722 } 723 696 724 697 725 #if defined(__CFA_WITH_VERIFY__) -
libcfa/src/concurrency/kernel_private.hfa
rc920317 rbfcf6b9 49 49 50 50 void * __create_pthread( pthread_t *, void * (*)(void *), void * ); 51 void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ); 51 52 52 53 -
libcfa/src/concurrency/preemption.cfa
rc920317 rbfcf6b9 536 536 // Wait for the preemption thread to finish 537 537 538 pthread_join( alarm_thread, 0p ); 539 free( alarm_stack ); 538 __destroy_pthread( alarm_thread, alarm_stack, 0p ); 540 539 541 540 // Preemption is now fully stopped
Note: See TracChangeset
for help on using the changeset viewer.