Ignore:
File:
1 edited

Legend:

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

    r4aa2fb2 r1c273d0  
    1616
    1717extern "C" {
     18#include <errno.h>
     19#include <stdio.h>
     20#include <string.h>
    1821#include <time.h>
     22#include <unistd.h>
    1923#include <sys/time.h>
    2024}
     
    2226#include "alarm.h"
    2327#include "kernel_private.h"
     28#include "libhdr.h"
    2429#include "preemption.h"
    2530
     
    3136        timespec curr;
    3237        clock_gettime( CLOCK_REALTIME, &curr );
    33         return ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
     38        __cfa_time_t curr_time = ((__cfa_time_t)curr.tv_sec * TIMEGRAN) + curr.tv_nsec;
     39        LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : current time is %lu\n", curr_time );
     40        return curr_time;
    3441}
    3542
    3643void __kernel_set_timer( __cfa_time_t alarm ) {
     44        LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : set timer to %lu\n", (__cfa_time_t)alarm );
    3745        itimerval val;
    3846        val.it_value.tv_sec = alarm / TIMEGRAN;                 // seconds
     
    7179}
    7280
     81LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
     82        alarm_node_t ** it = &this->head;
     83        while( (*it) ) {
     84                it = &(*it)->next;
     85        }
     86
     87        return it == this->tail;
     88})
     89
    7390static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
    74         assert( !n->next );
     91        verify( !n->next );
    7592        if( p == this->tail ) {
    7693                this->tail = &n->next;
     
    8097        }
    8198        *p = n;
     99
     100        verify( validate( this ) );
    82101}
    83102
     
    89108
    90109        insert_at( this, n, it );
     110
     111        verify( validate( this ) );
    91112}
    92113
     
    100121                head->next = NULL;
    101122        }
     123        verify( validate( this ) );
    102124        return head;
    103125}
     
    105127static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
    106128        verify( it );
    107         verify( (*it)->next == n );
     129        verify( (*it) == n );
    108130
    109         (*it)->next = n->next;
     131        (*it) = n->next;
    110132        if( !n-> next ) {
    111133                this->tail = it;
    112134        }
    113135        n->next = NULL;
     136
     137        verify( validate( this ) );
    114138}
    115139
    116140static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
    117141        alarm_node_t ** it = &this->head;
    118         while( (*it) && (*it)->next != n ) {
     142        while( (*it) && (*it) != n ) {
    119143                it = &(*it)->next;
    120144        }
    121145
     146        verify( validate( this ) );
     147
    122148        if( *it ) { remove_at( this, n, it ); }
     149
     150        verify( validate( this ) );
    123151}
    124152
    125153void register_self( alarm_node_t * this ) {
    126154        disable_interrupts();
    127         assert( !systemProcessor->pending_alarm );
    128         lock( &systemProcessor->alarm_lock );
     155        verify( !systemProcessor->pending_alarm );
     156        lock( &systemProcessor->alarm_lock, __PRETTY_FUNCTION__ );
    129157        {
     158                verify( validate( &systemProcessor->alarms ) );
     159                bool first = !systemProcessor->alarms.head;
     160
    130161                insert( &systemProcessor->alarms, this );
    131162                if( systemProcessor->pending_alarm ) {
    132163                        tick_preemption();
    133164                }
     165                if( first ) {
     166                        __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() );
     167                }
    134168        }
    135169        unlock( &systemProcessor->alarm_lock );
    136170        this->set = true;
    137         enable_interrupts();
     171        enable_interrupts( __PRETTY_FUNCTION__ );
    138172}
    139173
    140174void unregister_self( alarm_node_t * this ) {
     175        // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : unregister %p start\n", this );
    141176        disable_interrupts();
    142         lock( &systemProcessor->alarm_lock );
    143         remove( &systemProcessor->alarms, this );
     177        lock( &systemProcessor->alarm_lock, __PRETTY_FUNCTION__ );
     178        {
     179                verify( validate( &systemProcessor->alarms ) );
     180                remove( &systemProcessor->alarms, this );
     181        }
    144182        unlock( &systemProcessor->alarm_lock );
    145183        disable_interrupts();
    146184        this->set = false;
     185        // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Kernel : unregister %p end\n", this );
    147186}
Note: See TracChangeset for help on using the changeset viewer.