6 ****************************************************************************
7 * Copyright IBM Corporation 1988, 1989 - All Rights Reserved *
9 * Permission to use, copy, modify, and distribute this software and its *
10 * documentation for any purpose and without fee is hereby granted, *
11 * provided that the above copyright notice appear in all copies and *
12 * that both that copyright notice and this permission notice appear in *
13 * supporting documentation, and that the name of IBM not be used in *
14 * advertising or publicity pertaining to distribution of the software *
15 * without specific, written prior permission. *
17 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
19 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY *
20 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER *
21 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING *
22 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
23 ****************************************************************************
26 * fasttime.c -- Get the time of day quickly by mapping the kernel's
27 * time of day variable.
31 * Modification History
32 * 3/21/86: Added FT_ApproxTime which returns the last time
33 * in seconds returned by RT_FastTime. The intent is to give
34 * routines which aren't too concerned about the exact time
35 * fast access to the time, even on kernels without mmap.
36 * 4/2/86: Fixed my previous mod and fixed FT_Init so it doesn't initialize
37 * a second time if explicitly called after being implicitly called.
38 * This saves a (precious) file descriptor.
41 #include <afs/param.h>
43 #include <sys/types.h>
45 #include <sys/timeb.h>
51 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV)
54 #include <afs/afsutil.h>
56 extern char *valloc ();
62 static enum InitState { notTried, tried, done } initState = notTried;
64 struct timeval FT_LastTime; /* last time returned by RT_FastTime. Used to implement
68 /* Call this to get the memory mapped. It will return -1 if anything went
69 wrong. In that case, calls to FT_GetTimeOfDay will call gettimeofday
70 instead. If printErrors is true, errors in initialization will cause
71 error messages to be printed on stderr. If notReally is true, then
72 things are set up so that all calls to FT_GetTimeOfDay call gettimeofday.
73 You might want this if your program won't run too long and the nlist
74 call is too expensive. Yeah, it's pretty horrible.
76 int FT_Init (int printErrors, int notReally)
78 if (initState != notTried && !notReally)
79 return (initState == done? 0: -1); /* This is in case explicit initialization
80 occurs after automatic initialization */
83 return 0; /* fake success, but leave initState
86 fprintf (stderr, "FT_Init: mmap not implemented on this kernel\n");
90 /* Call this to get the time of day. It will automatically initialize the
91 first time you call it. If you want error messages when you initialize,
92 call FT_Init yourself. If the initialization failed, this will just
93 call gettimeofday. If you ask for the timezone info, this routine will
94 punt to gettimeofday. */
95 int FT_GetTimeOfDay(struct timeval *tv, struct timezone *tz)
98 ret = gettimeofday (tv, tz);
100 /* need to bounds check 'cause Unix can fail these checks, (esp on Suns)
101 and time package can generate invalid (to select syscall) values
102 for the time until the next interesting event if it encounters
103 out of range microsecond fields */
104 if (tv->tv_usec < 0) tv->tv_usec = 0;
105 if (tv->tv_usec > 999999) tv->tv_usec = 999999;
106 FT_LastTime.tv_sec = tv->tv_sec;
107 FT_LastTime.tv_usec = tv->tv_usec;
113 /* For compatibility. Should go away. */
114 TM_GetTimeOfDay (tv, tz)
118 return FT_GetTimeOfDay(tv, tz);
121 int FT_AGetTimeOfDay(struct timeval *tv, struct timezone *tz)
123 if (FT_LastTime.tv_sec) {
124 tv->tv_sec = FT_LastTime.tv_sec;
125 tv->tv_usec = FT_LastTime.tv_usec;
128 return FT_GetTimeOfDay(tv, tz);
131 unsigned int FT_ApproxTime(void)
133 if (!FT_LastTime.tv_sec) {
134 FT_GetTimeOfDay(&FT_LastTime, 0);
136 return FT_LastTime.tv_sec;