libwp: Tidy header includes
[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 #include <roken.h>
30
31 #include <afs/afsutil.h>
32
33 int ft_debug;
34
35 #define TRUE    1
36 #define FALSE   0
37
38 static enum InitState { notTried, tried, done } initState = notTried;
39
40 struct timeval FT_LastTime;     /* last time returned by RT_FastTime.  Used to implement
41                                  * FT_ApproxTime */
42
43
44 /* Call this to get the memory mapped.  It will return -1 if anything went
45    wrong.  In that case, calls to FT_GetTimeOfDay will call gettimeofday
46    instead.  If printErrors is true, errors in initialization will cause
47    error messages to be printed on stderr.  If notReally is true, then
48    things are set up so that all calls to FT_GetTimeOfDay call gettimeofday.
49    You might want this if your program won't run too long and the nlist
50    call is too expensive.  Yeah, it's pretty horrible.
51 */
52 int
53 FT_Init(int printErrors, int notReally)
54 {
55     if (initState != notTried && !notReally)
56         return (initState == done ? 0 : -1);    /* This is in case explicit initialization
57                                                  * occurs after automatic initialization */
58     initState = tried;
59     if (notReally)
60         return 0;               /* fake success, but leave initState
61                                  * wrong. */
62     if (printErrors)
63         fprintf(stderr, "FT_Init: mmap  not implemented on this kernel\n");
64     return (-1);
65 }
66
67 /* Call this to get the time of day.  It will automatically initialize the
68    first time you call it.  If you want error messages when you initialize,
69    call FT_Init yourself.  If the initialization failed, this will just
70    call gettimeofday.  If you ask for the timezone info, this routine will
71    punt to gettimeofday. */
72 int
73 FT_GetTimeOfDay(struct timeval *tv, struct timezone *tz)
74 {
75     int ret;
76     ret = gettimeofday(tv, tz);
77     if (!ret) {
78         /* need to bounds check 'cause Unix can fail these checks, (esp on Suns)
79          * and time package can generate invalid (to select syscall) values
80          * for the time until the next interesting event if it encounters
81          * out of range microsecond fields */
82         if (tv->tv_usec < 0)
83             tv->tv_usec = 0;
84         if (tv->tv_usec > 999999)
85             tv->tv_usec = 999999;
86         FT_LastTime.tv_sec = tv->tv_sec;
87         FT_LastTime.tv_usec = tv->tv_usec;
88     }
89     return ret;
90 }
91
92
93 /* For compatibility.  Should go away. */
94 int
95 TM_GetTimeOfDay(struct timeval *tv, struct timezone *tz)
96 {
97     return FT_GetTimeOfDay(tv, tz);
98 }
99
100 int
101 FT_AGetTimeOfDay(struct timeval *tv, struct timezone *tz)
102 {
103     if (FT_LastTime.tv_sec) {
104         tv->tv_sec = FT_LastTime.tv_sec;
105         tv->tv_usec = FT_LastTime.tv_usec;
106         return 0;
107     }
108     return FT_GetTimeOfDay(tv, tz);
109 }
110
111 #ifdef AFS_PTHREAD_ENV
112 unsigned int FT_ApproxTime(void)
113 {
114     return time(0);
115 }
116 #else
117 unsigned int
118 FT_ApproxTime(void)
119 {
120     if (!FT_LastTime.tv_sec) {
121         FT_GetTimeOfDay(&FT_LastTime, 0);
122     }
123     return FT_LastTime.tv_sec;
124 }
125 #endif