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