Changes in src/libcfa/concurrency/alarm.c [47ecf2b:6b0b624]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/alarm.c
r47ecf2b r6b0b624 1 // -*- Mode: CFA -*-2 1 // 3 2 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo … … 10 9 // Author : Thierry Delisle 11 10 // Created On : Fri Jun 2 11:31:25 2017 12 // Last Modified By : Thierry Delisle13 // Last Modified On : --14 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jul 21 22:35:18 2017 13 // Update Count : 1 15 14 // 16 15 … … 31 30 32 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 //============================================================================================= 33 70 // Clock logic 34 71 //============================================================================================= … … 37 74 timespec curr; 38 75 clock_gettime( CLOCK_REALTIME, &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; 76 return (__cfa_time_t){ &curr }; 42 77 } 43 78 44 79 void __kernel_set_timer( __cfa_time_t 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; 80 itimerval val = { &alarm }; 51 81 setitimer( ITIMER_REAL, &val, NULL ); 52 82 } … … 56 86 //============================================================================================= 57 87 58 void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = 0, __cfa_time_t period = 0) {88 void ?{}( alarm_node_t * this, thread_desc * thrd, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) { 59 89 this->thrd = thrd; 60 90 this->alarm = alarm; … … 65 95 } 66 96 67 void ?{}( alarm_node_t * this, processor * proc, __cfa_time_t alarm = 0, __cfa_time_t period = 0) {97 void ?{}( alarm_node_t * this, processor * proc, __cfa_time_t alarm = zero_time, __cfa_time_t period = zero_time ) { 68 98 this->proc = proc; 69 99 this->alarm = alarm; … … 153 183 154 184 void register_self( alarm_node_t * this ) { 185 alarm_list_t * alarms = &event_kernel->alarms; 186 155 187 disable_interrupts(); 156 verify( !systemProcessor->pending_alarm ); 157 lock( &systemProcessor->alarm_lock DEBUG_CTX2 ); 188 lock( &event_kernel->lock DEBUG_CTX2 ); 158 189 { 159 verify( validate( &systemProcessor->alarms ) );160 bool first = ! systemProcessor->alarms.head;161 162 insert( &systemProcessor->alarms, this );163 if( systemProcessor->pending_alarm) {164 tick_preemption();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() ); 165 196 } 166 if( first ) { 167 __kernel_set_timer( systemProcessor->alarms.head->alarm - __kernel_get_time() ); 168 } 169 } 170 unlock( &systemProcessor->alarm_lock ); 197 } 198 unlock( &event_kernel->lock ); 171 199 this->set = true; 172 200 enable_interrupts( DEBUG_CTX ); … … 174 202 175 203 void unregister_self( alarm_node_t * this ) { 176 // LIB_DEBUG_PRINT_BUFFER_DECL( STDERR_FILENO, "Kernel : unregister %p start\n", this );177 204 disable_interrupts(); 178 lock( & systemProcessor->alarm_lock DEBUG_CTX2 );205 lock( &event_kernel->lock DEBUG_CTX2 ); 179 206 { 180 verify( validate( & systemProcessor->alarms ) );181 remove( & systemProcessor->alarms, this );182 } 183 unlock( & systemProcessor->alarm_lock );207 verify( validate( &event_kernel->alarms ) ); 208 remove( &event_kernel->alarms, this ); 209 } 210 unlock( &event_kernel->lock ); 184 211 enable_interrupts( DEBUG_CTX ); 185 212 this->set = false; 186 // LIB_DEBUG_PRINT_BUFFER_LOCAL( STDERR_FILENO, "Kernel : unregister %p end\n", this ); 187 } 213 } 214 215 // Local Variables: // 216 // mode: c // 217 // tab-width: 4 // 218 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.