2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /*******************************************************************\
13 * Information Technology Center *
14 * Carnegie-Mellon University *
18 \*******************************************************************/
21 #include <afsconfig.h>
22 #include <afs/param.h>
41 typedef unsigned char bool;
46 #define expiration TotalTime
48 #define new_elem() ((struct TM_Elem *) malloc(sizeof(struct TM_Elem)))
50 #define MILLION 1000000
52 static int globalInitDone = 0;
56 subtract(register struct timeval *t1, register struct timeval *t2,
57 register struct timeval *t3)
59 register int sec2, usec2, sec3, usec3;
66 /* Take care of the probably non-existent case where the
67 * usec field has more than 1 second in it. */
69 while (usec3 > usec2) {
74 /* Check for a negative time and use zero for the answer,
75 * since the tv_sec field is unsigned */
79 t1->tv_sec = (afs_uint32) 0;
81 t1->tv_usec = usec2 - usec3;
82 t1->tv_sec = sec2 - sec3;
89 add(register struct timeval *t1, register struct timeval *t2)
91 t1->tv_usec += t2->tv_usec;
92 t1->tv_sec += t2->tv_sec;
93 if (t1->tv_usec >= MILLION) {
95 t1->tv_usec -= MILLION;
102 TM_eql(struct timeval *t1, struct timeval *t2)
104 return (t1->tv_usec == t2->tv_usec) && (t1->tv_sec == t2->tv_sec);
110 obsolete, commentless procedure, all done by hand expansion now.
111 static bool geq(register struct timeval *t1, register struct timeval *t2)
113 return (t1->tv_sec > t2->tv_sec) ||
114 (t1->tv_sec == t2->tv_sec && t1->tv_usec >= t2->tv_usec);
119 blocking(register struct TM_Elem *t)
121 return (t->TotalTime.tv_sec < 0 || t->TotalTime.tv_usec < 0);
127 Initializes a list -- returns -1 if failure, else 0.
131 TM_Init(register struct TM_Elem **list)
133 if (!globalInitDone) {
141 (*list)->Next = *list;
142 (*list)->Prev = *list;
143 (*list)->TotalTime.tv_sec = 0;
144 (*list)->TotalTime.tv_usec = 0;
145 (*list)->TimeLeft.tv_sec = 0;
146 (*list)->TimeLeft.tv_usec = 0;
147 (*list)->BackPointer = NULL;
154 TM_Final(register struct TM_Elem **list)
156 if (list == NULL || *list == NULL)
166 Inserts elem into the timer list pointed to by *tlistPtr.
170 TM_Insert(struct TM_Elem *tlistPtr, struct TM_Elem *elem)
172 register struct TM_Elem *next;
174 /* TimeLeft must be set for function IOMGR with infinite timeouts */
175 elem->TimeLeft = elem->TotalTime;
177 /* Special case -- infinite timeout */
178 if (blocking(elem)) {
179 openafs_insque(elem, tlistPtr->Prev);
183 /* Finite timeout, set expiration time */
184 FT_AGetTimeOfDay(&elem->expiration, 0);
185 add(&elem->expiration, &elem->TimeLeft);
187 FOR_ALL_ELTS(p, tlistPtr, {
189 || !(elem->TimeLeft.tv_sec > p->TimeLeft.tv_sec
190 || (elem->TimeLeft.tv_sec == p->TimeLeft.tv_sec
191 && elem->TimeLeft.tv_usec >=
192 p->TimeLeft.tv_usec))
194 next = p; /* Save ptr to element that will be after this one */
201 openafs_insque(elem, next->Prev);
205 Walks through the specified list and updates the TimeLeft fields in it.
206 Returns number of expired elements in the list.
210 TM_Rescan(struct TM_Elem *tlist) /* head pointer of timer list */
213 register int expired;
215 #ifndef AFS_DJGPP_ENV
216 FT_AGetTimeOfDay(&time, 0);
218 FT_GetTimeOfDay(&time, 0); /* we need a real time value */
221 FOR_ALL_ELTS(e, tlist, {
223 subtract(&e->TimeLeft, &e->expiration, &time);
224 if (0 > e->TimeLeft.tv_sec
225 || (0 == e->TimeLeft.tv_sec && 0 >= e->TimeLeft.tv_usec))
233 RETURNS POINTER TO earliest expired entry from tlist.
234 Returns 0 if no expired entries are present.
238 TM_GetExpired(struct TM_Elem *tlist) /* head pointer of timer list */
240 FOR_ALL_ELTS(e, tlist, {
242 && (0 > e->TimeLeft.tv_sec
243 || (0 == e->TimeLeft.tv_sec
244 && 0 >= e->TimeLeft.tv_usec)))
251 Returns a pointer to the earliest unexpired element in tlist.
252 Its TimeLeft field will specify how much time is left.
253 Returns 0 if tlist is empty or if there are no unexpired elements.
257 TM_GetEarliest(struct TM_Elem *tlist)
259 register struct TM_Elem *e;
262 return (e == tlist ? NULL : e);
265 /* This used to be in hputils.c, but it's only use is in the LWP package. */
267 * Emulate the vax instructions for queue insertion and deletion, somewhat.
268 * A std_queue structure is defined here and used by these routines. These
269 * routines use caddr_ts so they can operate on any structure. The std_queue
270 * structure is used rather than proc structures so that when the proc struct
271 * changes only process management code breaks. The ideal solution would be
272 * to define a std_queue as a global type which is part of all the structures
273 * which are manipulated by these routines. This would involve considerable
278 openafs_insque(struct TM_Elem *elementp, struct TM_Elem *quep)
280 elementp->Next = quep->Next;
281 elementp->Prev = quep;
283 quep->Next->Prev = elementp;
284 quep->Next = elementp;
288 openafs_remque(struct TM_Elem *elementp)
290 elementp->Next->Prev = elementp->Prev;
291 elementp->Prev->Next = elementp->Next;
292 elementp->Prev = elementp->Next = NULL;