Ignore:
Timestamp:
May 14, 2021, 5:23:49 PM (3 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
353aaba
Parents:
e2f601f
Message:

Fix sequential handling of timers

File:
1 edited

Legend:

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

    re2f601f rc457dc41  
    3838
    3939void __kernel_set_timer( Duration alarm ) {
    40         verifyf(alarm >= 1`us || alarm == 0, "Setting timer to < 1us (%jins)", alarm`ns);
    41         setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p );
     40        alarm = max(alarm, 1`us);
     41        itimerval otv @= { 0 };
     42        getitimer( ITIMER_REAL, &otv );
     43        Duration od = { otv.it_value };
     44        if(od == 0 || od > alarm) {
     45                setitimer( ITIMER_REAL, &(itimerval){ alarm }, 0p );
     46        }
    4247}
    4348
     
    4651//=============================================================================================
    4752
    48 void ?{}( alarm_node_t & this, $thread * thrd, Time alarm, Duration period) with( this ) {
     53void ?{}( alarm_node_t & this, $thread * thrd, Duration alarm, Duration period) with( this ) {
     54        this.initial = alarm;
     55        this.period  = period;
    4956        this.thrd = thrd;
    50         this.alarm = alarm;
    51         this.period = period;
    5257        set = false;
    5358        type = User;
    5459}
    5560
    56 void ?{}( alarm_node_t & this, processor * proc, Time alarm, Duration period ) with( this ) {
     61void ?{}( alarm_node_t & this, processor * proc, Duration alarm, Duration period ) with( this ) {
     62        this.initial = alarm;
     63        this.period  = period;
    5764        this.proc = proc;
    58         this.alarm = alarm;
    59         this.period = period;
    6065        set = false;
    6166        type = Kernel;
    6267}
    63 void ?{}( alarm_node_t & this, Alarm_Callback callback, Time alarm, Duration period ) with( this ) {
    64         this.alarm = alarm;
    65         this.period = period;
     68void ?{}( alarm_node_t & this, Alarm_Callback callback, Duration alarm, Duration period ) with( this ) {
     69        this.initial = alarm;
     70        this.period  = period;
    6671        this.callback = callback;
    6772        set = false;
     
    7782void insert( alarm_list_t * this, alarm_node_t * n ) {
    7883        alarm_node_t * it = & (*this)`first;
    79         while( it && (n->alarm > it->alarm) ) {
     84        while( it && (n->timeval > it->timeval) ) {
    8085                it = & (*it)`next;
    8186        }
     
    105110        lock( event_kernel->lock __cfaabi_dbg_ctx2 );
    106111        {
    107                 verify( validate( alarms ) );
    108                 bool first = ! & alarms`first;
     112                Time curr = __kernel_get_time();
     113                this->timeval = curr + this->initial;
    109114
    110                 __cfadbg_print_safe( preemption, " KERNEL: alarm inserting %p (%lu).\n", this, this->alarm.tn );
     115                /* paranoid */ verify( validate( alarms ) );
     116
     117                __cfadbg_print_safe( preemption, " KERNEL: alarm inserting %p (%lu -> %lu).\n", this, curr.tn, this->timeval.tn );
    111118                insert( &alarms, this );
    112                 if( first ) {
    113                         __kernel_set_timer( alarms`first.alarm - __kernel_get_time() );
    114                 }
     119                __kernel_set_timer( this->initial );
    115120        }
    116121        unlock( event_kernel->lock );
     
    136141
    137142void sleep( Duration duration ) {
    138         alarm_node_t node = { active_thread(), __kernel_get_time() + duration, 0`s };
     143        alarm_node_t node = { active_thread(), duration, 0`s };
    139144
    140145        register_self( &node );
Note: See TracChangeset for help on using the changeset viewer.