4 ****************************************************************************
5 * Copyright IBM Corporation 1988, 1989 - All Rights Reserved *
7 * Permission to use, copy, modify, and distribute this software and its *
8 * documentation for any purpose and without fee is hereby granted, *
9 * provided that the above copyright notice appear in all copies and *
10 * that both that copyright notice and this permission notice appear in *
11 * supporting documentation, and that the name of IBM not be used in *
12 * advertising or publicity pertaining to distribution of the software *
13 * without specific, written prior permission. *
15 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY *
18 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER *
19 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING *
20 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
21 ****************************************************************************
24 /* Elapsed time package
26 * A time package that make sure time never goes backwards
33 #if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV)
34 #include "../h/systm.h"
35 #include "../h/time.h"
40 #endif /* ITIMER_REAL */
44 * Some macros to make macros more reasonable (this allows a block to
45 * be used within a macro which does not cause if statements to screw
46 * up). That is, you can use "if (...) macro_name(); else ...;"
47 * without having things blow up on the semi-colon.
52 #define END } while(0)
55 /* A clock value is the number of seconds and microseconds that have elapsed since calling clock_Init. */
57 long sec
; /* Seconds since clock_Init */
58 long usec
; /* Microseconds since clock_Init */
63 /* For internal use. The last value returned from clock_GetTime() */
64 extern struct clock clock_now
;
66 /* For internal use: this flag, if set, indicates a new time should be read by clock_getTime() */
68 extern int clock_haveCurrentTime
;
70 /* For external use: the number of times the clock value is actually updated */
71 extern int clock_nUpdates
;
73 /* Initialize the clock package */
74 void clock_Init (void);
76 /* Restart the interval timer */
77 void clock_ReInit(void);
79 #define clock_NewTime() (clock_haveCurrentTime = 0)
81 /* Update the value to be returned by gettime */
82 void clock_UpdateTime (void);
84 /* Return the current clock time. If the clock value has not been updated since the last call to clock_NewTime, it is updated now */
85 #define clock_GetTime(cv) \
87 if (!clock_haveCurrentTime) clock_UpdateTime(); \
88 (cv)->sec = clock_now.sec; \
89 (cv)->usec = clock_now.usec; \
92 /* Current clock time, truncated to seconds */
93 #define clock_Sec() ((!clock_haveCurrentTime)? clock_UpdateTime(), clock_now.sec:clock_now.sec)
98 #define clock_GetTime(cv) osi_GetTime((struct timeval *)cv) /* Bogus--uses TOD clock */
99 #define clock_Sec() osi_Time()
100 #define clock_NewTime() /* don't do anything; clock is fast
101 * enough in kernel */
104 /* Returns the elapsed time in milliseconds between clock values (*cv1) and (*cv2) */
105 #define clock_ElapsedTime(cv1, cv2) \
106 (((cv2)->sec - (cv1)->sec)*1000 + ((cv2)->usec - (cv1)->usec)/1000)
108 /* Advance the known value of the current clock time (clock_now) by the specified clock value */
109 #define clock_Advance(cv) clock_Add(&clock_now, cv)
111 /* Some comparison operators for clock values */
112 #define clock_Gt(a, b) ((a)->sec>(b)->sec || ((a)->sec==(b)->sec && (a)->usec>(b)->usec))
113 #define clock_Ge(a, b) ((a)->sec>(b)->sec || ((a)->sec==(b)->sec && (a)->usec>=(b)->usec))
114 #define clock_Eq(a, b) ((a)->sec==(b)->sec && (a)->usec==(b)->usec)
115 #define clock_Le(a, b) ((a)->sec<(b)->sec || ((a)->sec==(b)->sec && (a)->usec<=(b)->usec))
116 #define clock_Lt(a, b) ((a)->sec<(b)->sec || ((a)->sec==(b)->sec && (a)->usec<(b)->usec))
118 /* Is the clock value zero? */
119 #define clock_IsZero(c) ((c)->sec == 0 && (c)->usec == 0)
121 /* Set the clock value to zero */
122 #define clock_Zero(c) ((c)->sec = (c)->usec = 0)
124 /* Add time c2 to time c1. Both c2 and c1 must be positive times. */
125 #define clock_Add(c1, c2) \
127 if (((c1)->usec += (c2)->usec) >= 1000000) { \
128 (c1)->usec -= 1000000; \
131 (c1)->sec += (c2)->sec; \
134 #define MSEC(cp) ((cp->sec * 1000) + (cp->usec / 1000))
136 /* Add ms milliseconds to time c1. Both ms and c1 must be positive */
137 /* optimized for ms << 1000 */
138 #define clock_Addmsec(c1, ms) \
140 (c1)->usec += ((unsigned long) (ms)) * 1000; \
141 while ((c1)->usec >= 1000000) { \
142 (c1)->usec -= 1000000; \
147 /* Subtract time c2 from time c1. c2 should be less than c1 */
148 #define clock_Sub(c1, c2) \
150 if (((c1)->usec -= (c2)->usec) < 0) { \
151 (c1)->usec += 1000000; \
154 (c1)->sec -= (c2)->sec; \
157 #define clock_Float(c) ((c)->sec + (c)->usec/1e6)
159 void clock_Init(void);
160 void clock_UpdateTime(void);