Changeset 53449a4 for libcfa/src/concurrency
- Timestamp:
- Dec 16, 2020, 4:01:57 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:
- 8ba363e, c8025a21
- Parents:
- b3c8496 (diff), 3e5dd913 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- libcfa/src/concurrency
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/coroutine.cfa
rb3c8496 r53449a4 10 10 // Created On : Mon Nov 28 12:27:26 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Oct 23 23:05:24 202013 // Update Count : 2 212 // Last Modified On : Tue Dec 15 12:06:04 2020 13 // Update Count : 23 14 14 // 15 15 … … 28 28 #include "kernel_private.hfa" 29 29 #include "exception.hfa" 30 #include "math.hfa" 31 32 #define CFA_COROUTINE_USE_MMAP 0 30 33 31 34 #define __CFA_INVOKE_PRIVATE__ … … 85 88 static const size_t MinStackSize = 1000; 86 89 extern size_t __page_size; // architecture pagesize HACK, should go in proper runtime singleton 90 extern int __map_prot; 87 91 88 92 void __stack_prepare( __stack_info_t * this, size_t create_size ); 93 void __stack_clean ( __stack_info_t * this ); 89 94 90 95 //----------------------------------------------------------------------------- … … 107 112 bool userStack = ((intptr_t)this.storage & 0x1) != 0; 108 113 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 ); 114 __stack_clean( &this ); 121 115 } 122 116 } … … 167 161 assert(__page_size != 0l); 168 162 size_t size = libCeiling( storageSize, 16 ) + stack_data_size; 163 size = ceiling(size, __page_size); 169 164 170 165 // If we are running debug, we also need to allocate a guardpage to catch stack overflows. 171 166 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 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 179 191 __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 192 187 193 verify( ((intptr_t)storage & (libAlign() - 1)) == 0ul ); 188 194 return [storage, size]; 195 } 196 197 void __stack_clean ( __stack_info_t * this ) { 198 size_t size = ((intptr_t)this->storage->base) - ((intptr_t)this->storage->limit) + sizeof(__stack_t); 199 void * storage = this->storage->limit; 200 201 #if CFA_COROUTINE_USE_MMAP 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); 189 217 } 190 218 … … 210 238 assertf( size >= MinStackSize, "Stack size %zd provides less than minimum of %zd bytes for a stack.", size, MinStackSize ); 211 239 212 this->storage = (__stack_t *)((intptr_t)storage + size );240 this->storage = (__stack_t *)((intptr_t)storage + size - sizeof(__stack_t)); 213 241 this->storage->limit = storage; 214 this->storage->base = (void*)((intptr_t)storage + size );242 this->storage->base = (void*)((intptr_t)storage + size - sizeof(__stack_t)); 215 243 this->storage->exception_context.top_resume = 0p; 216 244 this->storage->exception_context.current_exception = 0p; -
libcfa/src/concurrency/coroutine.hfa
rb3c8496 r53449a4 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 … … 142 144 143 145 if( unlikely(dst->context.SP == 0p) ) { 144 active_thread()->curr_cor = dst;145 146 __stack_prepare(&dst->stack, 65000); 146 147 __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine); 147 active_thread()->curr_cor = src;148 148 } 149 149 -
libcfa/src/concurrency/io/setup.cfa
rb3c8496 r53449a4 17 17 #define _GNU_SOURCE /* See feature_test_macros(7) */ 18 18 19 #if defined(__CFA_DEBUG__) 20 // #define __CFA_DEBUG_PRINT_IO__ 21 // #define __CFA_DEBUG_PRINT_IO_CORE__ 22 #endif 23 19 24 #include "io/types.hfa" 20 25 #include "kernel.hfa" … … 111 116 112 117 void __kernel_io_startup(void) { 113 __cfa abi_dbg_print_safe("Kernel : Creating EPOLL instance\n" );118 __cfadbg_print_safe(io_core, "Kernel : Creating EPOLL instance\n" ); 114 119 115 120 iopoll.epollfd = epoll_create1(0); … … 118 123 } 119 124 120 __cfa abi_dbg_print_safe("Kernel : Starting io poller thread\n" );125 __cfadbg_print_safe(io_core, "Kernel : Starting io poller thread\n" ); 121 126 122 127 iopoll.run = true; … … 132 137 // Wait for the io poller thread to finish 133 138 134 pthread_join( iopoll.thrd, 0p ); 135 free( iopoll.stack ); 139 __destroy_pthread( iopoll.thrd, iopoll.stack, 0p ); 136 140 137 141 int ret = close(iopoll.epollfd); … … 142 146 // Io polling is now fully stopped 143 147 144 __cfa abi_dbg_print_safe("Kernel : IO poller stopped\n" );148 __cfadbg_print_safe(io_core, "Kernel : IO poller stopped\n" ); 145 149 } 146 150 … … 150 154 id.id = doregister(&id); 151 155 __cfaabi_tls.this_proc_id = &id; 152 __cfa abi_dbg_print_safe("Kernel : IO poller thread starting\n" );156 __cfadbg_print_safe(io_core, "Kernel : IO poller thread starting\n" ); 153 157 154 158 // Block signals to control when they arrive … … 185 189 } 186 190 187 __cfa abi_dbg_print_safe("Kernel : IO poller thread stopping\n" );191 __cfadbg_print_safe(io_core, "Kernel : IO poller thread stopping\n" ); 188 192 unregister(&id); 189 193 return 0p; -
libcfa/src/concurrency/kernel/startup.cfa
rb3c8496 r53449a4 29 29 #include "kernel_private.hfa" 30 30 #include "startup.hfa" // STARTUP_PRIORITY_XXX 31 #include "math.hfa" 32 33 #define CFA_PROCESSOR_USE_MMAP 0 31 34 32 35 //----------------------------------------------------------------------------- … … 114 117 } 115 118 116 size_t __page_size = 0;119 extern size_t __page_size; 117 120 118 121 //----------------------------------------------------------------------------- … … 158 161 /* paranoid */ verify( ! __preemption_enabled() ); 159 162 __cfadbg_print_safe(runtime_core, "Kernel : Starting\n"); 160 161 __page_size = sysconf( _SC_PAGESIZE );162 163 163 164 __cfa_dbg_global_clusters.list{ __get }; … … 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. 679 #if CFA_PROCESSOR_USE_MMAP 680 stacksize = ceiling( stacksize, __page_size ) + __page_size; 681 stack = mmap(0p, stacksize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); 682 if(stack == ((void*)-1)) { 683 abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) ); 684 } 683 685 if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { 684 abort( " mprotect: internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );686 abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); 685 687 } // if 686 ); 687 __cfaabi_dbg_no_debug_do( 688 stack = malloc( stacksize ); 689 ); 688 #else 689 __cfaabi_dbg_debug_do( 690 stack = memalign( __page_size, stacksize + __page_size ); 691 // pthread has no mechanism to create the guard page in user supplied stack. 692 if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { 693 abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); 694 } // if 695 ); 696 __cfaabi_dbg_no_debug_do( 697 stack = malloc( stacksize ); 698 ); 699 #endif 700 690 701 691 702 check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" ); … … 694 705 return stack; 695 706 } 707 708 void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) { 709 int err = pthread_join( pthread, retval ); 710 if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err)); 711 712 #if CFA_PROCESSOR_USE_MMAP 713 pthread_attr_t attr; 714 715 check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute 716 717 size_t stacksize; 718 // default stack size, normally defined by shell limit 719 check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" ); 720 assert( stacksize >= PTHREAD_STACK_MIN ); 721 stacksize += __page_size; 722 723 if(munmap(stack, stacksize) == -1) { 724 abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) ); 725 } 726 #else 727 free( stack ); 728 #endif 729 } 730 696 731 697 732 #if defined(__CFA_WITH_VERIFY__) -
libcfa/src/concurrency/kernel_private.hfa
rb3c8496 r53449a4 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
rb3c8496 r53449a4 575 575 // Wait for the preemption thread to finish 576 576 577 pthread_join( alarm_thread, 0p ); 578 free( alarm_stack ); 577 __destroy_pthread( alarm_thread, alarm_stack, 0p ); 579 578 580 579 // Preemption is now fully stopped
Note: See TracChangeset
for help on using the changeset viewer.