static void TimerService(int data) { struct btTimer *tp, *exp, *next; tp = TimerList; if (tp) { tp->rest = 0; /* Multiple timers might expire at once. Create a list of expired timers */ exp = NULL; do { tp->state = TIMER_EXPIRED; next = tp->next; tp->enext = exp; exp = tp; tp = next; } while (tp && tp->rest == 0); TimerList = tp; if (TimerList != NULL) /* Any timers remaining ? */ timer_InitService(1); /* Restart the Timer Service */ else timer_TermService(); /* Stop the Timer Service */ /* Process all expired timers */ while (exp) { ExpiredList = exp->enext; exp->enext = NULL; if (exp->func) (*exp->func)(exp->arg); exp = ExpiredList; } } }
void timer_Start(struct pppTimer *tp) { struct itimerval itimer; struct pppTimer *t, *pt; u_long ticks = 0; sigset_t mask, omask; sigemptyset(&mask); sigaddset(&mask, SIGALRM); sigprocmask(SIG_BLOCK, &mask, &omask); if (tp->state != TIMER_STOPPED) StopTimerNoBlock(tp); if (tp->load == 0) { log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp); sigprocmask(SIG_SETMASK, &omask, NULL); return; } /* * We just need to insert tp in the correct relative place. We don't * need to adjust TimerList->rest (yet). */ if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0) ticks = RESTVAL(itimer) - TimerList->rest; pt = NULL; for (t = TimerList; t; t = t->next) { if (ticks + t->rest >= tp->load) break; ticks += t->rest; pt = t; } tp->state = TIMER_RUNNING; tp->rest = tp->load - ticks; if (t) log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s " "timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest); else log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp); /* Insert given *tp just before *t */ tp->next = t; if (pt) { pt->next = tp; } else { TimerList = tp; timer_InitService(t != NULL); /* [re]Start the Timer Service */ } if (t) t->rest -= tp->rest; sigprocmask(SIG_SETMASK, &omask, NULL); }
static void StopTimerNoBlock(struct pppTimer *tp) { struct itimerval itimer; struct pppTimer *t, *pt; /* * A RUNNING timer must be removed from TimerList (->next list). * A STOPPED timer isn't in any list, but may have a bogus [e]next field. * An EXPIRED timer is in the ->enext list. */ if (tp->state == TIMER_STOPPED) return; pt = NULL; for (t = TimerList; t != tp && t != NULL; t = t->next) pt = t; if (t) { if (pt) pt->next = t->next; else { TimerList = t->next; if (TimerList == NULL) /* Last one ? */ timer_TermService(); /* Terminate Timer Service */ } if (t->next) { if (!pt && getitimer(ITIMER_REAL, &itimer) == 0) t->next->rest += RESTVAL(itimer); /* t (tp) was the first in the list */ else t->next->rest += t->rest; if (!pt && t->next->rest > 0) /* t->next is now the first in the list */ timer_InitService(1); } } else { /* Search for any pending expired timers */ pt = NULL; for (t = ExpiredList; t != tp && t != NULL; t = t->enext) pt = t; if (t) { if (pt) pt->enext = t->enext; else ExpiredList = t->enext; } else if (tp->state == TIMER_RUNNING) log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name); } tp->next = tp->enext = NULL; tp->state = TIMER_STOPPED; }
static void TimerService(void) { struct pppTimer *tp, *exp, *next; if (log_IsKept(LogTIMER)) { static time_t t; /* Only show timers globally every second */ time_t n = time(NULL); if (n > t) timer_Show(LogTIMER, NULL); t = n; } tp = TimerList; if (tp) { tp->rest = 0; /* Multiple timers might expire at once. Create a list of expired timers */ exp = NULL; do { tp->state = TIMER_EXPIRED; next = tp->next; tp->enext = exp; exp = tp; tp = next; } while (tp && tp->rest == 0); TimerList = tp; if (TimerList != NULL) /* Any timers remaining ? */ timer_InitService(1); /* Restart the Timer Service */ else timer_TermService(); /* Stop the Timer Service */ /* Process all expired timers */ while (exp) { ExpiredList = exp->enext; exp->enext = NULL; if (exp->func) (*exp->func)(exp->arg); exp = ExpiredList; } } }