etimer.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include "contiki-conf.h"
00049
00050 #include "sys/etimer.h"
00051 #include "sys/process.h"
00052
00053 static struct etimer *timerlist;
00054 static clock_time_t next_expiration;
00055
00056 PROCESS(etimer_process, "Event timer");
00057
00058 static void
00059 update_time(void)
00060 {
00061 clock_time_t tdist;
00062 clock_time_t now;
00063 struct etimer *t;
00064
00065 if (timerlist == NULL) {
00066 next_expiration = 0;
00067 } else {
00068 now = clock_time();
00069 t = timerlist;
00070
00071 tdist = t->timer.start + t->timer.interval - now;
00072 for(t = t->next; t != NULL; t = t->next) {
00073 if(t->timer.start + t->timer.interval - now < tdist) {
00074 tdist = t->timer.start + t->timer.interval - now;
00075 }
00076 }
00077 next_expiration = now + tdist;
00078 }
00079 }
00080
00081 PROCESS_THREAD(etimer_process, ev, data)
00082 {
00083 struct etimer *t, *u;
00084
00085 PROCESS_BEGIN();
00086
00087 timerlist = NULL;
00088
00089 while(1) {
00090 PROCESS_YIELD();
00091
00092 if(ev == PROCESS_EVENT_EXITED) {
00093 struct process *p = data;
00094
00095 while(timerlist != NULL && timerlist->p == p) {
00096 timerlist = timerlist->next;
00097 }
00098
00099 if(timerlist != NULL) {
00100 t = timerlist;
00101 while(t->next != NULL) {
00102 if(t->next->p == p) {
00103 t->next = t->next->next;
00104 } else
00105 t = t->next;
00106 }
00107 }
00108 continue;
00109 } else if(ev != PROCESS_EVENT_POLL) {
00110 continue;
00111 }
00112
00113 again:
00114
00115 u = NULL;
00116
00117 for(t = timerlist; t != NULL; t = t->next) {
00118 if(timer_expired(&t->timer)) {
00119 if(process_post(t->p, PROCESS_EVENT_TIMER, t) == PROCESS_ERR_OK) {
00120
00121
00122
00123
00124 t->p = PROCESS_NONE;
00125 if(u != NULL) {
00126 u->next = t->next;
00127 } else {
00128 timerlist = t->next;
00129 }
00130 t->next = NULL;
00131 update_time();
00132 goto again;
00133 } else {
00134 etimer_request_poll();
00135 }
00136 }
00137 u = t;
00138 }
00139
00140 }
00141
00142 PROCESS_END();
00143 }
00144
00145 void
00146 etimer_request_poll(void)
00147 {
00148 process_poll(&etimer_process);
00149 }
00150
00151 static void
00152 add_timer(struct etimer *timer)
00153 {
00154 struct etimer *t;
00155
00156 etimer_request_poll();
00157
00158 if(timer->p != PROCESS_NONE) {
00159
00160
00161 for(t = timerlist; t != NULL; t = t->next) {
00162 if(t == timer) {
00163
00164 update_time();
00165 return;
00166 }
00167 }
00168 }
00169
00170 timer->p = PROCESS_CURRENT();
00171 timer->next = timerlist;
00172 timerlist = timer;
00173
00174 update_time();
00175 }
00176
00177 void
00178 etimer_set(struct etimer *et, clock_time_t interval)
00179 {
00180 timer_set(&et->timer, interval);
00181 add_timer(et);
00182 }
00183
00184 void
00185 etimer_reset(struct etimer *et)
00186 {
00187 timer_reset(&et->timer);
00188 add_timer(et);
00189 }
00190
00191 void
00192 etimer_restart(struct etimer *et)
00193 {
00194 timer_restart(&et->timer);
00195 add_timer(et);
00196 }
00197
00198 void
00199 etimer_adjust(struct etimer *et, int timediff)
00200 {
00201 et->timer.start += timediff;
00202 update_time();
00203 }
00204
00205 int
00206 etimer_expired(struct etimer *et)
00207 {
00208 return et->p == PROCESS_NONE;
00209 }
00210
00211 clock_time_t
00212 etimer_expiration_time(struct etimer *et)
00213 {
00214 return et->timer.start + et->timer.interval;
00215 }
00216
00217 clock_time_t
00218 etimer_start_time(struct etimer *et)
00219 {
00220 return et->timer.start;
00221 }
00222
00223 int
00224 etimer_pending(void)
00225 {
00226 return timerlist != NULL;
00227 }
00228
00229 clock_time_t
00230 etimer_next_expiration_time(void)
00231 {
00232 return etimer_pending() ? next_expiration : 0;
00233 }
00234
00235 void
00236 etimer_stop(struct etimer *et)
00237 {
00238 struct etimer *t;
00239
00240
00241 if(et == timerlist) {
00242 timerlist = timerlist->next;
00243 update_time();
00244 } else {
00245
00246
00247 for(t = timerlist; t != NULL && t->next != et; t = t->next);
00248
00249 if(t != NULL) {
00250
00251
00252
00253 t->next = et->next;
00254
00255 update_time();
00256 }
00257 }
00258
00259
00260 et->next = NULL;
00261
00262 et->p = PROCESS_NONE;
00263 }
00264
00265