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