- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/kernel.cfa
r09d4b22 r2026bb6 10 10 // Created On : Tue Jan 17 12:27:26 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Dec 5 16:25:52201913 // Update Count : 5212 // Last Modified On : Thu Jun 20 17:21:23 2019 13 // Update Count : 25 14 14 // 15 15 … … 26 26 #include <signal.h> 27 27 #include <unistd.h> 28 #include <limits.h> // PTHREAD_STACK_MIN29 #include <sys/mman.h> // mprotect30 28 } 31 29 … … 42 40 //----------------------------------------------------------------------------- 43 41 // Some assembly required 44 #if defined( __i386 )42 #if defined( __i386 ) 45 43 #define CtxGet( ctx ) \ 46 44 __asm__ volatile ( \ … … 125 123 126 124 extern "C" { 127 125 struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters; 128 126 } 129 127 … … 133 131 // Global state 134 132 thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = { 135 NULL, // cannot use 0p136 133 NULL, 137 { 1, false, false },138 6u //this should be seeded better but due to a bug calling rdtsc doesn't work134 NULL, 135 { 1, false, false } 139 136 }; 140 137 … … 142 139 // Struct to steal stack 143 140 struct current_stack_info_t { 144 __stack_t * storage; 145 void * base;// base of stack146 void * limit;// stack grows towards stack limit147 void * context;// address of cfa_context_t141 __stack_t * storage; // pointer to stack object 142 void *base; // base of stack 143 void *limit; // stack grows towards stack limit 144 void *context; // address of cfa_context_t 148 145 }; 149 146 … … 174 171 name = "Main Thread"; 175 172 state = Start; 176 starter = 0p;177 last = 0p;178 cancellation = 0p;173 starter = NULL; 174 last = NULL; 175 cancellation = NULL; 179 176 } 180 177 … … 187 184 self_mon.recursion = 1; 188 185 self_mon_p = &self_mon; 189 next = 0p;190 191 node.next = 0p;192 node.prev = 0p;186 next = NULL; 187 188 node.next = NULL; 189 node.prev = NULL; 193 190 doregister(curr_cluster, this); 194 191 … … 214 211 terminated{ 0 }; 215 212 do_terminate = false; 216 preemption_alarm = 0p;213 preemption_alarm = NULL; 217 214 pending_preemption = false; 218 215 runner.proc = &this; … … 234 231 } 235 232 236 pthread_join( kernel_thread, 0p ); 237 free( this.stack ); 233 pthread_join( kernel_thread, NULL ); 238 234 } 239 235 … … 264 260 //Main of the processor contexts 265 261 void main(processorCtx_t & runner) { 266 // Because of a bug, we couldn't initialized the seed on construction267 // Do it here268 kernelTLS.rand_seed ^= rdtscl();269 270 262 processor * this = runner.proc; 271 263 verify(this); … … 281 273 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this); 282 274 283 thread_desc * readyThread = 0p; 284 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) { 275 thread_desc * readyThread = NULL; 276 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) 277 { 285 278 readyThread = nextThread( this->cltr ); 286 279 287 if(readyThread) { 280 if(readyThread) 281 { 288 282 verify( ! kernelTLS.preemption_state.enabled ); 289 283 … … 296 290 297 291 spin_count = 0; 298 } else { 292 } 293 else 294 { 299 295 // spin(this, &spin_count); 300 296 halt(this); … … 409 405 processor * proc = (processor *) arg; 410 406 kernelTLS.this_processor = proc; 411 kernelTLS.this_thread = 0p;407 kernelTLS.this_thread = NULL; 412 408 kernelTLS.preemption_state.[enabled, disable_count] = [false, 1]; 413 409 // SKULLDUGGERY: We want to create a context for the processor coroutine … … 422 418 423 419 //Set global state 424 kernelTLS.this_thread = 0p;420 kernelTLS.this_thread = NULL; 425 421 426 422 //We now have a proper context from which to schedule threads … … 438 434 __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner); 439 435 440 return 0p; 441 } 442 443 static void Abort( int ret, const char * func ) { 444 if ( ret ) { // pthread routines return errno values 445 abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) ); 446 } // if 447 } // Abort 448 449 void * create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) { 450 pthread_attr_t attr; 451 452 Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute 453 454 size_t stacksize; 455 // default stack size, normally defined by shell limit 456 Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" ); 457 assert( stacksize >= PTHREAD_STACK_MIN ); 458 459 void * stack; 460 __cfaabi_dbg_debug_do( 461 stack = memalign( __page_size, stacksize + __page_size ); 462 // pthread has no mechanism to create the guard page in user supplied stack. 463 if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { 464 abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); 465 } // if 466 ); 467 __cfaabi_dbg_no_debug_do( 468 stack = malloc( stacksize ); 469 ); 470 471 Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" ); 472 473 Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" ); 474 return stack; 436 return NULL; 475 437 } 476 438 … … 478 440 __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this); 479 441 480 this->stack = create_pthread( &this->kernel_thread, CtxInvokeProcessor, (void*)this );442 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this ); 481 443 482 444 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this); … … 535 497 verify( ! kernelTLS.preemption_state.enabled ); 536 498 537 verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );499 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next ); 538 500 539 501 with( *thrd->curr_cluster ) { … … 714 676 void ?{}(processorCtx_t & this, processor * proc) { 715 677 (this.__cor){ "Processor" }; 716 this.__cor.starter = 0p;678 this.__cor.starter = NULL; 717 679 this.proc = proc; 718 680 } … … 723 685 terminated{ 0 }; 724 686 do_terminate = false; 725 preemption_alarm = 0p;687 preemption_alarm = NULL; 726 688 pending_preemption = false; 727 689 kernel_thread = pthread_self(); … … 857 819 if(thrd) { 858 820 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); 859 __cfaabi_ bits_write( STDERR_FILENO,abort_text, len );821 __cfaabi_dbg_bits_write( abort_text, len ); 860 822 861 823 if ( &thrd->self_cor != thrd->curr_cor ) { 862 824 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); 863 __cfaabi_ bits_write( STDERR_FILENO,abort_text, len );825 __cfaabi_dbg_bits_write( abort_text, len ); 864 826 } 865 827 else { 866 __cfaabi_ bits_write( STDERR_FILENO,".\n", 2 );828 __cfaabi_dbg_bits_write( ".\n", 2 ); 867 829 } 868 830 } 869 831 else { 870 832 int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); 871 __cfaabi_ bits_write( STDERR_FILENO,abort_text, len );833 __cfaabi_dbg_bits_write( abort_text, len ); 872 834 } 873 835 } … … 880 842 881 843 extern "C" { 882 void __cfaabi_ bits_acquire() {844 void __cfaabi_dbg_bits_acquire() { 883 845 lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); 884 846 } 885 847 886 void __cfaabi_ bits_release() {848 void __cfaabi_dbg_bits_release() { 887 849 unlock( kernel_debug_lock ); 888 850 } … … 917 879 918 880 void V(semaphore & this) with( this ) { 919 thread_desc * thrd = 0p;881 thread_desc * thrd = NULL; 920 882 lock( lock __cfaabi_dbg_ctx2 ); 921 883 count += 1;
Note:
See TracChangeset
for help on using the changeset viewer.