timer.c revision 30676
1/*
2 *		PPP Timer Processing Module
3 *
4 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc.  The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $Id: timer.c,v 1.18 1997/08/25 00:29:30 brian Exp $
21 *
22 *  TODO:
23 */
24#include "defs.h"
25#include <sys/time.h>
26#include <signal.h>
27#include "timeout.h"
28#ifdef SIGALRM
29#include <errno.h>
30#endif
31#include "sig.h"
32
33void StopTimerNoBlock(struct pppTimer *);
34
35void
36StopTimer(struct pppTimer * tp)
37{
38#ifdef SIGALRM
39  int omask;
40
41  omask = sigblock(sigmask(SIGALRM));
42#endif
43  StopTimerNoBlock(tp);
44#ifdef SIGALRM
45  sigsetmask(omask);
46#endif
47}
48
49void
50StartTimer(struct pppTimer * tp)
51{
52  struct pppTimer *t, *pt;
53  u_long ticks = 0;
54
55#ifdef SIGALRM
56  int omask;
57
58  omask = sigblock(sigmask(SIGALRM));
59#endif
60
61  if (tp->state != TIMER_STOPPED) {
62    StopTimerNoBlock(tp);
63  }
64  if (tp->load == 0) {
65    LogPrintf(LogDEBUG, "timer %x has 0 load!\n", tp);
66    sigsetmask(omask);
67    return;
68  }
69  pt = NULL;
70  for (t = TimerList; t; t = t->next) {
71    LogPrintf(LogDEBUG, "StartTimer: %x(%d):  ticks: %d, rest: %d\n",
72	      t, t->state, ticks, t->rest);
73    if (ticks + t->rest >= tp->load)
74      break;
75    ticks += t->rest;
76    pt = t;
77  }
78
79  tp->state = TIMER_RUNNING;
80  tp->rest = tp->load - ticks;
81  LogPrintf(LogDEBUG, "StartTimer: Inserting %x before %x, rest = %d\n",
82	    tp, t, tp->rest);
83  /* Insert given *tp just before *t */
84  tp->next = t;
85  if (pt) {
86    pt->next = tp;
87  } else {
88    InitTimerService();
89    TimerList = tp;
90  }
91  if (t)
92    t->rest -= tp->rest;
93
94#ifdef SIGALRM
95  sigsetmask(omask);
96#endif
97}
98
99void
100StopTimerNoBlock(struct pppTimer * tp)
101{
102  struct pppTimer *t, *pt;
103
104  /*
105   * A Running Timer should be removing TimerList, But STOPPED/EXPIRED is
106   * already removing TimerList. So just marked as TIMER_STOPPED. Do not
107   * change tp->enext!! (Might be Called by expired proc)
108   */
109  LogPrintf(LogDEBUG, "StopTimer: %x, next = %x state=%x\n",
110	    tp, tp->next, tp->state);
111  if (tp->state != TIMER_RUNNING) {
112    tp->next = NULL;
113    tp->state = TIMER_STOPPED;
114    return;
115  }
116  pt = NULL;
117  for (t = TimerList; t != tp && t != NULL; t = t->next)
118    pt = t;
119  if (t) {
120    if (pt) {
121      pt->next = t->next;
122    } else {
123      TimerList = t->next;
124      if (TimerList == NULL)	/* Last one ? */
125	TermTimerService();	/* Terminate Timer Service */
126    }
127    if (t->next)
128      t->next->rest += tp->rest;
129  } else
130    LogPrintf(LogERROR, "Oops, timer not found!!\n");
131
132  tp->next = NULL;
133  tp->state = TIMER_STOPPED;
134}
135
136void
137TimerService()
138{
139  struct pppTimer *tp, *exp, *wt;
140
141  if (LogIsKept(LogDEBUG))
142    ShowTimers();
143  tp = TimerList;
144  if (tp) {
145    tp->rest--;
146    if (tp->rest == 0) {
147
148      /*
149       * Multiple timers may expires at once. Create list of expired timers.
150       */
151      exp = NULL;
152      do {
153	tp->state = TIMER_EXPIRED;
154	wt = tp->next;
155	tp->enext = exp;
156	exp = tp;
157	LogPrintf(LogDEBUG, "TimerService: Add %x to exp\n", tp);
158	tp = wt;
159      } while (tp && (tp->rest == 0));
160
161      TimerList = tp;
162      if (TimerList == NULL)	/* No timers ? */
163	TermTimerService();	/* Terminate Timer Service */
164      LogPrintf(LogDEBUG, "TimerService: next is %x(%d)\n",
165		TimerList, TimerList ? TimerList->rest : 0);
166
167      /*
168       * Process all expired timers.
169       */
170      while (exp) {
171#ifdef notdef
172	StopTimer(exp);
173#endif
174	if (exp->func)
175	  (*exp->func) (exp->arg);
176
177	/*
178	 * Just Removing each item from expired list And exp->enext will be
179	 * intialized at next expire in this funtion.
180	 */
181	exp = exp->enext;
182      }
183    }
184  }
185}
186
187void
188ShowTimers()
189{
190  struct pppTimer *pt;
191
192  LogPrintf(LogDEBUG, "---- Begin of Timer Service List---\n");
193  for (pt = TimerList; pt; pt = pt->next)
194    LogPrintf(LogDEBUG, "%x: load = %d, rest = %d, state =%x\n",
195	      pt, pt->load, pt->rest, pt->state);
196  LogPrintf(LogDEBUG, "---- End of Timer Service List ---\n");
197}
198
199#ifdef SIGALRM
200void
201InitTimerService(void)
202{
203  struct itimerval itimer;
204
205  pending_signal(SIGALRM, (void (*) (int)) TimerService);
206  itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0;
207  itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT;
208  if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
209    LogPrintf(LogERROR, "Unable to set itimer.\n");
210}
211
212void
213TermTimerService(void)
214{
215  struct itimerval itimer;
216
217  itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
218  itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
219  if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
220    LogPrintf(LogERROR, "Unable to set itimer.\n");
221  pending_signal(SIGALRM, SIG_IGN);
222}
223
224#endif
225