Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / lwp / fasttime.c
1
2 #ifndef lint
3 #endif
4
5 /*
6 ****************************************************************************
7 *        Copyright IBM Corporation 1988, 1989 - All Rights Reserved        *
8 *                                                                          *
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.                              *
16 *                                                                          *
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 ****************************************************************************
24 */
25 /*
26  *      fasttime.c -- Get the time of day quickly by mapping the kernel's
27  *                    time of day variable.
28  *
29  *      6 January 1986
30  *
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.
39  */
40
41 #include <afs/param.h>
42 #include <stdio.h>
43 #include <sys/types.h>
44 #ifdef AFS_NT40_ENV
45 #include <sys/timeb.h>
46 #include <winsock2.h>
47 #else
48 #include <sys/time.h>
49 #include <sys/file.h>
50 #endif
51 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV)
52 #include <sys/mman.h>
53 #endif
54 #include <afs/afsutil.h>
55
56 extern char *valloc ();
57 int ft_debug;
58
59 #define TRUE    1
60 #define FALSE   0
61
62 static enum InitState { notTried, tried, done } initState = notTried;
63
64 struct timeval FT_LastTime;     /* last time returned by RT_FastTime.  Used to implement
65                                    FT_ApproxTime */
66
67
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.
75 */
76 int FT_Init (int printErrors, int notReally)
77 {
78     if (initState != notTried && !notReally)
79         return (initState == done? 0: -1);      /* This is in case explicit initialization
80                                                    occurs after automatic initialization */
81     initState = tried;
82     if (notReally)
83         return 0;               /* fake success, but leave initState
84                                    wrong. */
85     if (printErrors)
86         fprintf (stderr, "FT_Init: mmap  not implemented on this kernel\n");
87     return (-1);
88 }
89
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)
96 {
97     register int ret;
98     ret = gettimeofday (tv, tz);
99     if (!ret) {
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;
108     }
109     return ret;
110 }
111
112
113 /* For compatibility.  Should go away. */
114 TM_GetTimeOfDay (tv, tz)
115     struct timeval *tv;
116     struct timezone *tz;
117 {
118     return FT_GetTimeOfDay(tv, tz);
119 }
120
121 int FT_AGetTimeOfDay(struct timeval *tv, struct timezone *tz)
122 {
123     if (FT_LastTime.tv_sec) {
124         tv->tv_sec = FT_LastTime.tv_sec;
125         tv->tv_usec = FT_LastTime.tv_usec;
126         return 0;
127     }
128     return FT_GetTimeOfDay(tv, tz);
129 }
130
131 unsigned int FT_ApproxTime(void)
132 {
133     if (!FT_LastTime.tv_sec) {
134         FT_GetTimeOfDay(&FT_LastTime, 0);
135     }
136     return FT_LastTime.tv_sec;
137 }