reindent-20030715
[openafs.git] / src / lwp / fasttime.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  *      fasttime.c -- Get the time of day quickly by mapping the kernel's
12  *                    time of day variable.
13  *
14  *      6 January 1986
15  *
16  *      Modification History
17  *      3/21/86:  Added FT_ApproxTime which returns the last time
18  *                in seconds returned by RT_FastTime.  The intent is to give
19  *                routines which aren't too concerned about the exact time
20  *                fast access to the time, even on kernels without mmap.
21  *      4/2/86:   Fixed my previous mod and fixed FT_Init so it doesn't initialize
22  *                a second time if explicitly called after being implicitly called.
23  *                This saves a (precious) file descriptor.
24  */
25
26 #include <afsconfig.h>
27 #include <afs/param.h>
28
29 RCSID
30     ("$Header$");
31
32 #include <stdio.h>
33 #include <sys/types.h>
34 #ifdef AFS_NT40_ENV
35 #include <sys/timeb.h>
36 #include <winsock2.h>
37 #else
38 #include <sys/time.h>
39 #include <sys/file.h>
40 #endif
41 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV)
42 #include <sys/mman.h>
43 #endif
44 #include <afs/afsutil.h>
45
46 extern char *valloc();
47 int ft_debug;
48
49 #define TRUE    1
50 #define FALSE   0
51
52 static enum InitState { notTried, tried, done } initState = notTried;
53
54 struct timeval FT_LastTime;     /* last time returned by RT_FastTime.  Used to implement
55                                  * FT_ApproxTime */
56
57
58 /* Call this to get the memory mapped.  It will return -1 if anything went
59    wrong.  In that case, calls to FT_GetTimeOfDay will call gettimeofday
60    instead.  If printErrors is true, errors in initialization will cause
61    error messages to be printed on stderr.  If notReally is true, then
62    things are set up so that all calls to FT_GetTimeOfDay call gettimeofday.
63    You might want this if your program won't run too long and the nlist
64    call is too expensive.  Yeah, it's pretty horrible.
65 */
66 int
67 FT_Init(int printErrors, int notReally)
68 {
69     if (initState != notTried && !notReally)
70         return (initState == done ? 0 : -1);    /* This is in case explicit initialization
71                                                  * occurs after automatic initialization */
72     initState = tried;
73     if (notReally)
74         return 0;               /* fake success, but leave initState
75                                  * wrong. */
76     if (printErrors)
77         fprintf(stderr, "FT_Init: mmap  not implemented on this kernel\n");
78     return (-1);
79 }
80
81 /* Call this to get the time of day.  It will automatically initialize the
82    first time you call it.  If you want error messages when you initialize,
83    call FT_Init yourself.  If the initialization failed, this will just
84    call gettimeofday.  If you ask for the timezone info, this routine will
85    punt to gettimeofday. */
86 int
87 FT_GetTimeOfDay(struct timeval *tv, struct timezone *tz)
88 {
89     register int ret;
90     ret = gettimeofday(tv, tz);
91     if (!ret) {
92         /* need to bounds check 'cause Unix can fail these checks, (esp on Suns)
93          * and time package can generate invalid (to select syscall) values
94          * for the time until the next interesting event if it encounters
95          * out of range microsecond fields */
96         if (tv->tv_usec < 0)
97             tv->tv_usec = 0;
98         if (tv->tv_usec > 999999)
99             tv->tv_usec = 999999;
100         FT_LastTime.tv_sec = tv->tv_sec;
101         FT_LastTime.tv_usec = tv->tv_usec;
102     }
103     return ret;
104 }
105
106
107 /* For compatibility.  Should go away. */
108 int
109 TM_GetTimeOfDay(struct timeval *tv, struct timezone *tz)
110 {
111     return FT_GetTimeOfDay(tv, tz);
112 }
113
114 int
115 FT_AGetTimeOfDay(struct timeval *tv, struct timezone *tz)
116 {
117     if (FT_LastTime.tv_sec) {
118         tv->tv_sec = FT_LastTime.tv_sec;
119         tv->tv_usec = FT_LastTime.tv_usec;
120         return 0;
121     }
122     return FT_GetTimeOfDay(tv, tz);
123 }
124
125 unsigned int
126 FT_ApproxTime(void)
127 {
128     if (!FT_LastTime.tv_sec) {
129         FT_GetTimeOfDay(&FT_LastTime, 0);
130     }
131     return FT_LastTime.tv_sec;
132 }