Ignore:
File:
1 edited

Legend:

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

    r6b0b624 r47ecf2b  
     1//                              -*- Mode: CFA -*-
    12//
    23// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     
    910// Author           : Thierry Delisle
    1011// Created On       : Fri Jun 2 11:31:25 2017
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 21 22:35:18 2017
    13 // Update Count     : 1
     12// Last Modified By : Thierry Delisle
     13// Last Modified On : --
     14// Update Count     : 0
    1415//
    1516
     
    3031
    3132//=============================================================================================
    32 // time type
    33 //=============================================================================================
    34 
    35 #define one_second         1_000_000_000ul
    36 #define one_milisecond         1_000_000ul
    37 #define one_microsecond            1_000ul
    38 #define one_nanosecond                 1ul
    39 
    40 __cfa_time_t zero_time = { 0 };
    41 
    42 void ?{}( __cfa_time_t * this ) { this->val = 0; }
    43 void ?{}( __cfa_time_t * this, zero_t zero ) { this->val = 0; }
    44 
    45 void ?{}( itimerval * this, __cfa_time_t * alarm ) {
    46         this->it_value.tv_sec = alarm->val / one_second;                        // seconds
    47         this->it_value.tv_usec = max( (alarm->val % one_second) / one_microsecond, 1000 ); // microseconds
    48         this->it_interval.tv_sec = 0;
    49         this->it_interval.tv_usec = 0;
    50 }
    51 
    52 
    53 void ?{}( __cfa_time_t * this, timespec * curr ) {
    54         uint64_t secs  = curr->tv_sec;
    55         uint64_t nsecs = curr->tv_nsec;
    56         this->val = (secs * one_second) + nsecs;
    57 }
    58 
    59 __cfa_time_t ?=?( __cfa_time_t * this, zero_t rhs ) {
    60         this->val = 0;
    61         return *this;
    62 }
    63 
    64 __cfa_time_t from_s ( uint64_t val ) { __cfa_time_t ret; ret.val = val * 1_000_000_000ul; return ret; }
    65 __cfa_time_t from_ms( uint64_t val ) { __cfa_time_t ret; ret.val = val *     1_000_000ul; return ret; }
    66 __cfa_time_t from_us( uint64_t val ) { __cfa_time_t ret; ret.val = val *         1_000ul; return ret; }
    67 __cfa_time_t from_ns( uint64_t val ) { __cfa_time_t ret; ret.val = val *             1ul; return ret; }
    68 
    69 //=============================================================================================
    7033// Clock logic
    7134//=============================================================================================
     
    7437        timespec curr;
    7538        clock_gettime( CLOCK_REALTIME, &curr );
    76         return (__cfa_time_t){ &curr };
     39        __cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
     40        // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : current time is %lu\n", curr_time );
     41        return curr_time;
    7742}
    7843
    7944void __kernel_set_timer( __cfa_time_t alarm ) {
    80         itimerval val = { &alarm };
     45        LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );
     46        itimerval val;
     47        val.it_value.tv_sec = alarm / TIMEGRAN;                 // seconds
     48        val.it_value.tv_usec = (alarm % TIMEGRAN) / ( TIMEGRAN / 1_000_000L ); // microseconds
     49        val.it_interval.tv_sec = 0;
     50        val.it_interval.tv_usec = 0;
    8151        setitimer( ITIMER_REAL, &val, NULL );
    8252}
     
    8656//=============================================================================================
    8757
    88 void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
     58void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
    8959        this->thrd = thrd;
    9060        this->alarm = alarm;
     
    9565}
    9666
    97 void ?{}( alarm_node_t * this, processor   * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
     67void ?{}( alarm_node_t * this, processor   * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
    9868        this->proc = proc;
    9969        this->alarm = alarm;
     
    183153
    184154void register_self( alarm_node_t * this ) {
    185         alarm_list_t * alarms = &event_kernel->alarms;
     155        disable_interrupts();
     156        verify( !systemProcessor->pending_alarm );
     157        lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
     158        {
     159                verify( validate( &systemProcessor->alarms ) );
     160                bool first = !systemProcessor->alarms.head;
    186161
    187         disable_interrupts();
    188         lock( &event_kernel->lock DEBUG_CTX2 );
    189         {
    190                 verify( validate( alarms ) );
    191                 bool first = !alarms->head;
    192 
    193                 insert( alarms, this );
     162                insert( &systemProcessor->alarms, this );
     163                if( systemProcessor->pending_alarm ) {
     164                        tick_preemption();
     165                }
    194166                if( first ) {
    195                         __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
     167                        __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
    196168                }
    197169        }
    198         unlock( &event_kernel->lock );
     170        unlock( &systemProcessor->alarm_lock );
    199171        this->set = true;
    200172        enable_interrupts( DEBUG_CTX );
     
    202174
    203175void unregister_self( alarm_node_t * this ) {
     176        // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : unregister %p start\n", this );
    204177        disable_interrupts();
    205         lock( &event_kernel->lock DEBUG_CTX2 );
     178        lock( &systemProcessor->alarm_lock DEBUG_CTX2 );
    206179        {
    207                 verify( validate( &event_kernel->alarms ) );
    208                 remove( &event_kernel->alarms, this );
     180                verify( validate( &systemProcessor->alarms ) );
     181                remove( &systemProcessor->alarms, this );
    209182        }
    210         unlock( &event_kernel->lock );
     183        unlock( &systemProcessor->alarm_lock );
    211184        enable_interrupts( DEBUG_CTX );
    212185        this->set = false;
     186        // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Kernel : unregister %p end\n", this );
    213187}
    214 
    215 // Local Variables: //
    216 // mode: c //
    217 // tab-width: 4 //
    218 // End: //
Note: See TracChangeset for help on using the changeset viewer.