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>
39 typedef unsigned char bool;
44 #define expiration TotalTime
46 #define new_elem() ((struct TM_Elem *) malloc(sizeof(struct TM_Elem)))
48 #define MILLION 1000000
50 static int globalInitDone = 0;
54 subtract(struct timeval *t1, struct timeval *t2,
57 int sec2, usec2, sec3, usec3;
64 /* Take care of the probably non-existent case where the
65 * usec field has more than 1 second in it. */
67 while (usec3 > usec2) {
72 /* Check for a negative time and use zero for the answer,
73 * since the tv_sec field is unsigned */
77 t1->tv_sec = (afs_uint32) 0;
79 t1->tv_usec = usec2 - usec3;
80 t1->tv_sec = sec2 - sec3;
87 add(struct timeval *t1, struct timeval *t2)
89 t1->tv_usec += t2->tv_usec;
90 t1->tv_sec += t2->tv_sec;
91 if (t1->tv_usec >= MILLION) {
93 t1->tv_usec -= MILLION;
100 TM_eql(struct timeval *t1, struct timeval *t2)
102 return (t1->tv_usec == t2->tv_usec) && (t1->tv_sec == t2->tv_sec);
108 obsolete, commentless procedure, all done by hand expansion now.
109 static bool geq(struct timeval *t1, struct timeval *t2)
111 return (t1->tv_sec > t2->tv_sec) ||
112 (t1->tv_sec == t2->tv_sec && t1->tv_usec >= t2->tv_usec);
117 blocking(struct TM_Elem *t)
119 return (t->TotalTime.tv_sec < 0 || t->TotalTime.tv_usec < 0);
125 Initializes a list -- returns -1 if failure, else 0.
129 TM_Init(struct TM_Elem **list)
131 if (!globalInitDone) {
139 (*list)->Next = *list;
140 (*list)->Prev = *list;
141 (*list)->TotalTime.tv_sec = 0;
142 (*list)->TotalTime.tv_usec = 0;
143 (*list)->TimeLeft.tv_sec = 0;
144 (*list)->TimeLeft.tv_usec = 0;
145 (*list)->BackPointer = NULL;
152 TM_Final(struct TM_Elem **list)
154 if (list == NULL || *list == NULL)
164 Inserts elem into the timer list pointed to by *tlistPtr.
168 TM_Insert(struct TM_Elem *tlistPtr, struct TM_Elem *elem)
170 struct TM_Elem *next;
172 /* TimeLeft must be set for function IOMGR with infinite timeouts */
173 elem->TimeLeft = elem->TotalTime;
175 /* Special case -- infinite timeout */
176 if (blocking(elem)) {
177 openafs_insque(elem, tlistPtr->Prev);
181 /* Finite timeout, set expiration time */
182 FT_AGetTimeOfDay(&elem->expiration, 0);
183 add(&elem->expiration, &elem->TimeLeft);
185 FOR_ALL_ELTS(p, tlistPtr, {
187 || !(elem->TimeLeft.tv_sec > p->TimeLeft.tv_sec
188 || (elem->TimeLeft.tv_sec == p->TimeLeft.tv_sec
189 && elem->TimeLeft.tv_usec >=
190 p->TimeLeft.tv_usec))
192 next = p; /* Save ptr to element that will be after this one */
199 openafs_insque(elem, next->Prev);
203 Walks through the specified list and updates the TimeLeft fields in it.
204 Returns number of expired elements in the list.
208 TM_Rescan(struct TM_Elem *tlist) /* head pointer of timer list */
213 FT_AGetTimeOfDay(&time, 0);
215 FOR_ALL_ELTS(e, tlist, {
217 subtract(&e->TimeLeft, &e->expiration, &time);
218 if (0 > e->TimeLeft.tv_sec
219 || (0 == e->TimeLeft.tv_sec && 0 >= e->TimeLeft.tv_usec))
227 RETURNS POINTER TO earliest expired entry from tlist.
228 Returns 0 if no expired entries are present.
232 TM_GetExpired(struct TM_Elem *tlist) /* head pointer of timer list */
234 FOR_ALL_ELTS(e, tlist, {
236 && (0 > e->TimeLeft.tv_sec
237 || (0 == e->TimeLeft.tv_sec
238 && 0 >= e->TimeLeft.tv_usec)))
245 Returns a pointer to the earliest unexpired element in tlist.
246 Its TimeLeft field will specify how much time is left.
247 Returns 0 if tlist is empty or if there are no unexpired elements.
251 TM_GetEarliest(struct TM_Elem *tlist)
256 return (e == tlist ? NULL : e);
259 /* This used to be in hputils.c, but it's only use is in the LWP package. */
261 * Emulate the vax instructions for queue insertion and deletion, somewhat.
262 * A std_queue structure is defined here and used by these routines. These
263 * routines use caddr_ts so they can operate on any structure. The std_queue
264 * structure is used rather than proc structures so that when the proc struct
265 * changes only process management code breaks. The ideal solution would be
266 * to define a std_queue as a global type which is part of all the structures
267 * which are manipulated by these routines. This would involve considerable
272 openafs_insque(struct TM_Elem *elementp, struct TM_Elem *quep)
274 elementp->Next = quep->Next;
275 elementp->Prev = quep;
277 quep->Next->Prev = elementp;
278 quep->Next = elementp;
282 openafs_remque(struct TM_Elem *elementp)
284 elementp->Next->Prev = elementp->Prev;
285 elementp->Prev->Next = elementp->Next;
286 elementp->Prev = elementp->Next = NULL;