Changes in / [9d85038:667c7da]
- Location:
- src/libcfa/concurrency
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/alarm.c
r9d85038 r667c7da 16 16 17 17 extern "C" { 18 #include <errno.h>19 #include <stdio.h>20 #include <string.h>21 18 #include <time.h> 22 #include <unistd.h>23 19 #include <sys/time.h> 24 20 } … … 26 22 #include "alarm.h" 27 23 #include "kernel_private.h" 28 #include "libhdr.h"29 24 #include "preemption.h" 30 25 … … 36 31 timespec curr; 37 32 clock_gettime( CLOCK_REALTIME, &curr ); 38 __cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec; 39 LIB_DEBUG_DO( 40 char text[256]; 41 __attribute__((unused)) int len = snprintf( text, 256, "Kernel : current time is %lu\n", curr_time ); 42 LIB_DEBUG_WRITE( STDERR_FILENO, text, len ); 43 ); 44 return curr_time; 33 return ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec; 45 34 } 46 35 47 36 void __kernel_set_timer( __cfa_time_t alarm ) { 48 49 LIB_DEBUG_DO(50 char text[256];51 __attribute__((unused)) int len = snprintf( text, 256, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );52 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );53 );54 55 37 itimerval val; 56 38 val.it_value.tv_sec = alarm / TIMEGRAN; // seconds … … 146 128 lock( &systemProcessor->alarm_lock ); 147 129 { 148 bool first = !systemProcessor->alarms.head;149 150 130 insert( &systemProcessor->alarms, this ); 151 131 if( systemProcessor->pending_alarm ) { 152 132 tick_preemption(); 153 }154 if( first ) {155 __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );156 133 } 157 134 } -
src/libcfa/concurrency/invoke.c
r9d85038 r667c7da 30 30 extern void __suspend_internal(void); 31 31 extern void __leave_monitor_desc( struct monitor_desc * this ); 32 extern void disable_interrupts();33 extern void enable_interrupts();34 32 35 33 void CtxInvokeCoroutine( … … 69 67 struct monitor_desc* mon = &thrd->mon; 70 68 cor->state = Active; 71 enable_interrupts();72 69 73 70 // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this); 74 71 main( this ); 75 72 76 disable_interrupts();77 73 __leave_monitor_desc( mon ); 78 74 -
src/libcfa/concurrency/kernel.c
r9d85038 r667c7da 154 154 (&this->terminated){}; 155 155 this->is_terminated = false; 156 this->preemption_alarm = NULL; 157 this->preemption = default_preemption(); 158 this->disable_preempt_count = 1; 156 this->disable_preempt_count = 0; 159 157 this->pending_preemption = false; 160 this->kernel_thread = pthread_self();161 158 162 159 this->runner = runner; 163 LIB_DEBUG_PRINT_SAFE("Kernel : constructing systemprocessor context %p\n", runner);160 LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner); 164 161 runner{ this }; 165 162 } … … 243 240 //Update global state 244 241 this->current_thread = dst; 245 246 LIB_DEBUG_PRINT_SAFE("Kernel : running %p\n", dst);247 242 248 243 // Context Switch to the thread … … 327 322 void start(processor * this) { 328 323 LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this); 329 330 // SIGALRM must only be caught by the system processor 331 sigset_t old_mask; 332 bool is_system_proc = this_processor == &systemProcessor->proc; 333 if ( is_system_proc ) { 334 // Child kernel-thread inherits the signal mask from the parent kernel-thread. So one special case for the 335 // system processor creating the user processor => toggle the blocking SIGALRM on system processor, create user 336 // processor, and toggle back (below) previous signal mask of the system processor. 337 338 sigset_t new_mask; 339 sigemptyset( &new_mask ); 340 sigemptyset( &old_mask ); 341 sigaddset( &new_mask, SIGALRM ); 342 343 if ( sigprocmask( SIG_BLOCK, &new_mask, &old_mask ) == -1 ) { 344 abortf( "internal error, sigprocmask" ); 345 } 346 347 assert( ! sigismember( &old_mask, SIGALRM ) ); 348 } 349 324 350 325 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this ); 351 352 // Toggle back previous signal mask of system processor.353 if ( is_system_proc ) {354 if ( sigprocmask( SIG_SETMASK, &old_mask, NULL ) == -1 ) {355 abortf( "internal error, sigprocmask" );356 } // if357 } // if358 326 359 327 LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this); … … 379 347 } 380 348 381 void BlockInternal() { 382 disable_interrupts(); 349 void ScheduleInternal() { 383 350 suspend(); 384 enable_interrupts(); 385 } 386 387 void BlockInternal( spinlock * lock ) { 388 disable_interrupts(); 351 } 352 353 void ScheduleInternal( spinlock * lock ) { 389 354 this_processor->finish.action_code = Release; 390 355 this_processor->finish.lock = lock; 391 356 suspend(); 392 enable_interrupts(); 393 } 394 395 void BlockInternal( thread_desc * thrd ) { 396 disable_interrupts(); 357 } 358 359 void ScheduleInternal( thread_desc * thrd ) { 397 360 this_processor->finish.action_code = Schedule; 398 361 this_processor->finish.thrd = thrd; 399 362 suspend(); 400 enable_interrupts(); 401 } 402 403 void BlockInternal( spinlock * lock, thread_desc * thrd ) { 404 disable_interrupts(); 363 } 364 365 void ScheduleInternal( spinlock * lock, thread_desc * thrd ) { 405 366 this_processor->finish.action_code = Release_Schedule; 406 367 this_processor->finish.lock = lock; 407 368 this_processor->finish.thrd = thrd; 408 369 suspend(); 409 enable_interrupts(); 410 } 411 412 void BlockInternal(spinlock ** locks, unsigned short count) { 413 disable_interrupts(); 370 } 371 372 void ScheduleInternal(spinlock ** locks, unsigned short count) { 414 373 this_processor->finish.action_code = Release_Multi; 415 374 this_processor->finish.locks = locks; 416 375 this_processor->finish.lock_count = count; 417 376 suspend(); 418 enable_interrupts(); 419 } 420 421 void BlockInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) { 422 disable_interrupts(); 377 } 378 379 void ScheduleInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) { 423 380 this_processor->finish.action_code = Release_Multi_Schedule; 424 381 this_processor->finish.locks = locks; … … 427 384 this_processor->finish.thrd_count = thrd_count; 428 385 suspend(); 429 enable_interrupts();430 386 } 431 387 … … 447 403 LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n"); 448 404 405 // Enable preemption 406 kernel_start_preemption(); 407 449 408 // Initialize the system cluster 450 409 systemCluster = (cluster *)&systemCluster_storage; … … 467 426 this_processor->current_coroutine = &mainThread->cor; 468 427 469 // Enable preemption470 kernel_start_preemption();471 472 428 // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX 473 429 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that … … 479 435 // THE SYSTEM IS NOW COMPLETELY RUNNING 480 436 LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n"); 481 482 enable_interrupts();483 437 } 484 438 … … 493 447 494 448 // THE SYSTEM IS NOW COMPLETELY STOPPED 495 496 // Disable preemption497 kernel_stop_preemption();498 449 499 450 // Destroy the system processor and its context in reverse order of construction … … 599 550 if( !this->cond ) { 600 551 append( &this->blocked, this_thread() ); 601 BlockInternal( &this->lock );552 ScheduleInternal( &this->lock ); 602 553 lock( &this->lock ); 603 554 } -
src/libcfa/concurrency/kernel_private.h
r9d85038 r667c7da 28 28 thread_desc * nextThread(cluster * this); 29 29 30 void BlockInternal(void);31 void BlockInternal(spinlock * lock);32 void BlockInternal(thread_desc * thrd);33 void BlockInternal(spinlock * lock, thread_desc * thrd);34 void BlockInternal(spinlock ** locks, unsigned short count);35 void BlockInternal(spinlock ** locks, unsigned short count, thread_desc ** thrds, unsigned short thrd_count);30 void ScheduleInternal(void); 31 void ScheduleInternal(spinlock * lock); 32 void ScheduleInternal(thread_desc * thrd); 33 void ScheduleInternal(spinlock * lock, thread_desc * thrd); 34 void ScheduleInternal(spinlock ** locks, unsigned short count); 35 void ScheduleInternal(spinlock ** locks, unsigned short count, thread_desc ** thrds, unsigned short thrd_count); 36 36 37 37 //----------------------------------------------------------------------------- … … 60 60 extern thread_local processor * this_processor; 61 61 62 extern "C" { 63 void disable_interrupts(); 64 void enable_interrupts_noRF(); 65 void enable_interrupts(); 62 static inline void disable_interrupts() { 63 __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, 1, __ATOMIC_SEQ_CST ); 64 assert( prev != (unsigned short) -1 ); 65 } 66 67 static inline void enable_interrupts_noRF() { 68 unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, -1, __ATOMIC_SEQ_CST ); 69 assert( prev != (unsigned short) 0 ); 70 } 71 72 static inline void enable_interrupts() { 73 unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, -1, __ATOMIC_SEQ_CST ); 74 assert( prev != (unsigned short) 0 ); 75 if( prev == 1 && this_processor->pending_preemption ) { 76 ScheduleInternal( this_processor->current_thread ); 77 this_processor->pending_preemption = false; 78 } 66 79 } 67 80 -
src/libcfa/concurrency/monitor.c
r9d85038 r667c7da 63 63 append( &this->entry_queue, thrd ); 64 64 LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd); 65 BlockInternal( &this->lock );66 67 // BlockInternal will unlock spinlock, no need to unlock ourselves65 ScheduleInternal( &this->lock ); 66 67 //ScheduleInternal will unlock spinlock, no need to unlock ourselves 68 68 return; 69 69 } … … 172 172 unsigned short count = this->monitor_count; 173 173 unsigned int recursions[ count ]; //Save the current recursion levels to restore them later 174 spinlock * locks [ count ]; //We need to pass-in an array of locks to BlockInternal174 spinlock * locks [ count ]; //We need to pass-in an array of locks to ScheduleInternal 175 175 176 176 LIB_DEBUG_PRINT_SAFE("count %i\n", count); … … 210 210 211 211 // Everything is ready to go to sleep 212 BlockInternal( locks, count, threads, thread_count );212 ScheduleInternal( locks, count, threads, thread_count ); 213 213 214 214 … … 283 283 unsigned short count = this->monitor_count; 284 284 unsigned int recursions[ count ]; //Save the current recursion levels to restore them later 285 spinlock * locks [ count ]; //We need to pass-in an array of locks to BlockInternal285 spinlock * locks [ count ]; //We need to pass-in an array of locks to ScheduleInternal 286 286 287 287 lock_all( this->monitors, locks, count ); … … 311 311 312 312 //Everything is ready to go to sleep 313 BlockInternal( locks, count, &signallee, 1 );313 ScheduleInternal( locks, count, &signallee, 1 ); 314 314 315 315 … … 343 343 // unsigned short count = this->current_monitor_count; 344 344 // unsigned int recursions[ count ]; //Save the current recursion levels to restore them later 345 // spinlock * locks [ count ]; //We need to pass-in an array of locks to BlockInternal345 // spinlock * locks [ count ]; //We need to pass-in an array of locks to ScheduleInternal 346 346 347 347 // lock_all( this->current_monitors, locks, count ); … … 352 352 353 353 // // // Everything is ready to go to sleep 354 // // BlockInternal( locks, count, threads, thread_count );354 // // ScheduleInternal( locks, count, threads, thread_count ); 355 355 356 356 -
src/libcfa/concurrency/preemption.c
r9d85038 r667c7da 18 18 19 19 extern "C" { 20 #include <errno.h>21 20 #include <signal.h> 22 #include <stdio.h>23 #include <string.h>24 #include <unistd.h>25 21 } 26 22 27 #include "libhdr.h" 28 29 #define __CFA_DEFAULT_PREEMPTION__ 10000 23 #define __CFA_DEFAULT_PREEMPTION__ 10 30 24 31 25 __attribute__((weak)) unsigned int default_preemption() { … … 33 27 } 34 28 35 #define __CFA_SIGCXT__ ucontext_t *36 #define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt37 38 29 static void preempt( processor * this ); 39 30 static void timeout( thread_desc * this ); 40 41 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );42 void sigHandler_alarm ( __CFA_SIGPARMS__ );43 44 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );45 31 46 32 //============================================================================================= … … 49 35 50 36 void kernel_start_preemption() { 51 LIB_DEBUG_PRINT_SAFE("Kernel : Starting preemption\n");52 __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO );53 __kernel_sigaction( SIGALRM, sigHandler_alarm , SA_SIGINFO );54 }55 37 56 void kernel_stop_preemption() {57 //Block all signals, we are no longer in a position to handle them58 sigset_t mask;59 sigfillset( &mask );60 sigprocmask( SIG_BLOCK, &mask, NULL );61 LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopped\n");62 38 } 63 39 64 40 void tick_preemption() { 65 LIB_DEBUG_DO(66 char text[256];67 __attribute__((unused)) int len = snprintf( text, 256, "Ticking preemption\n" );68 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );69 );70 71 41 alarm_list_t * alarms = &systemProcessor->alarms; 72 42 __cfa_time_t currtime = __kernel_get_time(); 73 43 while( alarms->head && alarms->head->alarm < currtime ) { 74 44 alarm_node_t * node = pop(alarms); 75 LIB_DEBUG_DO(76 len = snprintf( text, 256, "Ticking %p\n", node );77 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );78 );79 45 if( node->kernel_alarm ) { 80 46 preempt( node->proc ); … … 85 51 86 52 if( node->period > 0 ) { 87 node->alarm = currtime +node->period;53 node->alarm += node->period; 88 54 insert( alarms, node ); 89 55 } … … 96 62 __kernel_set_timer( alarms->head->alarm - currtime ); 97 63 } 98 99 LIB_DEBUG_DO(100 len = snprintf( text, 256, "Ticking preemption done\n" );101 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );102 );103 64 } 104 65 105 66 void update_preemption( processor * this, __cfa_time_t duration ) { 106 LIB_DEBUG_DO( 107 char text[256]; 108 __attribute__((unused)) int len = snprintf( text, 256, "Processor : updating preemption to %lu\n", duration ); 109 LIB_DEBUG_WRITE( STDERR_FILENO, text, len ); 110 ); 111 67 // assert( THREAD_GETMEM( disableInt ) && THREAD_GETMEM( disableIntCnt ) == 1 ); 112 68 alarm_node_t * alarm = this->preemption_alarm; 113 duration *= 1000;114 69 115 70 // Alarms need to be enabled … … 139 94 this->proc->preemption_alarm = &this->alarm; 140 95 update_preemption( this->proc, this->proc->preemption ); 141 142 // enable_interrupts();143 96 } 144 97 145 98 void ^?{}( preemption_scope * this ) { 146 disable_interrupts();147 148 99 update_preemption( this->proc, 0 ); 149 100 } … … 152 103 // Kernel Signal logic 153 104 //============================================================================================= 154 155 extern "C" {156 void disable_interrupts() {157 __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, 1, __ATOMIC_SEQ_CST );158 assert( prev != (unsigned short) -1 );159 }160 161 void enable_interrupts_noRF() {162 unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, -1, __ATOMIC_SEQ_CST );163 assert( prev != (unsigned short) 0 );164 }165 166 void enable_interrupts() {167 unsigned short prev = __atomic_fetch_add_2( &this_processor->disable_preempt_count, -1, __ATOMIC_SEQ_CST );168 assert( prev != (unsigned short) 0 );169 if( prev == 1 && this_processor->pending_preemption ) {170 this_processor->pending_preemption = false;171 LIB_DEBUG_DO(172 char text[256];173 __attribute__((unused)) int len = snprintf( text, 256, "Executing deferred CtxSwitch\n" );174 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );175 );176 BlockInternal( this_processor->current_thread );177 }178 }179 }180 181 static inline void signal_unblock( bool alarm ) {182 sigset_t mask;183 sigemptyset( &mask );184 sigaddset( &mask, SIGUSR1 );185 186 if( alarm ) sigaddset( &mask, SIGALRM );187 188 if ( sigprocmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {189 abortf( "internal error, sigprocmask" );190 } // if191 }192 105 193 106 static inline bool preemption_ready() { … … 203 116 } 204 117 205 void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) { 206 207 LIB_DEBUG_DO( 208 char text[256]; 209 __attribute__((unused)) int len = snprintf( text, 256, "Ctx Switch IRH\n" ); 210 LIB_DEBUG_WRITE( STDERR_FILENO, text, len ); 211 ); 212 213 signal_unblock( false ); 118 void sigHandler_ctxSwitch( __attribute__((unused)) int sig ) { 214 119 if( preemption_ready() ) { 215 LIB_DEBUG_DO( 216 len = snprintf( text, 256, "Ctx Switch IRH : Blocking thread\n" ); 217 LIB_DEBUG_WRITE( STDERR_FILENO, text, len ); 218 ); 219 BlockInternal( this_processor->current_thread ); 120 ScheduleInternal( this_processor->current_thread ); 220 121 } 221 122 else { 222 LIB_DEBUG_DO(223 len = snprintf( text, 256, "Ctx Switch IRH : Defering\n" );224 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );225 );226 123 defer_ctxSwitch(); 227 124 } 228 125 } 229 126 230 void sigHandler_alarm( __CFA_SIGPARMS__ ) { 231 232 LIB_DEBUG_DO( 233 char text[256]; 234 __attribute__((unused)) int len = snprintf( text, 256, "\nAlarm IRH\n" ); 235 LIB_DEBUG_WRITE( STDERR_FILENO, text, len ); 236 ); 237 238 signal_unblock( true ); 127 void sigHandler_alarm( __attribute__((unused)) int sig ) { 239 128 if( try_lock( &systemProcessor->alarm_lock ) ) { 240 129 tick_preemption(); … … 244 133 defer_alarm(); 245 134 } 246 247 if( preemption_ready() && this_processor->pending_preemption ) {248 LIB_DEBUG_DO(249 len = snprintf( text, 256, "Alarm IRH : Blocking thread\n" );250 LIB_DEBUG_WRITE( STDERR_FILENO, text, len );251 );252 this_processor->pending_preemption = false;253 BlockInternal( this_processor->current_thread );254 }255 135 } 256 136 257 137 static void preempt( processor * this ) { 258 LIB_DEBUG_DO( 259 char text[256]; 260 __attribute__((unused)) int len = snprintf( text, 256, "Processor : signalling %p\n", this ); 261 LIB_DEBUG_WRITE( STDERR_FILENO, text, len ); 262 ); 263 264 if( this != systemProcessor ) { 265 pthread_kill( this->kernel_thread, SIGUSR1 ); 266 } 267 else { 268 defer_ctxSwitch(); 269 } 138 pthread_kill( this->kernel_thread, SIGUSR1 ); 270 139 } 271 140 … … 273 142 //TODO : implement waking threads 274 143 } 275 276 static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {277 struct sigaction act;278 279 act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;280 sigemptyset( &act.sa_mask );281 sigaddset( &act.sa_mask, SIGALRM ); // disabled during signal handler282 sigaddset( &act.sa_mask, SIGUSR1 );283 284 act.sa_flags = flags;285 286 if ( sigaction( sig, &act, NULL ) == -1 ) {287 // THE KERNEL IS NOT STARTED SO CALL NO uC++ ROUTINES!288 char helpText[256];289 __attribute__((unused)) int len = snprintf( helpText, 256, " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",290 sig, handler, flags, errno, strerror( errno ) );291 LIB_DEBUG_WRITE( STDERR_FILENO, helpText, len );292 _exit( EXIT_FAILURE );293 } // if294 } -
src/libcfa/concurrency/thread.c
r9d85038 r667c7da 84 84 85 85 void yield( void ) { 86 BlockInternal( this_processor->current_thread );86 ScheduleInternal( this_processor->current_thread ); 87 87 } 88 88
Note:
See TracChangeset
for help on using the changeset viewer.