Changeset 7416d46a for src/libcfa/concurrency
- Timestamp:
- Jan 30, 2018, 3:54:32 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 633a642
- Parents:
- f792cb8 (diff), 42be3c3 (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:
- src/libcfa/concurrency
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/invoke.c
rf792cb8 r7416d46a 10 10 // Created On : Tue Jan 17 12:27:26 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jul 21 22:28:33 201713 // Update Count : 112 // Last Modified On : Tue Jan 23 14:04:56 2018 13 // Update Count : 2 14 14 // 15 15 … … 133 133 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520 134 134 ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F; //Vol. 1 8-7 135 136 #elif defined( __ARM_ARCH ) 137 138 struct FakeStack { 139 float fpRegs[16]; // floating point registers 140 void *intRegs[9]; // integer/pointer registers 141 void *arg[2]; // placeholder for this pointer 142 }; 143 144 ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack ); 145 ((struct machine_context_t *)stack->context)->FP = NULL; 146 147 struct FakeStack *fs = (struct FakeStack *)((struct machine_context_t *)stack->context)->SP; 148 149 fs->intRegs[8] = CtxInvokeStub; 150 fs->arg[0] = this; 151 fs->arg[1] = invoke; 135 152 #else 136 153 #error Only __i386__ and __x86_64__ is supported for threads in cfa -
src/libcfa/concurrency/invoke.h
rf792cb8 r7416d46a 10 10 // Created On : Tue Jan 17 12:27:26 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jul 21 22:28:56 201713 // Update Count : 112 // Last Modified On : Tue Jan 23 14:55:46 2018 13 // Update Count : 3 14 14 // 15 15 … … 134 134 // instrusive link field for threads 135 135 struct thread_desc * next; 136 137 __cfaabi_dbg_debug_do( 138 // instrusive link field for debugging 139 struct thread_desc * dbg_next; 140 struct thread_desc * dbg_prev; 141 ) 136 142 }; 137 143 … … 203 209 "movl %%ebp,%1\n" \ 204 210 : "=rm" (ctx.SP), "=rm" (ctx.FP) ) 211 #elif defined( __ARM_ARCH ) 212 #define CtxGet( ctx ) __asm__ ( \ 213 "mov %0,%%sp\n" \ 214 "mov %1,%%r11\n" \ 215 : "=rm" (ctx.SP), "=rm" (ctx.FP) ) 205 216 #endif 206 217 -
src/libcfa/concurrency/kernel.c
rf792cb8 r7416d46a 10 10 // Created On : Tue Jan 17 12:27:26 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jul 21 22:33:18201713 // Update Count : 212 // Last Modified On : Fri Dec 8 16:23:33 2017 13 // Update Count : 3 14 14 // 15 15 16 16 //C Includes 17 17 #include <stddef.h> 18 #define ftype `ftype` 18 19 extern "C" { 19 20 #include <stdio.h> … … 23 24 #include <unistd.h> 24 25 } 26 #undef ftype 25 27 26 28 //CFA Includes … … 240 242 void finishRunning(processor * this) with( this->finish ) { 241 243 if( action_code == Release ) { 244 verify( disable_preempt_count > 1 ); 242 245 unlock( *lock ); 243 246 } … … 246 249 } 247 250 else if( action_code == Release_Schedule ) { 251 verify( disable_preempt_count > 1 ); 248 252 unlock( *lock ); 249 253 ScheduleThread( thrd ); 250 254 } 251 255 else if( action_code == Release_Multi ) { 256 verify( disable_preempt_count > lock_count ); 252 257 for(int i = 0; i < lock_count; i++) { 253 258 unlock( *locks[i] ); … … 363 368 this_processor->finish.lock = lock; 364 369 365 verify( disable_preempt_count > 0);370 verify( disable_preempt_count > 1 ); 366 371 suspend(); 367 372 verify( disable_preempt_count > 0 ); … … 389 394 this_processor->finish.thrd = thrd; 390 395 391 verify( disable_preempt_count > 0);396 verify( disable_preempt_count > 1 ); 392 397 suspend(); 393 398 verify( disable_preempt_count > 0 ); … … 514 519 } 515 520 521 //============================================================================================= 522 // Unexpected Terminating logic 523 //============================================================================================= 524 525 516 526 static __spinlock_t kernel_abort_lock; 517 527 static __spinlock_t kernel_debug_lock; … … 609 619 } 610 620 621 //----------------------------------------------------------------------------- 622 // Debug 623 __cfaabi_dbg_debug_do( 624 struct { 625 thread_desc * tail; 626 } __cfaabi_dbg_thread_list = { NULL }; 627 628 void __cfaabi_dbg_thread_register( thread_desc * thrd ) { 629 if( !__cfaabi_dbg_thread_list.tail ) { 630 __cfaabi_dbg_thread_list.tail = thrd; 631 return; 632 } 633 __cfaabi_dbg_thread_list.tail->dbg_next = thrd; 634 thrd->dbg_prev = __cfaabi_dbg_thread_list.tail; 635 __cfaabi_dbg_thread_list.tail = thrd; 636 } 637 638 void __cfaabi_dbg_thread_unregister( thread_desc * thrd ) { 639 thread_desc * prev = thrd->dbg_prev; 640 thread_desc * next = thrd->dbg_next; 641 642 if( next ) { next->dbg_prev = prev; } 643 else { 644 assert( __cfaabi_dbg_thread_list.tail == thrd ); 645 __cfaabi_dbg_thread_list.tail = prev; 646 } 647 648 if( prev ) { prev->dbg_next = next; } 649 650 thrd->dbg_prev = NULL; 651 thrd->dbg_next = NULL; 652 } 653 ) 611 654 // Local Variables: // 612 655 // mode: c // -
src/libcfa/concurrency/kernel_private.h
rf792cb8 r7416d46a 85 85 extern void ThreadCtxSwitch(coroutine_desc * src, coroutine_desc * dst); 86 86 87 __cfaabi_dbg_debug_do( 88 extern void __cfaabi_dbg_thread_register ( thread_desc * thrd ); 89 extern void __cfaabi_dbg_thread_unregister( thread_desc * thrd ); 90 ) 91 87 92 //----------------------------------------------------------------------------- 88 93 // Utils -
src/libcfa/concurrency/monitor.c
rf792cb8 r7416d46a 53 53 static inline __lock_size_t aggregate ( monitor_desc * storage [], const __waitfor_mask_t & mask ); 54 54 55 #ifndef __CFA_LOCK_NO_YIELD56 #define DO_LOCK lock_yield57 #else58 #define DO_LOCK lock59 #endif60 61 55 //----------------------------------------------------------------------------- 62 56 // Useful defines … … 90 84 static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) { 91 85 // Lock the monitor spinlock 92 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );86 lock( this->lock __cfaabi_dbg_ctx2 ); 93 87 thread_desc * thrd = this_thread; 88 89 verify( disable_preempt_count > 0 ); 94 90 95 91 __cfaabi_dbg_print_safe("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner); … … 121 117 // Some one else has the monitor, wait in line for it 122 118 append( this->entry_queue, thrd ); 119 120 verify( disable_preempt_count > 0 ); 121 123 122 BlockInternal( &this->lock ); 124 123 … … 138 137 static void __enter_monitor_dtor( monitor_desc * this, fptr_t func ) { 139 138 // Lock the monitor spinlock 140 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );139 lock( this->lock __cfaabi_dbg_ctx2 ); 141 140 thread_desc * thrd = this_thread; 142 141 … … 201 200 // Leave single monitor 202 201 void __leave_monitor_desc( monitor_desc * this ) { 203 // Lock the monitor spinlock , DO_LOCK to reduce contention204 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );202 // Lock the monitor spinlock 203 lock( this->lock __cfaabi_dbg_ctx2 ); 205 204 206 205 __cfaabi_dbg_print_safe("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner); … … 248 247 249 248 // Lock the monitor now 250 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );249 lock( this->lock __cfaabi_dbg_ctx2 ); 251 250 252 251 disable_interrupts(); … … 397 396 append( this.blocked, &waiter ); 398 397 398 verify( disable_preempt_count == 0 ); 399 399 400 // Lock all monitors (aggregates the locks as well) 400 401 lock_all( monitors, locks, count ); 402 403 // verifyf( disable_preempt_count == count, "Got %d, expected %d\n", disable_preempt_count, count ); 404 if(disable_preempt_count != count) { __cfaabi_dbg_print_buffer_decl("----------Gonna crash\n"); } 401 405 402 406 // Find the next thread(s) to run … … 473 477 monitor_ctx( this.monitors, this.monitor_count ); 474 478 479 verify( disable_preempt_count == 0 ); 480 475 481 // Lock all monitors (aggregates the locks them as well) 476 482 lock_all( monitors, locks, count ); 483 484 // verify( disable_preempt_count == count ); 485 if(disable_preempt_count != count) { __cfaabi_dbg_print_buffer_decl("----------Gonna crash\n"); } 486 477 487 478 488 // Create the node specific to this wait operation … … 737 747 static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) { 738 748 for( __lock_size_t i = 0; i < count; i++ ) { 739 DO_LOCK( *locks[i] __cfaabi_dbg_ctx2 );749 lock( *locks[i] __cfaabi_dbg_ctx2 ); 740 750 } 741 751 } … … 744 754 for( __lock_size_t i = 0; i < count; i++ ) { 745 755 __spinlock_t * l = &source[i]->lock; 746 DO_LOCK( *l __cfaabi_dbg_ctx2 );756 lock( *l __cfaabi_dbg_ctx2 ); 747 757 if(locks) locks[i] = l; 748 758 } -
src/libcfa/concurrency/preemption.c
rf792cb8 r7416d46a 10 10 // Created On : Mon Jun 5 14:20:42 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jul 21 22:36:05 201713 // Update Count : 212 // Last Modified On : Tue Jan 23 17:59:30 2018 13 // Update Count : 7 14 14 // 15 15 16 16 #include "preemption.h" 17 17 18 #define ftype `ftype` 18 19 extern "C" { 19 20 #include <errno.h> 20 #include <execinfo.h>21 #define __USE_GNU22 #include <signal.h>23 #undef __USE_GNU24 21 #include <stdio.h> 25 22 #include <string.h> 26 23 #include <unistd.h> 27 24 } 28 29 30 #ifdef __USE_STREAM__ 31 #include "fstream" 32 #endif 25 #undef ftype 26 27 #include "bits/signal.h" 33 28 34 29 //TODO move to defaults … … 39 34 return __CFA_DEFAULT_PREEMPTION__; 40 35 } 41 42 // Short hands for signal context information43 #define __CFA_SIGCXT__ ucontext_t *44 #define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt45 36 46 37 // FwdDeclarations : timeout handlers … … 53 44 void sigHandler_abort ( __CFA_SIGPARMS__ ); 54 45 55 // FwdDeclarations : sigaction wrapper56 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );57 58 46 // FwdDeclarations : alarm thread main 59 47 void * alarm_loop( __attribute__((unused)) void * args ); 60 48 61 49 // Machine specific register name 62 #if def __x86_64__50 #if defined(__x86_64__) 63 51 #define CFA_REG_IP REG_RIP 64 #el se52 #elif defined(__i386__) 65 53 #define CFA_REG_IP REG_EIP 54 #elif defined(__ARM_ARCH__) 55 #define CFA_REG_IP REG_R15 66 56 #endif 67 57 … … 179 169 void enable_interrupts_noPoll() { 180 170 __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST ); 181 verify ( prev != 0u); // If this triggers someone is enabled already enabled interrupts171 verifyf( prev != 0u, "Incremented from %u\n", prev ); // If this triggers someone is enabled already enabled interrupts 182 172 } 183 173 } … … 243 233 // Setup proper signal handlers 244 234 __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART ); // CtxSwitch handler 245 // __kernel_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO ); // Failure handler246 // __kernel_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO ); // Failure handler247 235 248 236 signal_block( SIGALRM ); … … 296 284 // Receives SIGUSR1 signal and causes the current thread to yield 297 285 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) { 286 #if defined( __ARM_ARCH ) 287 __cfaabi_dbg_debug_do( last_interrupt = (void *)(cxt->uc_mcontext.arm_pc); ) 288 #else 298 289 __cfaabi_dbg_debug_do( last_interrupt = (void *)(cxt->uc_mcontext.gregs[CFA_REG_IP]); ) 299 300 // Check if it is safe to preempt here 290 #endif 291 292 // Check if it is safe to preempt here 301 293 if( !preemption_ready() ) { return; } 294 295 __cfaabi_dbg_print_buffer_decl(" KERNEL: preempting core %p (%p).\n", this_processor, this_thread); 302 296 303 297 preemption_in_progress = true; // Sync flag : prevent recursive calls to the signal handler … … 371 365 } 372 366 373 // Sigaction wrapper : register an signal handler374 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {375 struct sigaction act;376 377 act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;378 act.sa_flags = flags;379 380 if ( sigaction( sig, &act, NULL ) == -1 ) {381 __cfaabi_dbg_print_buffer_decl(382 " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",383 sig, handler, flags, errno, strerror( errno )384 );385 _exit( EXIT_FAILURE );386 }387 }388 389 // Sigaction wrapper : restore default handler390 static void __kernel_sigdefault( int sig ) {391 struct sigaction act;392 393 act.sa_handler = SIG_DFL;394 act.sa_flags = 0;395 sigemptyset( &act.sa_mask );396 397 if ( sigaction( sig, &act, NULL ) == -1 ) {398 __cfaabi_dbg_print_buffer_decl(399 " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",400 sig, errno, strerror( errno )401 );402 _exit( EXIT_FAILURE );403 }404 }405 406 //=============================================================================================407 // Terminating Signals logic408 //=============================================================================================409 410 __cfaabi_dbg_debug_do(411 static void __kernel_backtrace( int start ) {412 // skip first N stack frames413 414 enum { Frames = 50 };415 void * array[Frames];416 int size = backtrace( array, Frames );417 char ** messages = backtrace_symbols( array, size );418 419 // find executable name420 *index( messages[0], '(' ) = '\0';421 #ifdef __USE_STREAM__422 serr | "Stack back trace for:" | messages[0] | endl;423 #else424 fprintf( stderr, "Stack back trace for: %s\n", messages[0]);425 #endif426 427 // skip last 2 stack frames after main428 for ( int i = start; i < size && messages != NULL; i += 1 ) {429 char * name = NULL;430 char * offset_begin = NULL;431 char * offset_end = NULL;432 433 for ( char *p = messages[i]; *p; ++p ) {434 // find parantheses and +offset435 if ( *p == '(' ) {436 name = p;437 }438 else if ( *p == '+' ) {439 offset_begin = p;440 }441 else if ( *p == ')' ) {442 offset_end = p;443 break;444 }445 }446 447 // if line contains symbol print it448 int frameNo = i - start;449 if ( name && offset_begin && offset_end && name < offset_begin ) {450 // delimit strings451 *name++ = '\0';452 *offset_begin++ = '\0';453 *offset_end++ = '\0';454 455 #ifdef __USE_STREAM__456 serr | "(" | frameNo | ")" | messages[i] | ":"457 | name | "+" | offset_begin | offset_end | endl;458 #else459 fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);460 #endif461 }462 // otherwise, print the whole line463 else {464 #ifdef __USE_STREAM__465 serr | "(" | frameNo | ")" | messages[i] | endl;466 #else467 fprintf( stderr, "(%i) %s\n", frameNo, messages[i] );468 #endif469 }470 }471 472 free( messages );473 }474 )475 476 // void sigHandler_segv( __CFA_SIGPARMS__ ) {477 // __cfaabi_dbg_debug_do(478 // #ifdef __USE_STREAM__479 // serr | "*CFA runtime error* program cfa-cpp terminated with"480 // | (sig == SIGSEGV ? "segment fault." : "bus error.")481 // | endl;482 // #else483 // fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );484 // #endif485 486 // // skip first 2 stack frames487 // __kernel_backtrace( 1 );488 // )489 // exit( EXIT_FAILURE );490 // }491 492 // void sigHandler_abort( __CFA_SIGPARMS__ ) {493 // // skip first 6 stack frames494 // __cfaabi_dbg_debug_do( __kernel_backtrace( 6 ); )495 496 // // reset default signal handler497 // __kernel_sigdefault( SIGABRT );498 499 // raise( SIGABRT );500 // }501 502 367 // Local Variables: // 503 368 // mode: c // -
src/libcfa/concurrency/thread.c
rf792cb8 r7416d46a 38 38 self_mon_p = &self_mon; 39 39 next = NULL; 40 __cfaabi_dbg_debug_do( 41 dbg_next = NULL; 42 dbg_prev = NULL; 43 __cfaabi_dbg_thread_register(&this); 44 ) 40 45 41 46 monitors{ &self_mon_p, 1, (fptr_t)0 };
Note:
See TracChangeset
for help on using the changeset viewer.