- File:
-
- 1 edited
-
src/libcfa/concurrency/preemption.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/preemption.c
r1c273d0 rc5ac6d5 17 17 #include "preemption.h" 18 18 19 20 19 extern "C" { 21 #include <errno.h>22 #include <execinfo.h>23 #define __USE_GNU24 20 #include <signal.h> 25 #undef __USE_GNU26 #include <stdio.h>27 #include <string.h>28 #include <unistd.h>29 21 } 30 22 31 32 #ifdef __USE_STREAM__ 33 #include "fstream" 34 #endif 35 #include "libhdr.h" 36 37 #define __CFA_DEFAULT_PREEMPTION__ 10000 23 #define __CFA_DEFAULT_PREEMPTION__ 10 38 24 39 25 __attribute__((weak)) unsigned int default_preemption() { … … 41 27 } 42 28 43 #define __CFA_SIGCXT__ ucontext_t *44 #define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt45 46 29 static void preempt( processor * this ); 47 30 static void timeout( thread_desc * this ); 48 49 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );50 void sigHandler_alarm ( __CFA_SIGPARMS__ );51 void sigHandler_segv ( __CFA_SIGPARMS__ );52 void sigHandler_abort ( __CFA_SIGPARMS__ );53 54 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );55 31 56 32 //============================================================================================= … … 59 35 60 36 void kernel_start_preemption() { 61 LIB_DEBUG_PRINT_SAFE("Kernel : Starting preemption\n"); 62 __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO ); 63 __kernel_sigaction( SIGALRM, sigHandler_alarm , SA_SIGINFO ); 64 __kernel_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO ); 65 __kernel_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO ); 66 // __kernel_sigaction( SIGABRT, sigHandler_abort , SA_SIGINFO ); 37 67 38 } 68 39 69 void kernel_stop_preemption() {70 //Block all signals, we are no longer in a position to handle them71 sigset_t mask;72 sigfillset( &mask );73 sigprocmask( SIG_BLOCK, &mask, NULL );74 LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopped\n");75 76 // assert( !systemProcessor->alarms.head );77 // assert( systemProcessor->alarms.tail == &systemProcessor->alarms.head );78 }79 80 LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )81 82 40 void tick_preemption() { 83 // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ticking preemption\n" );84 85 41 alarm_list_t * alarms = &systemProcessor->alarms; 86 42 __cfa_time_t currtime = __kernel_get_time(); 87 43 while( alarms->head && alarms->head->alarm < currtime ) { 88 44 alarm_node_t * node = pop(alarms); 89 // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking %p\n", node );90 91 45 if( node->kernel_alarm ) { 92 46 preempt( node->proc ); … … 96 50 } 97 51 98 verify( validate( alarms ) );99 100 52 if( node->period > 0 ) { 101 node->alarm = currtime +node->period;53 node->alarm += node->period; 102 54 insert( alarms, node ); 103 55 } … … 110 62 __kernel_set_timer( alarms->head->alarm - currtime ); 111 63 } 112 113 verify( validate( alarms ) );114 // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ticking preemption done\n" );115 64 } 116 65 117 66 void update_preemption( processor * this, __cfa_time_t duration ) { 118 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p updating preemption to %lu\n", this, duration ); 119 67 // assert( THREAD_GETMEM( disableInt ) && THREAD_GETMEM( disableIntCnt ) == 1 ); 120 68 alarm_node_t * alarm = this->preemption_alarm; 121 duration *= 1000;122 69 123 70 // Alarms need to be enabled … … 150 97 151 98 void ^?{}( preemption_scope * this ) { 152 disable_interrupts();153 154 99 update_preemption( this->proc, 0 ); 155 100 } … … 159 104 //============================================================================================= 160 105 161 extern "C" {162 void disable_interrupts() {163 __attribute__((unused)) unsigned short new_val = __atomic_add_fetch_2( &disable_preempt_count, 1, __ATOMIC_SEQ_CST );164 verify( new_val < (unsigned short)65_000 );165 verify( new_val != (unsigned short) 0 );166 }167 168 void enable_interrupts_noRF() {169 unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );170 verify( prev != (unsigned short) 0 );171 }172 173 void enable_interrupts( const char * func ) {174 processor * proc = this_processor;175 thread_desc * thrd = this_thread;176 unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );177 verify( prev != (unsigned short) 0 );178 if( prev == 1 && proc->pending_preemption ) {179 proc->pending_preemption = false;180 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Executing deferred CtxSwitch on %p\n", this_processor );181 BlockInternal( thrd );182 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Executing deferred back\n" );183 }184 185 proc->last_enable = func;186 }187 }188 189 static inline void signal_unblock( int sig ) {190 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : %p unblocking sig %i\n", this_processor, sig );191 192 sigset_t mask;193 sigemptyset( &mask );194 sigaddset( &mask, sig );195 196 if ( sigprocmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {197 abortf( "internal error, sigprocmask" );198 } // if199 }200 201 106 static inline bool preemption_ready() { 202 return disable_preempt_count == 0;107 return this_processor->disable_preempt_count == 0; 203 108 } 204 109 … … 211 116 } 212 117 213 extern "C" { 214 __attribute__((noinline)) void __debug_break() { 215 pthread_kill( pthread_self(), SIGTRAP ); 118 void sigHandler_ctxSwitch( __attribute__((unused)) int sig ) { 119 if( preemption_ready() ) { 120 ScheduleInternal( this_processor->current_thread ); 121 } 122 else { 123 defer_ctxSwitch(); 216 124 } 217 125 } 218 126 219 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) { 220 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Ctx Switch IRH %p running %p @ %p\n", this_processor, this_thread, (void *)(cxt->uc_mcontext.gregs[REG_RIP]) ); 221 222 if( preemption_ready() ) { 223 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Blocking thread %p on %p\n", this_thread, this_processor ); 224 signal_unblock( SIGUSR1 ); 225 BlockInternal( (thread_desc*)this_thread ); 226 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Back\n\n"); 227 } 228 else { 229 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Ctx Switch IRH : Defering\n" ); 230 defer_ctxSwitch(); 231 signal_unblock( SIGUSR1 ); 232 } 233 } 234 235 void sigHandler_alarm( __CFA_SIGPARMS__ ) { 236 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "\nAlarm IRH %p running %p @ %p\n", this_processor, this_thread, (void *)(cxt->uc_mcontext.gregs[REG_RIP]) ); 237 238 // if( ((intptr_t)cxt->uc_mcontext.gregs[REG_RIP]) > 0xFFFFFF ) __debug_break(); 239 240 if( try_lock( &systemProcessor->alarm_lock, __PRETTY_FUNCTION__ ) ) { 127 void sigHandler_alarm( __attribute__((unused)) int sig ) { 128 if( try_lock( &systemProcessor->alarm_lock ) ) { 241 129 tick_preemption(); 242 130 unlock( &systemProcessor->alarm_lock ); … … 245 133 defer_alarm(); 246 134 } 247 248 signal_unblock( SIGALRM );249 250 if( preemption_ready() && this_processor->pending_preemption ) {251 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Alarm IRH : Blocking thread %p on %p\n", this_thread, this_processor );252 this_processor->pending_preemption = false;253 BlockInternal( (thread_desc*)this_thread );254 LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Alarm Switch IRH : Back\n\n");255 }256 135 } 257 136 258 137 static void preempt( processor * this ) { 259 // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Processor : signalling %p\n", this ); 260 261 if( this != systemProcessor ) { 262 pthread_kill( this->kernel_thread, SIGUSR1 ); 263 } 264 else { 265 defer_ctxSwitch(); 266 } 138 pthread_kill( this->kernel_thread, SIGUSR1 ); 267 139 } 268 140 … … 270 142 //TODO : implement waking threads 271 143 } 272 273 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {274 struct sigaction act;275 276 act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;277 act.sa_flags = flags;278 279 // disabled during signal handler280 sigemptyset( &act.sa_mask );281 sigaddset( &act.sa_mask, sig );282 283 if ( sigaction( sig, &act, NULL ) == -1 ) {284 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO,285 " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",286 sig, handler, flags, errno, strerror( errno )287 );288 _exit( EXIT_FAILURE );289 }290 }291 292 typedef void (*sa_handler_t)(int);293 294 static void __kernel_sigdefault( int sig ) {295 struct sigaction act;296 297 // act.sa_handler = SIG_DFL;298 act.sa_flags = 0;299 sigemptyset( &act.sa_mask );300 301 if ( sigaction( sig, &act, NULL ) == -1 ) {302 LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO,303 " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",304 sig, errno, strerror( errno )305 );306 _exit( EXIT_FAILURE );307 }308 }309 310 //=============================================================================================311 // Terminating Signals logic312 //=============================================================================================313 314 LIB_DEBUG_DO(315 static void __kernel_backtrace( int start ) {316 // skip first N stack frames317 318 enum { Frames = 50 };319 void * array[Frames];320 int size = backtrace( array, Frames );321 char ** messages = backtrace_symbols( array, size );322 323 // find executable name324 *index( messages[0], '(' ) = '\0';325 #ifdef __USE_STREAM__326 serr | "Stack back trace for:" | messages[0] | endl;327 #else328 fprintf( stderr, "Stack back trace for: %s\n", messages[0]);329 #endif330 331 // skip last 2 stack frames after main332 for ( int i = start; i < size && messages != NULL; i += 1 ) {333 char * name = NULL;334 char * offset_begin = NULL;335 char * offset_end = NULL;336 337 for ( char *p = messages[i]; *p; ++p ) {338 // find parantheses and +offset339 if ( *p == '(' ) {340 name = p;341 }342 else if ( *p == '+' ) {343 offset_begin = p;344 }345 else if ( *p == ')' ) {346 offset_end = p;347 break;348 }349 }350 351 // if line contains symbol print it352 int frameNo = i - start;353 if ( name && offset_begin && offset_end && name < offset_begin ) {354 // delimit strings355 *name++ = '\0';356 *offset_begin++ = '\0';357 *offset_end++ = '\0';358 359 #ifdef __USE_STREAM__360 serr | "(" | frameNo | ")" | messages[i] | ":"361 | name | "+" | offset_begin | offset_end | endl;362 #else363 fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);364 #endif365 }366 // otherwise, print the whole line367 else {368 #ifdef __USE_STREAM__369 serr | "(" | frameNo | ")" | messages[i] | endl;370 #else371 fprintf( stderr, "(%i) %s\n", frameNo, messages[i] );372 #endif373 }374 }375 376 free( messages );377 }378 )379 380 void sigHandler_segv( __CFA_SIGPARMS__ ) {381 LIB_DEBUG_DO(382 #ifdef __USE_STREAM__383 serr | "*CFA runtime error* program cfa-cpp terminated with"384 | (sig == SIGSEGV ? "segment fault." : "bus error.")385 | endl;386 #else387 fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );388 #endif389 390 // skip first 2 stack frames391 __kernel_backtrace( 1 );392 )393 exit( EXIT_FAILURE );394 }395 396 // void sigHandler_abort( __CFA_SIGPARMS__ ) {397 // // skip first 6 stack frames398 // LIB_DEBUG_DO( __kernel_backtrace( 6 ); )399 400 // // reset default signal handler401 // __kernel_sigdefault( SIGABRT );402 403 // raise( SIGABRT );404 // }
Note:
See TracChangeset
for help on using the changeset viewer.