[6cff9f3] | 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 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[8ad6533] | 12 | // Last Modified On : Mon Apr 9 13:36:18 2018 |
---|
| 13 | // Update Count : 61 |
---|
[6cff9f3] | 14 | // |
---|
| 15 | |
---|
[c81ebf9] | 16 | extern "C" { |
---|
[82ff5845] | 17 | #include <errno.h> |
---|
| 18 | #include <stdio.h> |
---|
| 19 | #include <string.h> |
---|
| 20 | #include <unistd.h> |
---|
[c81ebf9] | 21 | #include <sys/time.h> |
---|
| 22 | } |
---|
| 23 | |
---|
[6cff9f3] | 24 | #include "alarm.h" |
---|
| 25 | #include "kernel_private.h" |
---|
[c81ebf9] | 26 | #include "preemption.h" |
---|
[6cff9f3] | 27 | |
---|
[c81ebf9] | 28 | //============================================================================================= |
---|
| 29 | // Clock logic |
---|
| 30 | //============================================================================================= |
---|
| 31 | |
---|
[2a84d06d] | 32 | Time __kernel_get_time() { |
---|
[c81ebf9] | 33 | timespec curr; |
---|
[2a84d06d] | 34 | clock_gettime( CLOCK_MONOTONIC_RAW, &curr ); // CLOCK_REALTIME |
---|
| 35 | return (Time){ curr }; |
---|
[c81ebf9] | 36 | } |
---|
| 37 | |
---|
[2a84d06d] | 38 | void __kernel_set_timer( Duration alarm ) { |
---|
[b1a4300] | 39 | verifyf(alarm >= 1`us || alarm == 0, "Setting timer to < 1us (%luns)", alarm.tv); |
---|
[2a84d06d] | 40 | setitimer( ITIMER_REAL, &(itimerval){ alarm }, NULL ); |
---|
[6cff9f3] | 41 | } |
---|
| 42 | |
---|
[c81ebf9] | 43 | //============================================================================================= |
---|
| 44 | // Alarm logic |
---|
| 45 | //============================================================================================= |
---|
| 46 | |
---|
[2a84d06d] | 47 | void ?{}( alarm_node_t & this, thread_desc * thrd, Time alarm, Duration period ) with( this ) { |
---|
[242a902] | 48 | this.thrd = thrd; |
---|
| 49 | this.alarm = alarm; |
---|
| 50 | this.period = period; |
---|
[c40e7c5] | 51 | next = 0; |
---|
| 52 | set = false; |
---|
| 53 | kernel_alarm = false; |
---|
[6cff9f3] | 54 | } |
---|
| 55 | |
---|
[2a84d06d] | 56 | void ?{}( alarm_node_t & this, processor * proc, Time alarm, Duration period ) with( this ) { |
---|
[242a902] | 57 | this.proc = proc; |
---|
| 58 | this.alarm = alarm; |
---|
| 59 | this.period = period; |
---|
[c40e7c5] | 60 | next = 0; |
---|
| 61 | set = false; |
---|
| 62 | kernel_alarm = true; |
---|
[c81ebf9] | 63 | } |
---|
[6cff9f3] | 64 | |
---|
[242a902] | 65 | void ^?{}( alarm_node_t & this ) { |
---|
| 66 | if( this.set ) { |
---|
| 67 | unregister_self( &this ); |
---|
[c81ebf9] | 68 | } |
---|
[6cff9f3] | 69 | } |
---|
| 70 | |
---|
[b1a4300] | 71 | #if !defined(NDEBUG) && (defined(__CFA_DEBUG__) || defined(__CFA_VERIFY__)) |
---|
| 72 | bool validate( alarm_list_t * this ) { |
---|
[4e6fb8e] | 73 | alarm_node_t ** it = &this->head; |
---|
| 74 | while( (*it) ) { |
---|
| 75 | it = &(*it)->next; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | return it == this->tail; |
---|
[b1a4300] | 79 | } |
---|
| 80 | #endif |
---|
[4e6fb8e] | 81 | |
---|
[6cff9f3] | 82 | static inline void insert_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t p ) { |
---|
[0b33412] | 83 | verify( !n->next ); |
---|
[6cff9f3] | 84 | if( p == this->tail ) { |
---|
| 85 | this->tail = &n->next; |
---|
| 86 | } |
---|
| 87 | else { |
---|
| 88 | n->next = *p; |
---|
| 89 | } |
---|
| 90 | *p = n; |
---|
[4e6fb8e] | 91 | |
---|
[0b33412] | 92 | verify( validate( this ) ); |
---|
[6cff9f3] | 93 | } |
---|
| 94 | |
---|
| 95 | void insert( alarm_list_t * this, alarm_node_t * n ) { |
---|
| 96 | alarm_node_t ** it = &this->head; |
---|
| 97 | while( (*it) && (n->alarm > (*it)->alarm) ) { |
---|
| 98 | it = &(*it)->next; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | insert_at( this, n, it ); |
---|
[4e6fb8e] | 102 | |
---|
[0b33412] | 103 | verify( validate( this ) ); |
---|
[6cff9f3] | 104 | } |
---|
| 105 | |
---|
[c81ebf9] | 106 | alarm_node_t * pop( alarm_list_t * this ) { |
---|
[6cff9f3] | 107 | alarm_node_t * head = this->head; |
---|
| 108 | if( head ) { |
---|
| 109 | this->head = head->next; |
---|
| 110 | if( !head->next ) { |
---|
| 111 | this->tail = &this->head; |
---|
| 112 | } |
---|
| 113 | head->next = NULL; |
---|
| 114 | } |
---|
[0b33412] | 115 | verify( validate( this ) ); |
---|
[6cff9f3] | 116 | return head; |
---|
[c81ebf9] | 117 | } |
---|
| 118 | |
---|
| 119 | static inline void remove_at( alarm_list_t * this, alarm_node_t * n, __alarm_it_t it ) { |
---|
[4aa2fb2] | 120 | verify( it ); |
---|
[ec35498] | 121 | verify( (*it) == n ); |
---|
[c81ebf9] | 122 | |
---|
[4e6fb8e] | 123 | (*it) = n->next; |
---|
[c81ebf9] | 124 | if( !n-> next ) { |
---|
| 125 | this->tail = it; |
---|
| 126 | } |
---|
| 127 | n->next = NULL; |
---|
[4e6fb8e] | 128 | |
---|
[0b33412] | 129 | verify( validate( this ) ); |
---|
[c81ebf9] | 130 | } |
---|
| 131 | |
---|
| 132 | static inline void remove( alarm_list_t * this, alarm_node_t * n ) { |
---|
| 133 | alarm_node_t ** it = &this->head; |
---|
[4e6fb8e] | 134 | while( (*it) && (*it) != n ) { |
---|
[c81ebf9] | 135 | it = &(*it)->next; |
---|
| 136 | } |
---|
| 137 | |
---|
[0b33412] | 138 | verify( validate( this ) ); |
---|
[4e6fb8e] | 139 | |
---|
[c81ebf9] | 140 | if( *it ) { remove_at( this, n, it ); } |
---|
[4e6fb8e] | 141 | |
---|
[0b33412] | 142 | verify( validate( this ) ); |
---|
[c81ebf9] | 143 | } |
---|
| 144 | |
---|
| 145 | void register_self( alarm_node_t * this ) { |
---|
[e60e0dc] | 146 | alarm_list_t * alarms = &event_kernel->alarms; |
---|
| 147 | |
---|
[c81ebf9] | 148 | disable_interrupts(); |
---|
[36982fc] | 149 | lock( event_kernel->lock __cfaabi_dbg_ctx2 ); |
---|
[c81ebf9] | 150 | { |
---|
[e60e0dc] | 151 | verify( validate( alarms ) ); |
---|
| 152 | bool first = !alarms->head; |
---|
[82ff5845] | 153 | |
---|
[e60e0dc] | 154 | insert( alarms, this ); |
---|
[82ff5845] | 155 | if( first ) { |
---|
[e60e0dc] | 156 | __kernel_set_timer( alarms->head->alarm - __kernel_get_time() ); |
---|
[82ff5845] | 157 | } |
---|
[c81ebf9] | 158 | } |
---|
[ea7d2b0] | 159 | unlock( event_kernel->lock ); |
---|
[c81ebf9] | 160 | this->set = true; |
---|
[36982fc] | 161 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[c81ebf9] | 162 | } |
---|
| 163 | |
---|
| 164 | void unregister_self( alarm_node_t * this ) { |
---|
| 165 | disable_interrupts(); |
---|
[36982fc] | 166 | lock( event_kernel->lock __cfaabi_dbg_ctx2 ); |
---|
[0b33412] | 167 | { |
---|
[e60e0dc] | 168 | verify( validate( &event_kernel->alarms ) ); |
---|
| 169 | remove( &event_kernel->alarms, this ); |
---|
[0b33412] | 170 | } |
---|
[ea7d2b0] | 171 | unlock( event_kernel->lock ); |
---|
[36982fc] | 172 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[c81ebf9] | 173 | this->set = false; |
---|
[4aa2fb2] | 174 | } |
---|
[6b0b624] | 175 | |
---|
| 176 | // Local Variables: // |
---|
| 177 | // mode: c // |
---|
| 178 | // tab-width: 4 // |
---|
| 179 | // End: // |
---|