Ignore:
File:
1 edited

Legend:

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

    r969b3fe r4aa2fb2  
    1616
    1717extern "C" {
    18 #include <errno.h>
    19 #include <stdio.h>
    20 #include <string.h>
    2118#include <time.h>
    22 #include <unistd.h>
    2319#include <sys/time.h>
    2420}
    25 
    26 #include "libhdr.h"
    2721
    2822#include "alarm.h"
    2923#include "kernel_private.h"
    3024#include "preemption.h"
    31 
    32 //=============================================================================================
    33 // time type
    34 //=============================================================================================
    35 
    36 #define one_second         1_000_000_000ul
    37 #define one_milisecond         1_000_000ul
    38 #define one_microsecond            1_000ul
    39 #define one_nanosecond                 1ul
    40 
    41 __cfa_time_t zero_time = { 0 };
    42 
    43 void ?{}( __cfa_time_t * this ) { this->val = 0; }
    44 void ?{}( __cfa_time_t * this, zero_t zero ) { this->val = 0; }
    45 
    46 void ?{}( itimerval * this, __cfa_time_t * alarm ) {
    47         this->it_value.tv_sec = alarm->val / one_second;                        // seconds
    48         this->it_value.tv_usec = max( (alarm->val % one_second) / one_microsecond, 1000 ); // microseconds
    49         this->it_interval.tv_sec = 0;
    50         this->it_interval.tv_usec = 0;
    51 }
    52 
    53 
    54 void ?{}( __cfa_time_t * this, timespec * curr ) {
    55         uint64_t secs  = curr->tv_sec;
    56         uint64_t nsecs = curr->tv_nsec;
    57         this->val = (secs * one_second) + nsecs;
    58 }
    59 
    60 __cfa_time_t ?=?( __cfa_time_t * this, zero_t rhs ) {
    61         this->val = 0;
    62         return *this;
    63 }
    64 
    65 __cfa_time_t from_s ( uint64_t val ) { __cfa_time_t ret; ret.val = val * 1_000_000_000ul; return ret; }
    66 __cfa_time_t from_ms( uint64_t val ) { __cfa_time_t ret; ret.val = val *     1_000_000ul; return ret; }
    67 __cfa_time_t from_us( uint64_t val ) { __cfa_time_t ret; ret.val = val *         1_000ul; return ret; }
    68 __cfa_time_t from_ns( uint64_t val ) { __cfa_time_t ret; ret.val = val *             1ul; return ret; }
    6925
    7026//=============================================================================================
     
    7531        timespec curr;
    7632        clock_gettime( CLOCK_REALTIME, &curr );
    77         return (__cfa_time_t){ &curr };
     33        return ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
    7834}
    7935
    8036void __kernel_set_timer( __cfa_time_t alarm ) {
    81         itimerval val = { &alarm };
     37        itimerval val;
     38        val.it_value.tv_sec = alarm / TIMEGRAN;                 // seconds
     39        val.it_value.tv_usec = (alarm % TIMEGRAN) / ( TIMEGRAN / 1_000_000L ); // microseconds
     40        val.it_interval.tv_sec = 0;
     41        val.it_interval.tv_usec = 0;
    8242        setitimer( ITIMER_REAL, &val, NULL );
    8343}
     
    8747//=============================================================================================
    8848
    89 void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
     49void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
    9050        this->thrd = thrd;
    9151        this->alarm = alarm;
     
    9656}
    9757
    98 void ?{}( alarm_node_t * this, processor   * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
     58void ?{}( alarm_node_t * this, processor   * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0 ) {
    9959        this->proc = proc;
    10060        this->alarm = alarm;
     
    11171}
    11272
    113 LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
    114         alarm_node_t ** it = &this->head;
    115         while( (*it) ) {
    116                 it = &(*it)->next;
    117         }
    118 
    119         return it == this->tail;
    120 })
    121 
    12273static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
    123         verify( !n->next );
     74        assert( !n->next );
    12475        if( p == this->tail ) {
    12576                this->tail = &n->next;
     
    12980        }
    13081        *p = n;
    131 
    132         verify( validate( this ) );
    13382}
    13483
     
    14089
    14190        insert_at( this, n, it );
    142 
    143         verify( validate( this ) );
    14491}
    14592
     
    153100                head->next = NULL;
    154101        }
    155         verify( validate( this ) );
    156102        return head;
    157103}
     
    159105static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
    160106        verify( it );
    161         verify( (*it) == n );
     107        verify( (*it)->next == n );
    162108
    163         (*it) = n->next;
     109        (*it)->next = n->next;
    164110        if( !n-> next ) {
    165111                this->tail = it;
    166112        }
    167113        n->next = NULL;
    168 
    169         verify( validate( this ) );
    170114}
    171115
    172116static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
    173117        alarm_node_t ** it = &this->head;
    174         while( (*it) && (*it) != n ) {
     118        while( (*it) && (*it)->next != n ) {
    175119                it = &(*it)->next;
    176120        }
    177121
    178         verify( validate( this ) );
    179 
    180122        if( *it ) { remove_at( this, n, it ); }
    181 
    182         verify( validate( this ) );
    183123}
    184124
    185125void register_self( alarm_node_t * this ) {
    186         alarm_list_t * alarms = &event_kernel->alarms;
    187 
    188126        disable_interrupts();
    189         lock( &event_kernel->lock DEBUG_CTX2 );
     127        assert( !systemProcessor->pending_alarm );
     128        lock( &systemProcessor->alarm_lock );
    190129        {
    191                 verify( validate( alarms ) );
    192                 bool first = !alarms->head;
    193 
    194                 insert( alarms, this );
    195                 if( first ) {
    196                         __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
     130                insert( &systemProcessor->alarms, this );
     131                if( systemProcessor->pending_alarm ) {
     132                        tick_preemption();
    197133                }
    198134        }
    199         unlock( &event_kernel->lock );
     135        unlock( &systemProcessor->alarm_lock );
    200136        this->set = true;
    201         enable_interrupts( DEBUG_CTX );
     137        enable_interrupts();
    202138}
    203139
    204140void unregister_self( alarm_node_t * this ) {
    205141        disable_interrupts();
    206         lock( &event_kernel->lock DEBUG_CTX2 );
    207         {
    208                 verify( validate( &event_kernel->alarms ) );
    209                 remove( &event_kernel->alarms, this );
    210         }
    211         unlock( &event_kernel->lock );
    212         enable_interrupts( DEBUG_CTX );
     142        lock( &systemProcessor->alarm_lock );
     143        remove( &systemProcessor->alarms, this );
     144        unlock( &systemProcessor->alarm_lock );
     145        disable_interrupts();
    213146        this->set = false;
    214147}
Note: See TracChangeset for help on using the changeset viewer.