Ignore:
Timestamp:
Apr 17, 2018, 12:01:09 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
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, with_gc
Children:
3265399
Parents:
b2fe1c9 (diff), 81bb114 (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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

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

    rb2fe1c9 r32cab5b  
    1010// Created On       : Mon Jun 5 14:20:42 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Feb  9 16:38:13 2018
    13 // Update Count     : 14
     12// Last Modified On : Mon Apr  9 13:52:39 2018
     13// Update Count     : 36
    1414//
    1515
     
    2323}
    2424
    25 #include "bits/cfatime.h"
    2625#include "bits/signal.h"
    2726
    2827#if !defined(__CFA_DEFAULT_PREEMPTION__)
    29 #define __CFA_DEFAULT_PREEMPTION__ 10`cfa_ms
     28#define __CFA_DEFAULT_PREEMPTION__ 10`ms
    3029#endif
    3130
    32 __cfa_time_t default_preemption() __attribute__((weak)) {
     31Duration default_preemption() __attribute__((weak)) {
    3332        return __CFA_DEFAULT_PREEMPTION__;
    3433}
     
    7877
    7978// Get next expired node
    80 static inline alarm_node_t * get_expired( alarm_list_t * alarms, __cfa_time_t currtime ) {
     79static inline alarm_node_t * get_expired( alarm_list_t * alarms, Time currtime ) {
    8180        if( !alarms->head ) return NULL;                          // If no alarms return null
    8281        if( alarms->head->alarm >= currtime ) return NULL;        // If alarms head not expired return null
     
    8887        alarm_node_t * node = NULL;                     // Used in the while loop but cannot be declared in the while condition
    8988        alarm_list_t * alarms = &event_kernel->alarms;  // Local copy for ease of reading
    90         __cfa_time_t currtime = __kernel_get_time();    // Check current time once so we everything "happens at once"
     89        Time currtime = __kernel_get_time();                    // Check current time once so we everything "happens at once"
    9190
    9291        //Loop throught every thing expired
     
    102101
    103102                // Check if this is a periodic alarm
    104                 __cfa_time_t period = node->period;
     103                Duration period = node->period;
    105104                if( period > 0 ) {
    106105                        node->alarm = currtime + period;    // Alarm is periodic, add currtime to it (used cached current time)
     
    117116
    118117// Update the preemption of a processor and notify interested parties
    119 void update_preemption( processor * this, __cfa_time_t duration ) {
     118void update_preemption( processor * this, Duration duration ) {
    120119        alarm_node_t * alarm = this->preemption_alarm;
    121120
    122121        // Alarms need to be enabled
    123         if ( duration > 0 && !alarm->set ) {
     122        if ( duration > 0 && ! alarm->set ) {
    124123                alarm->alarm = __kernel_get_time() + duration;
    125124                alarm->period = duration;
    126125                register_self( alarm );
    127126        }
    128         // Zero duraction but alarm is set
     127        // Zero duration but alarm is set
    129128        else if ( duration == 0 && alarm->set ) {
    130129                unregister_self( alarm );
     
    150149        // Disable interrupts by incrementing the counter
    151150        void disable_interrupts() {
    152                 preemption_state.enabled = false;
    153                 __attribute__((unused)) unsigned short new_val = preemption_state.disable_count + 1;
    154                 preemption_state.disable_count = new_val;
     151                TL_GET( preemption_state ).enabled = false;
     152                __attribute__((unused)) unsigned short new_val = TL_GET( preemption_state ).disable_count + 1;
     153                TL_GET( preemption_state ).disable_count = new_val;
    155154                verify( new_val < 65_000u );              // If this triggers someone is disabling interrupts without enabling them
    156155        }
     
    159158        // If counter reaches 0, execute any pending CtxSwitch
    160159        void enable_interrupts( __cfaabi_dbg_ctx_param ) {
    161                 processor   * proc = this_processor;      // Cache the processor now since interrupts can start happening after the atomic add
    162                 thread_desc * thrd = this_thread;         // Cache the thread now since interrupts can start happening after the atomic add
    163 
    164                 unsigned short prev = preemption_state.disable_count;
    165                 preemption_state.disable_count -= 1;
     160                processor   * proc = TL_GET( this_processor ); // Cache the processor now since interrupts can start happening after the atomic add
     161                thread_desc * thrd = TL_GET( this_thread );       // Cache the thread now since interrupts can start happening after the atomic add
     162
     163                unsigned short prev = TL_GET( preemption_state ).disable_count;
     164                TL_GET( preemption_state ).disable_count -= 1;
    166165                verify( prev != 0u );                     // If this triggers someone is enabled already enabled interruptsverify( prev != 0u );
    167166
    168167                // Check if we need to prempt the thread because an interrupt was missed
    169168                if( prev == 1 ) {
    170                         preemption_state.enabled = true;
     169                        TL_GET( preemption_state ).enabled = true;
    171170                        if( proc->pending_preemption ) {
    172171                                proc->pending_preemption = false;
     
    182181        // Don't execute any pending CtxSwitch even if counter reaches 0
    183182        void enable_interrupts_noPoll() {
    184                 unsigned short prev = preemption_state.disable_count;
    185                 preemption_state.disable_count -= 1;
     183                unsigned short prev = TL_GET( preemption_state ).disable_count;
     184                TL_GET( preemption_state ).disable_count -= 1;
    186185                verifyf( prev != 0u, "Incremented from %u\n", prev );                     // If this triggers someone is enabled already enabled interrupts
    187186                if( prev == 1 ) {
    188                         preemption_state.enabled = true;
     187                        TL_GET( preemption_state ).enabled = true;
    189188                }
    190189        }
     
    236235// If false : preemption is unsafe and marked as pending
    237236static inline bool preemption_ready() {
    238         bool ready = preemption_state.enabled && !preemption_state.in_progress; // Check if preemption is safe
    239         this_processor->pending_preemption = !ready;                        // Adjust the pending flag accordingly
     237        bool ready = TL_GET( preemption_state ).enabled && !TL_GET( preemption_state ).in_progress; // Check if preemption is safe
     238        TL_GET( this_processor )->pending_preemption = !ready;                  // Adjust the pending flag accordingly
    240239        return ready;
    241240}
     
    251250
    252251        // Start with preemption disabled until ready
    253         preemption_state.enabled = false;
    254         preemption_state.disable_count = 1;
     252        TL_GET( preemption_state ).enabled = false;
     253        TL_GET( preemption_state ).disable_count = 1;
    255254
    256255        // Initialize the event kernel
     
    291290// Used by thread to control when they want to receive preemption signals
    292291void ?{}( preemption_scope & this, processor * proc ) {
    293         (this.alarm){ proc, 0`cfa_s, 0`cfa_s };
     292        (this.alarm){ proc, (Time){ 0 }, 0`s };
    294293        this.proc = proc;
    295294        this.proc->preemption_alarm = &this.alarm;
     
    301300        disable_interrupts();
    302301
    303         update_preemption( this.proc, 0`cfa_s );
     302        update_preemption( this.proc, 0`s );
    304303}
    305304
     
    317316        // before the kernel thread has even started running. When that happens an iterrupt
    318317        // we a null 'this_processor' will be caught, just ignore it.
    319         if(!this_processor) return;
     318        if(!TL_GET( this_processor )) return;
    320319
    321320        choose(sfp->si_value.sival_int) {
    322321                case PREEMPT_NORMAL   : ;// Normal case, nothing to do here
    323                 case PREEMPT_TERMINATE: verify(this_processor->do_terminate);
     322                case PREEMPT_TERMINATE: verify(TL_GET( this_processor )->do_terminate);
    324323                default:
    325324                        abort( "internal error, signal value is %d", sfp->si_value.sival_int );
     
    331330        __cfaabi_dbg_print_buffer_decl( " KERNEL: preempting core %p (%p).\n", this_processor, this_thread);
    332331
    333         preemption_state.in_progress = true;                      // Sync flag : prevent recursive calls to the signal handler
     332        TL_GET( preemption_state ).in_progress = true;  // Sync flag : prevent recursive calls to the signal handler
    334333        signal_unblock( SIGUSR1 );                          // We are about to CtxSwitch out of the signal handler, let other handlers in
    335         preemption_state.in_progress = false;                    // Clear the in progress flag
     334        TL_GET( preemption_state ).in_progress = false; // Clear the in progress flag
    336335
    337336        // Preemption can occur here
    338337
    339         BlockInternal( (thread_desc*)this_thread );        // Do the actual CtxSwitch
     338        BlockInternal( (thread_desc*)TL_GET( this_thread ) ); // Do the actual CtxSwitch
    340339}
    341340
Note: See TracChangeset for help on using the changeset viewer.