NS_STATIC void NS_CLASS timer_add(struct timer *t) { struct timer *curr, *prev; /* Sanity checks: */ if (!t) { perror("NULL timer!!!\n"); exit(-1); } if (!t->handler) { perror("NULL handler!!!\n"); exit(-1); } /* Make sure we remove unexpired timers before adding a new timeout... */ if (t->used) timer_remove(t); t->used = 1; #ifdef DEBUG_TIMER_QUEUE printf("New timer added!\n"); #endif /* Base case when queue is empty: */ if (!TQ) { TQ = t; t->next = NULL; /* Since we insert first in the queue we must reschedule the first timeout */ goto end; } for (prev = NULL, curr = TQ; curr; prev = curr, curr = curr->next) if (timeval_diff(&t->timeout, &curr->timeout) < 0) break; if (curr == TQ) { /* We insert first in queue */ t->next = TQ; TQ = t; } else { t->next = curr; prev->next = t; } end: #ifdef DEBUG_TIMER_QUEUE printTQ(); #endif return; }
/* Called when a timer should timeout */ void timer_timeout() { struct timer *prev, *expiredTQ, *tmp; struct timeval now; gettimeofday(&now, NULL); #ifdef DEBUG_TIMER_QUEUE printf("timer_timeout: called!!\n"); printTQ(); #endif expiredTQ = TQ; for (prev = NULL; TQ != NULL; prev = TQ, TQ = TQ->next) { /* Check if the current timer should not expire yet... */ if (timeval_diff(&TQ->timeout, &now) > 0) { if (prev == NULL) /* No timers have expired yet... */ goto end; prev->next = NULL; break; } } while (expiredTQ) { tmp = expiredTQ; expiredTQ->used = 0; expiredTQ = expiredTQ->next; tmp->next = NULL; #ifdef DEBUG_TIMER_QUEUE printf("timer_timeout: removing timer\n"); #endif /* Execute handler function for expired timer... */ if (tmp->handler) tmp->handler(tmp->data); } /* Schedule a new timeout... */ end: timer_set_alarm(); }
/* Called when a timer should timeout */ void NS_CLASS timer_timeout(struct timeval *now) { struct timer *prev, *expiredTQ, *tmp; #ifdef DEBUG_TIMER_QUEUE printf("\n######## timer_timeout: called!!\n"); printTQ(); #endif expiredTQ = TQ; for (prev = NULL; TQ; prev = TQ, TQ = TQ->next) if (timeval_diff(&TQ->timeout, now) > 0) break; /* No expired timers? */ if (prev == NULL) return; /* Decouple the expired timers from the rest of the queue. */ prev->next = NULL; while (expiredTQ) { tmp = expiredTQ; tmp->used = 0; expiredTQ = expiredTQ->next; #ifdef DEBUG_TIMER_QUEUE printf("removing timer\n"); #endif /* Execute handler function for expired timer... */ if (tmp->handler) { #ifdef NS_PORT (*this.*tmp->handler) (tmp->data); #else tmp->handler(tmp->data); #endif } } }
static void timer_add(struct timer *t) { struct timer *curr, *prev; /* Sanity checks: */ if (!t) { perror("NULL timer!!!\n"); exit(-1); } if (!t->handler) { perror("NULL handler!!!\n"); exit(-1); } /* Make sure we remove unexpired timers before adding a new timeout... */ if (t->used) timer_remove(t); t->used = 1; #ifdef DEBUG_TIMER_QUEUE printf("timer_add: New timer added!\n"); #endif /* Base case when queue is empty: */ if (!TQ) { TQ = t; t->next = NULL; /* Since we insert first in the queue we must reschedule the first timeout */ timer_set_alarm(); goto end; } for (prev = NULL, curr = TQ; curr != NULL; prev = curr, curr = curr->next) { if (timeval_diff(&t->timeout, &curr->timeout) < 0) { /* OK, we found the spot to insert the timer */ if (prev == NULL) { /* We insert first in queue */ TQ = t; t->next = curr; /* Since we insert first in the queue we must reschedule the first timeout */ timer_set_alarm(); goto end; } t->next = curr; prev->next = t; goto end; } } /* We insert last in queue */ prev->next = t; t->next = NULL; end: #ifdef DEBUG_TIMER_QUEUE printTQ(); #endif return; }