source: src/libcfa/concurrency/preemption.c@ 59a0bde

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 59a0bde was 6a5be52, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Modified waitfor to sort monitors

  • Property mode set to 100644
File size: 15.8 KB
RevLine 
[c81ebf9]1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// signal.c --
8//
9// Author : Thierry Delisle
10// Created On : Mon Jun 5 14:20:42 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 21 22:36:05 2017
13// Update Count : 2
[c81ebf9]14//
15
[2ac095d]16#include "libhdr.h"
[c81ebf9]17#include "preemption.h"
18
19extern "C" {
[82ff5845]20#include <errno.h>
[1c273d0]21#include <execinfo.h>
[4e6fb8e]22#define __USE_GNU
[c81ebf9]23#include <signal.h>
[4e6fb8e]24#undef __USE_GNU
[82ff5845]25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
[c81ebf9]28}
29
[1c273d0]30
31#ifdef __USE_STREAM__
32#include "fstream"
33#endif
[82ff5845]34
[969b3fe]35//TODO move to defaults
[82ff5845]36#define __CFA_DEFAULT_PREEMPTION__ 10000
[c81ebf9]37
[969b3fe]38//TODO move to defaults
[c81ebf9]39__attribute__((weak)) unsigned int default_preemption() {
40 return __CFA_DEFAULT_PREEMPTION__;
41}
42
[969b3fe]43// Short hands for signal context information
[82ff5845]44#define __CFA_SIGCXT__ ucontext_t *
45#define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt
46
[969b3fe]47// FwdDeclarations : timeout handlers
[c81ebf9]48static void preempt( processor * this );
49static void timeout( thread_desc * this );
50
[969b3fe]51// FwdDeclarations : Signal handlers
[82ff5845]52void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
[1c273d0]53void sigHandler_segv ( __CFA_SIGPARMS__ );
54void sigHandler_abort ( __CFA_SIGPARMS__ );
[82ff5845]55
[969b3fe]56// FwdDeclarations : sigaction wrapper
[82ff5845]57static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );
[cd17862]58
[969b3fe]59// FwdDeclarations : alarm thread main
60void * alarm_loop( __attribute__((unused)) void * args );
61
62// Machine specific register name
[cd17862]63#ifdef __x86_64__
64#define CFA_REG_IP REG_RIP
65#else
66#define CFA_REG_IP REG_EIP
67#endif
68
[969b3fe]69KERNEL_STORAGE(event_kernel_t, event_kernel); // private storage for event kernel
70event_kernel_t * event_kernel; // kernel public handle to even kernel
71static pthread_t alarm_thread; // pthread handle to alarm thread
72
[9236060]73void ?{}(event_kernel_t & this) {
74 (this.alarms){};
75 (this.lock){};
[969b3fe]76}
[82ff5845]77
[c81ebf9]78//=============================================================================================
79// Kernel Preemption logic
80//=============================================================================================
81
[969b3fe]82// Get next expired node
83static inline alarm_node_t * get_expired( alarm_list_t * alarms, __cfa_time_t currtime ) {
84 if( !alarms->head ) return NULL; // If no alarms return null
85 if( alarms->head->alarm >= currtime ) return NULL; // If alarms head not expired return null
86 return pop(alarms); // Otherwise just pop head
87}
88
89// Tick one frame of the Discrete Event Simulation for alarms
[c81ebf9]90void tick_preemption() {
[969b3fe]91 alarm_node_t * node = NULL; // Used in the while loop but cannot be declared in the while condition
92 alarm_list_t * alarms = &event_kernel->alarms; // Local copy for ease of reading
93 __cfa_time_t currtime = __kernel_get_time(); // Check current time once so we everything "happens at once"
[8cb529e]94
[969b3fe]95 //Loop throught every thing expired
96 while( node = get_expired( alarms, currtime ) ) {
[1c273d0]97
[969b3fe]98 // Check if this is a kernel
[c81ebf9]99 if( node->kernel_alarm ) {
100 preempt( node->proc );
101 }
102 else {
103 timeout( node->thrd );
104 }
105
[969b3fe]106 // Check if this is a periodic alarm
[8cb529e]107 __cfa_time_t period = node->period;
108 if( period > 0 ) {
[969b3fe]109 node->alarm = currtime + period; // Alarm is periodic, add currtime to it (used cached current time)
110 insert( alarms, node ); // Reinsert the node for the next time it triggers
[c81ebf9]111 }
112 else {
[969b3fe]113 node->set = false; // Node is one-shot, just mark it as not pending
[c81ebf9]114 }
115 }
116
[969b3fe]117 // If there are still alarms pending, reset the timer
118 if( alarms->head ) { __kernel_set_timer( alarms->head->alarm - currtime ); }
[c81ebf9]119}
120
[969b3fe]121// Update the preemption of a processor and notify interested parties
[c81ebf9]122void update_preemption( processor * this, __cfa_time_t duration ) {
123 alarm_node_t * alarm = this->preemption_alarm;
124
125 // Alarms need to be enabled
126 if ( duration > 0 && !alarm->set ) {
127 alarm->alarm = __kernel_get_time() + duration;
128 alarm->period = duration;
129 register_self( alarm );
130 }
131 // Zero duraction but alarm is set
132 else if ( duration == 0 && alarm->set ) {
133 unregister_self( alarm );
134 alarm->alarm = 0;
135 alarm->period = 0;
136 }
137 // If alarm is different from previous, change it
138 else if ( duration > 0 && alarm->period != duration ) {
139 unregister_self( alarm );
140 alarm->alarm = __kernel_get_time() + duration;
141 alarm->period = duration;
142 register_self( alarm );
143 }
144}
145
146//=============================================================================================
[cd17862]147// Kernel Signal Tools
[c81ebf9]148//=============================================================================================
149
[b227f68]150LIB_DEBUG_DO( static thread_local void * last_interrupt = 0; )
151
[82ff5845]152extern "C" {
[969b3fe]153 // Disable interrupts by incrementing the counter
[82ff5845]154 void disable_interrupts() {
[4e6fb8e]155 __attribute__((unused)) unsigned short new_val = __atomic_add_fetch_2( &disable_preempt_count, 1, __ATOMIC_SEQ_CST );
[969b3fe]156 verify( new_val < 65_000u ); // If this triggers someone is disabling interrupts without enabling them
[82ff5845]157 }
158
[969b3fe]159 // Enable interrupts by decrementing the counter
160 // If counter reaches 0, execute any pending CtxSwitch
[2ac095d]161 void enable_interrupts( DEBUG_CTX_PARAM ) {
[969b3fe]162 processor * proc = this_processor; // Cache the processor now since interrupts can start happening after the atomic add
163 thread_desc * thrd = this_thread; // Cache the thread now since interrupts can start happening after the atomic add
164
[4e6fb8e]165 unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
[969b3fe]166 verify( prev != 0u ); // If this triggers someone is enabled already enabled interruptsverify( prev != 0u );
167
168 // Check if we need to prempt the thread because an interrupt was missed
[1c273d0]169 if( prev == 1 && proc->pending_preemption ) {
170 proc->pending_preemption = false;
171 BlockInternal( thrd );
[82ff5845]172 }
[4e6fb8e]173
[969b3fe]174 // For debugging purposes : keep track of the last person to enable the interrupts
[2ac095d]175 LIB_DEBUG_DO( proc->last_enable = caller; )
[82ff5845]176 }
[969b3fe]177
178 // Disable interrupts by incrementint the counter
179 // Don't execute any pending CtxSwitch even if counter reaches 0
180 void enable_interrupts_noPoll() {
181 __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
182 verify( prev != 0u ); // If this triggers someone is enabled already enabled interrupts
183 }
[82ff5845]184}
185
[969b3fe]186// sigprocmask wrapper : unblock a single signal
[1c273d0]187static inline void signal_unblock( int sig ) {
[82ff5845]188 sigset_t mask;
189 sigemptyset( &mask );
[1c273d0]190 sigaddset( &mask, sig );
[82ff5845]191
[47ecf2b]192 if ( pthread_sigmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
193 abortf( "internal error, pthread_sigmask" );
[cd17862]194 }
[82ff5845]195}
196
[969b3fe]197// sigprocmask wrapper : block a single signal
[cd17862]198static inline void signal_block( int sig ) {
199 sigset_t mask;
200 sigemptyset( &mask );
201 sigaddset( &mask, sig );
[47ecf2b]202
[cd17862]203 if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
204 abortf( "internal error, pthread_sigmask" );
205 }
206}
[47ecf2b]207
[969b3fe]208// kill wrapper : signal a processor
[cd17862]209static void preempt( processor * this ) {
210 pthread_kill( this->kernel_thread, SIGUSR1 );
[1c273d0]211}
[82ff5845]212
[969b3fe]213// reserved for future use
[cd17862]214static void timeout( thread_desc * this ) {
215 //TODO : implement waking threads
216}
217
[969b3fe]218
219// Check if a CtxSwitch signal handler shoud defer
220// If true : preemption is safe
221// If false : preemption is unsafe and marked as pending
222static inline bool preemption_ready() {
223 bool ready = disable_preempt_count == 0 && !preemption_in_progress; // Check if preemption is safe
224 this_processor->pending_preemption = !ready; // Adjust the pending flag accordingly
225 return ready;
226}
227
[cd17862]228//=============================================================================================
229// Kernel Signal Startup/Shutdown logic
230//=============================================================================================
231
[969b3fe]232// Startup routine to activate preemption
233// Called from kernel_startup
[cd17862]234void kernel_start_preemption() {
235 LIB_DEBUG_PRINT_SAFE("Kernel : Starting preemption\n");
[969b3fe]236
237 // Start with preemption disabled until ready
238 disable_preempt_count = 1;
239
240 // Initialize the event kernel
241 event_kernel = (event_kernel_t *)&storage_event_kernel;
[9236060]242 (*event_kernel){};
[969b3fe]243
244 // Setup proper signal handlers
[ed235b6]245 __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART ); // CtxSwitch handler
[969b3fe]246 // __kernel_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO ); // Failure handler
247 // __kernel_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO ); // Failure handler
[cd17862]248
249 signal_block( SIGALRM );
250
251 pthread_create( &alarm_thread, NULL, alarm_loop, NULL );
252}
253
[969b3fe]254// Shutdown routine to deactivate preemption
255// Called from kernel_shutdown
[cd17862]256void kernel_stop_preemption() {
[d6ff3ff]257 LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopping\n");
258
[969b3fe]259 // Block all signals since we are already shutting down
[cd17862]260 sigset_t mask;
261 sigfillset( &mask );
262 sigprocmask( SIG_BLOCK, &mask, NULL );
263
[969b3fe]264 // Notify the alarm thread of the shutdown
[a0b3e32]265 sigval val = { 1 };
266 pthread_sigqueue( alarm_thread, SIGALRM, val );
[969b3fe]267
268 // Wait for the preemption thread to finish
[cd17862]269 pthread_join( alarm_thread, NULL );
[969b3fe]270
271 // Preemption is now fully stopped
272
[cd17862]273 LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopped\n");
274}
275
[969b3fe]276// Raii ctor/dtor for the preemption_scope
277// Used by thread to control when they want to receive preemption signals
[242a902]278void ?{}( preemption_scope & this, processor * proc ) {
[9236060]279 (this.alarm){ proc, zero_time, zero_time };
[242a902]280 this.proc = proc;
281 this.proc->preemption_alarm = &this.alarm;
[969b3fe]282
[9236060]283 update_preemption( this.proc, from_us(this.proc->cltr->preemption) );
[cd17862]284}
285
[242a902]286void ^?{}( preemption_scope & this ) {
[cd17862]287 disable_interrupts();
288
[9236060]289 update_preemption( this.proc, zero_time );
[cd17862]290}
291
292//=============================================================================================
293// Kernel Signal Handlers
294//=============================================================================================
[47ecf2b]295
[969b3fe]296// Context switch signal handler
297// Receives SIGUSR1 signal and causes the current thread to yield
[1c273d0]298void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
[47ecf2b]299 LIB_DEBUG_DO( last_interrupt = (void *)(cxt->uc_mcontext.gregs[CFA_REG_IP]); )
[969b3fe]300
301 // Check if it is safe to preempt here
302 if( !preemption_ready() ) { return; }
303
304 preemption_in_progress = true; // Sync flag : prevent recursive calls to the signal handler
305 signal_unblock( SIGUSR1 ); // We are about to CtxSwitch out of the signal handler, let other handlers in
306 preemption_in_progress = false; // Clear the in progress flag
307
308 // Preemption can occur here
309
310 BlockInternal( (thread_desc*)this_thread ); // Do the actual CtxSwitch
[c81ebf9]311}
312
[969b3fe]313// Main of the alarm thread
314// Waits on SIGALRM and send SIGUSR1 to whom ever needs it
[cd17862]315void * alarm_loop( __attribute__((unused)) void * args ) {
[969b3fe]316 // Block sigalrms to control when they arrive
[cd17862]317 sigset_t mask;
318 sigemptyset( &mask );
319 sigaddset( &mask, SIGALRM );
320
321 if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
322 abortf( "internal error, pthread_sigmask" );
[82ff5845]323 }
[c81ebf9]324
[969b3fe]325 // Main loop
[cd17862]326 while( true ) {
[969b3fe]327 // Wait for a sigalrm
[a0b3e32]328 siginfo_t info;
329 int sig = sigwaitinfo( &mask, &info );
[969b3fe]330
[e2f7bc3]331 if( sig < 0 ) {
332 //Error!
333 int err = errno;
334 switch( err ) {
335 case EAGAIN :
336 case EINTR :
337 continue;
338 case EINVAL :
339 abortf("Timeout was invalid.");
340 default:
341 abortf("Unhandled error %d", err);
342 }
343 }
344
[969b3fe]345 // If another signal arrived something went wrong
[8cb529e]346 assertf(sig == SIGALRM, "Kernel Internal Error, sigwait: Unexpected signal %d (%d : %d)\n", sig, info.si_code, info.si_value.sival_int);
347
[90c4df0]348 // LIB_DEBUG_PRINT_SAFE("Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int );
[969b3fe]349 // Switch on the code (a.k.a. the sender) to
[8cb529e]350 switch( info.si_code )
[a0b3e32]351 {
[969b3fe]352 // Timers can apparently be marked as sent for the kernel
353 // In either case, tick preemption
[8cb529e]354 case SI_TIMER:
355 case SI_KERNEL:
[90c4df0]356 // LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread tick\n");
[e60e0dc]357 lock( &event_kernel->lock DEBUG_CTX2 );
[8cb529e]358 tick_preemption();
[e60e0dc]359 unlock( &event_kernel->lock );
[8cb529e]360 break;
[969b3fe]361 // Signal was not sent by the kernel but by an other thread
[8cb529e]362 case SI_QUEUE:
[969b3fe]363 // For now, other thread only signal the alarm thread to shut it down
364 // If this needs to change use info.si_value and handle the case here
[8cb529e]365 goto EXIT;
[cd17862]366 }
367 }
[a0b3e32]368
[8cb529e]369EXIT:
[a0b3e32]370 LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread stopping\n");
371 return NULL;
[82ff5845]372}
373
[969b3fe]374// Sigaction wrapper : register an signal handler
[82ff5845]375static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {
376 struct sigaction act;
377
378 act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
[1c273d0]379 act.sa_flags = flags;
380
381 if ( sigaction( sig, &act, NULL ) == -1 ) {
[6a5be52]382 LIB_DEBUG_PRINT_BUFFER_DECL(
[1c273d0]383 " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",
384 sig, handler, flags, errno, strerror( errno )
385 );
386 _exit( EXIT_FAILURE );
387 }
388}
389
[969b3fe]390// Sigaction wrapper : restore default handler
[1c273d0]391static void __kernel_sigdefault( int sig ) {
392 struct sigaction act;
393
[969b3fe]394 act.sa_handler = SIG_DFL;
[1c273d0]395 act.sa_flags = 0;
396 sigemptyset( &act.sa_mask );
[82ff5845]397
398 if ( sigaction( sig, &act, NULL ) == -1 ) {
[6a5be52]399 LIB_DEBUG_PRINT_BUFFER_DECL(
[1c273d0]400 " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",
401 sig, errno, strerror( errno )
402 );
[82ff5845]403 _exit( EXIT_FAILURE );
[1c273d0]404 }
405}
406
407//=============================================================================================
408// Terminating Signals logic
409//=============================================================================================
410
411LIB_DEBUG_DO(
412 static void __kernel_backtrace( int start ) {
413 // skip first N stack frames
414
415 enum { Frames = 50 };
416 void * array[Frames];
417 int size = backtrace( array, Frames );
418 char ** messages = backtrace_symbols( array, size );
419
420 // find executable name
421 *index( messages[0], '(' ) = '\0';
422 #ifdef __USE_STREAM__
423 serr | "Stack back trace for:" | messages[0] | endl;
424 #else
425 fprintf( stderr, "Stack back trace for: %s\n", messages[0]);
426 #endif
427
428 // skip last 2 stack frames after main
429 for ( int i = start; i < size && messages != NULL; i += 1 ) {
430 char * name = NULL;
431 char * offset_begin = NULL;
432 char * offset_end = NULL;
433
434 for ( char *p = messages[i]; *p; ++p ) {
435 // find parantheses and +offset
436 if ( *p == '(' ) {
437 name = p;
438 }
439 else if ( *p == '+' ) {
440 offset_begin = p;
441 }
442 else if ( *p == ')' ) {
443 offset_end = p;
444 break;
445 }
446 }
447
448 // if line contains symbol print it
449 int frameNo = i - start;
450 if ( name && offset_begin && offset_end && name < offset_begin ) {
451 // delimit strings
452 *name++ = '\0';
453 *offset_begin++ = '\0';
454 *offset_end++ = '\0';
455
456 #ifdef __USE_STREAM__
457 serr | "(" | frameNo | ")" | messages[i] | ":"
458 | name | "+" | offset_begin | offset_end | endl;
459 #else
460 fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
461 #endif
462 }
463 // otherwise, print the whole line
464 else {
465 #ifdef __USE_STREAM__
466 serr | "(" | frameNo | ")" | messages[i] | endl;
467 #else
468 fprintf( stderr, "(%i) %s\n", frameNo, messages[i] );
469 #endif
470 }
471 }
472
473 free( messages );
474 }
475)
476
[f2b12406]477// void sigHandler_segv( __CFA_SIGPARMS__ ) {
478// LIB_DEBUG_DO(
479// #ifdef __USE_STREAM__
480// serr | "*CFA runtime error* program cfa-cpp terminated with"
481// | (sig == SIGSEGV ? "segment fault." : "bus error.")
482// | endl;
483// #else
484// fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );
485// #endif
486
487// // skip first 2 stack frames
488// __kernel_backtrace( 1 );
489// )
490// exit( EXIT_FAILURE );
491// }
[1c273d0]492
493// void sigHandler_abort( __CFA_SIGPARMS__ ) {
494// // skip first 6 stack frames
495// LIB_DEBUG_DO( __kernel_backtrace( 6 ); )
496
497// // reset default signal handler
498// __kernel_sigdefault( SIGABRT );
499
500// raise( SIGABRT );
[6b0b624]501// }
502
503// Local Variables: //
504// mode: c //
505// tab-width: 4 //
506// End: //
Note: See TracBrowser for help on using the repository browser.