| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // alarm.c --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Thierry Delisle
 | 
|---|
| 10 | // 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
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | extern "C" {
 | 
|---|
| 17 | #include <errno.h>
 | 
|---|
| 18 | #include <stdio.h>
 | 
|---|
| 19 | #include <string.h>
 | 
|---|
| 20 | #include <time.h>
 | 
|---|
| 21 | #include <unistd.h>
 | 
|---|
| 22 | #include <sys/time.h>
 | 
|---|
| 23 | }
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "libhdr.h"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "alarm.h"
 | 
|---|
| 28 | #include "kernel_private.h"
 | 
|---|
| 29 | #include "preemption.h"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | //=============================================================================================
 | 
|---|
| 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 | //=============================================================================================
 | 
|---|
| 70 | // Clock logic
 | 
|---|
| 71 | //=============================================================================================
 | 
|---|
| 72 | 
 | 
|---|
| 73 | __cfa_time_t __kernel_get_time() {
 | 
|---|
| 74 |         timespec curr;
 | 
|---|
| 75 |         clock_gettime( CLOCK_REALTIME, &curr );
 | 
|---|
| 76 |         return (__cfa_time_t){ &curr };
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | void __kernel_set_timer( __cfa_time_t alarm ) {
 | 
|---|
| 80 |         itimerval val = { &alarm };
 | 
|---|
| 81 |         setitimer( ITIMER_REAL, &val, NULL );
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | //=============================================================================================
 | 
|---|
| 85 | // Alarm logic
 | 
|---|
| 86 | //=============================================================================================
 | 
|---|
| 87 | 
 | 
|---|
| 88 | void ?{}( alarm_node_t & this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
 | 
|---|
| 89 |         this.thrd = thrd;
 | 
|---|
| 90 |         this.alarm = alarm;
 | 
|---|
| 91 |         this.period = period;
 | 
|---|
| 92 |         this.next = 0;
 | 
|---|
| 93 |         this.set = false;
 | 
|---|
| 94 |         this.kernel_alarm = false;
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | void ?{}( alarm_node_t & this, processor   * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) {
 | 
|---|
| 98 |         this.proc = proc;
 | 
|---|
| 99 |         this.alarm = alarm;
 | 
|---|
| 100 |         this.period = period;
 | 
|---|
| 101 |         this.next = 0;
 | 
|---|
| 102 |         this.set = false;
 | 
|---|
| 103 |         this.kernel_alarm = true;
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | void ^?{}( alarm_node_t & this ) {
 | 
|---|
| 107 |         if( this.set ) {
 | 
|---|
| 108 |                 unregister_self( &this );
 | 
|---|
| 109 |         }
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
 | 
|---|
| 113 |         alarm_node_t ** it = &this->head;
 | 
|---|
| 114 |         while( (*it) ) {
 | 
|---|
| 115 |                 it = &(*it)->next;
 | 
|---|
| 116 |         }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |         return it == this->tail;
 | 
|---|
| 119 | })
 | 
|---|
| 120 | 
 | 
|---|
| 121 | static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) {
 | 
|---|
| 122 |         verify( !n->next );
 | 
|---|
| 123 |         if( p == this->tail ) {
 | 
|---|
| 124 |                 this->tail = &n->next;
 | 
|---|
| 125 |         }
 | 
|---|
| 126 |         else {
 | 
|---|
| 127 |                 n->next = *p;
 | 
|---|
| 128 |         }
 | 
|---|
| 129 |         *p = n;
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         verify( validate( this ) );
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | void insert( alarm_list_t * this, alarm_node_t * n ) {
 | 
|---|
| 135 |         alarm_node_t ** it = &this->head;
 | 
|---|
| 136 |         while( (*it) && (n->alarm > (*it)->alarm) ) {
 | 
|---|
| 137 |                 it = &(*it)->next;
 | 
|---|
| 138 |         }
 | 
|---|
| 139 | 
 | 
|---|
| 140 |         insert_at( this, n, it );
 | 
|---|
| 141 | 
 | 
|---|
| 142 |         verify( validate( this ) );
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | alarm_node_t * pop( alarm_list_t * this ) {
 | 
|---|
| 146 |         alarm_node_t * head = this->head;
 | 
|---|
| 147 |         if( head ) {
 | 
|---|
| 148 |                 this->head = head->next;
 | 
|---|
| 149 |                 if( !head->next ) {
 | 
|---|
| 150 |                         this->tail = &this->head;
 | 
|---|
| 151 |                 }
 | 
|---|
| 152 |                 head->next = NULL;
 | 
|---|
| 153 |         }
 | 
|---|
| 154 |         verify( validate( this ) );
 | 
|---|
| 155 |         return head;
 | 
|---|
| 156 | }
 | 
|---|
| 157 | 
 | 
|---|
| 158 | static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) {
 | 
|---|
| 159 |         verify( it );
 | 
|---|
| 160 |         verify( (*it) == n );
 | 
|---|
| 161 | 
 | 
|---|
| 162 |         (*it) = n->next;
 | 
|---|
| 163 |         if( !n-> next ) {
 | 
|---|
| 164 |                 this->tail = it;
 | 
|---|
| 165 |         }
 | 
|---|
| 166 |         n->next = NULL;
 | 
|---|
| 167 | 
 | 
|---|
| 168 |         verify( validate( this ) );
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | static inline void remove( alarm_list_t * this, alarm_node_t * n ) {
 | 
|---|
| 172 |         alarm_node_t ** it = &this->head;
 | 
|---|
| 173 |         while( (*it) && (*it) != n ) {
 | 
|---|
| 174 |                 it = &(*it)->next;
 | 
|---|
| 175 |         }
 | 
|---|
| 176 | 
 | 
|---|
| 177 |         verify( validate( this ) );
 | 
|---|
| 178 | 
 | 
|---|
| 179 |         if( *it ) { remove_at( this, n, it ); }
 | 
|---|
| 180 | 
 | 
|---|
| 181 |         verify( validate( this ) );
 | 
|---|
| 182 | }
 | 
|---|
| 183 | 
 | 
|---|
| 184 | void register_self( alarm_node_t * this ) {
 | 
|---|
| 185 |         alarm_list_t * alarms = &event_kernel->alarms;
 | 
|---|
| 186 | 
 | 
|---|
| 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 );
 | 
|---|
| 194 |                 if( first ) {
 | 
|---|
| 195 |                         __kernel_set_timer( alarms->head->alarm - __kernel_get_time() );
 | 
|---|
| 196 |                 }
 | 
|---|
| 197 |         }
 | 
|---|
| 198 |         unlock( &event_kernel->lock );
 | 
|---|
| 199 |         this->set = true;
 | 
|---|
| 200 |         enable_interrupts( DEBUG_CTX );
 | 
|---|
| 201 | }
 | 
|---|
| 202 | 
 | 
|---|
| 203 | void unregister_self( alarm_node_t * this ) {
 | 
|---|
| 204 |         disable_interrupts();
 | 
|---|
| 205 |         lock( &event_kernel->lock DEBUG_CTX2 );
 | 
|---|
| 206 |         {
 | 
|---|
| 207 |                 verify( validate( &event_kernel->alarms ) );
 | 
|---|
| 208 |                 remove( &event_kernel->alarms, this );
 | 
|---|
| 209 |         }
 | 
|---|
| 210 |         unlock( &event_kernel->lock );
 | 
|---|
| 211 |         enable_interrupts( DEBUG_CTX );
 | 
|---|
| 212 |         this->set = false;
 | 
|---|
| 213 | }
 | 
|---|
| 214 | 
 | 
|---|
| 215 | // Local Variables: //
 | 
|---|
| 216 | // mode: c //
 | 
|---|
| 217 | // tab-width: 4 //
 | 
|---|
| 218 | // End: //
 | 
|---|