Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/preemption.cfa

    rac2b598 r2026bb6  
    1010// Created On       : Mon Jun 5 14:20:42 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Dec  5 16:34:05 2019
    13 // Update Count     : 43
     12// Last Modified On : Tue Jun  5 17:35:49 2018
     13// Update Count     : 37
    1414//
    1515
     
    2424#include <string.h>
    2525#include <unistd.h>
    26 #include <limits.h>                                                                             // PTHREAD_STACK_MIN
    2726}
    2827
     
    3938// FwdDeclarations : timeout handlers
    4039static void preempt( processor   * this );
    41 static void timeout( $thread * this );
     40static void timeout( thread_desc * this );
    4241
    4342// FwdDeclarations : Signal handlers
     
    6564event_kernel_t * event_kernel;                        // kernel public handle to even kernel
    6665static pthread_t alarm_thread;                        // pthread handle to alarm thread
    67 static void * alarm_stack;                                                        // pthread stack for alarm thread
    6866
    6967static void ?{}(event_kernel_t & this) with( this ) {
     
    8381// Get next expired node
    8482static inline alarm_node_t * get_expired( alarm_list_t * alarms, Time currtime ) {
    85         if( !alarms->head ) return 0p;                                          // If no alarms return null
    86         if( alarms->head->alarm >= currtime ) return 0p;        // If alarms head not expired return null
    87         return pop(alarms);                                                                     // Otherwise just pop head
     83        if( !alarms->head ) return NULL;                          // If no alarms return null
     84        if( alarms->head->alarm >= currtime ) return NULL;        // If alarms head not expired return null
     85        return pop(alarms);                                       // Otherwise just pop head
    8886}
    8987
    9088// Tick one frame of the Discrete Event Simulation for alarms
    9189static void tick_preemption() {
    92         alarm_node_t * node = 0p;                                                       // Used in the while loop but cannot be declared in the while condition
    93         alarm_list_t * alarms = &event_kernel->alarms;          // Local copy for ease of reading
    94         Time currtime = __kernel_get_time();                            // Check current time once so everything "happens at once"
     90        alarm_node_t * node = NULL;                     // Used in the while loop but cannot be declared in the while condition
     91        alarm_list_t * alarms = &event_kernel->alarms;  // Local copy for ease of reading
     92        Time currtime = __kernel_get_time();                    // Check current time once so we everything "happens at once"
    9593
    9694        //Loop throught every thing expired
     
    184182
    185183        // Enable interrupts by decrementing the counter
    186         // If counter reaches 0, execute any pending __cfactx_switch
     184        // If counter reaches 0, execute any pending CtxSwitch
    187185        void enable_interrupts( __cfaabi_dbg_ctx_param ) {
    188186                processor   * proc = kernelTLS.this_processor; // Cache the processor now since interrupts can start happening after the atomic store
     187                thread_desc * thrd = kernelTLS.this_thread;       // Cache the thread now since interrupts can start happening after the atomic store
    189188
    190189                with( kernelTLS.preemption_state ){
     
    208207                                if( proc->pending_preemption ) {
    209208                                        proc->pending_preemption = false;
    210                                         force_yield( __POLL_PREEMPTION );
     209                                        BlockInternal( thrd );
    211210                                }
    212211                        }
     
    218217
    219218        // Disable interrupts by incrementint the counter
    220         // Don't execute any pending __cfactx_switch even if counter reaches 0
     219        // Don't execute any pending CtxSwitch even if counter reaches 0
    221220        void enable_interrupts_noPoll() {
    222221                unsigned short prev = kernelTLS.preemption_state.disable_count;
     
    244243        sigaddset( &mask, sig );
    245244
    246         if ( pthread_sigmask( SIG_UNBLOCK, &mask, 0p ) == -1 ) {
     245        if ( pthread_sigmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
    247246            abort( "internal error, pthread_sigmask" );
    248247        }
     
    255254        sigaddset( &mask, sig );
    256255
    257         if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
     256        if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
    258257            abort( "internal error, pthread_sigmask" );
    259258        }
     
    267266
    268267// reserved for future use
    269 static void timeout( $thread * this ) {
     268static void timeout( thread_desc * this ) {
    270269        //TODO : implement waking threads
    271270}
    272271
    273272// KERNEL ONLY
    274 // Check if a __cfactx_switch signal handler shoud defer
     273// Check if a CtxSwitch signal handler shoud defer
    275274// If true  : preemption is safe
    276275// If false : preemption is unsafe and marked as pending
     
    302301
    303302        // Setup proper signal handlers
    304         __cfaabi_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART ); // __cfactx_switch handler
     303        __cfaabi_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART );         // CtxSwitch handler
    305304
    306305        signal_block( SIGALRM );
    307306
    308         alarm_stack = __create_pthread( &alarm_thread, alarm_loop, 0p );
     307        pthread_create( &alarm_thread, NULL, alarm_loop, NULL );
    309308}
    310309
     
    317316        sigset_t mask;
    318317        sigfillset( &mask );
    319         sigprocmask( SIG_BLOCK, &mask, 0p );
     318        sigprocmask( SIG_BLOCK, &mask, NULL );
    320319
    321320        // Notify the alarm thread of the shutdown
     
    324323
    325324        // Wait for the preemption thread to finish
    326 
    327         pthread_join( alarm_thread, 0p );
    328         free( alarm_stack );
     325        pthread_join( alarm_thread, NULL );
    329326
    330327        // Preemption is now fully stopped
     
    383380        static_assert( sizeof( sigset_t ) == sizeof( cxt->uc_sigmask ), "Expected cxt->uc_sigmask to be of sigset_t" );
    384381        #endif
    385         if ( pthread_sigmask( SIG_SETMASK, (sigset_t *)&(cxt->uc_sigmask), 0p ) == -1 ) {
     382        if ( pthread_sigmask( SIG_SETMASK, (sigset_t *)&(cxt->uc_sigmask), NULL ) == -1 ) {
    386383                abort( "internal error, sigprocmask" );
    387384        }
     
    393390        // Preemption can occur here
    394391
    395         force_yield( __ALARM_PREEMPTION ); // Do the actual __cfactx_switch
     392        BlockInternal( kernelTLS.this_thread ); // Do the actual CtxSwitch
    396393}
    397394
     
    402399        sigset_t mask;
    403400        sigfillset(&mask);
    404         if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
     401        if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
    405402            abort( "internal error, pthread_sigmask" );
    406403        }
     
    423420                                        {__cfaabi_dbg_print_buffer_decl( " KERNEL: Spurious wakeup %d.\n", err );}
    424421                                        continue;
    425                                 case EINVAL :
     422                        case EINVAL :
    426423                                        abort( "Timeout was invalid." );
    427424                                default:
     
    456453EXIT:
    457454        __cfaabi_dbg_print_safe( "Kernel : Preemption thread stopping\n" );
    458         return 0p;
     455        return NULL;
    459456}
    460457
     
    469466        sigset_t oldset;
    470467        int ret;
    471         ret = pthread_sigmask(0, 0p, &oldset);
     468        ret = pthread_sigmask(0, NULL, &oldset);
    472469        if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); }
    473470
Note: See TracChangeset for help on using the changeset viewer.