*** empty log message ***
[arla.git] / lwp / fasttime.c
blobc279f06eca02ad8a0df37b12509a53ad288457a6
1 /*
2 ****************************************************************************
3 * Copyright IBM Corporation 1988, 1989 - All Rights Reserved *
4 * *
5 * Permission to use, copy, modify, and distribute this software and its *
6 * documentation for any purpose and without fee is hereby granted, *
7 * provided that the above copyright notice appear in all copies and *
8 * that both that copyright notice and this permission notice appear in *
9 * supporting documentation, and that the name of IBM not be used in *
10 * advertising or publicity pertaining to distribution of the software *
11 * without specific, written prior permission. *
12 * *
13 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
15 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY *
16 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER *
17 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING *
18 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
19 ****************************************************************************
22 * fasttime.c -- Get the time of day quickly by mapping the kernel's
23 * time of day variable.
25 * David Nichols
26 * 6 January 1986
28 * Modification History
29 * 3/21/86: Added FT_ApproxTime which returns the last time
30 * in seconds returned by RT_FastTime. The intent is to give
31 * routines which aren't too concerned about the exact time
32 * fast access to the time, even on kernels without mmap.
33 * - Bob Sidebotham.
34 * 4/2/86: Fixed my previous mod and fixed FT_Init so it doesn't initialize
35 * a second time if explicitly called after being implicitly called.
36 * This saves a (precious) file descriptor.
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 RCSID("$Id$");
42 #endif
44 #include <stdio.h>
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/file.h>
48 #ifdef HAVE_SYS_MMAN_H
49 #include <sys/mman.h>
50 #endif
52 #ifdef HAVE_NLIST_H
53 #include <nlist.h>
54 #else
55 #ifdef HAVE_LIBELF_NLIST_H
56 #include <libelf/nlist.h>
57 #endif
58 #endif
60 #include "timer.h"
62 #define TRUE 1
63 #define FALSE 0
65 static enum InitState {
66 notTried, tried, done
67 } initState = notTried;
69 /* last time returned by RT_FastTime. Used to implement FT_ApproxTime */
70 struct timeval FT_LastTime;
74 * Call this to get the memory mapped. It will return -1 if anything went
75 * wrong. In that case, calls to FT_GetTimeOfDay will call gettimeofday
76 * instead. If printErrors is true, errors in initialization will cause
77 * error messages to be printed on stderr. If notReally is true, then
78 * things are set up so that all calls to FT_GetTimeOfDay call gettimeofday.
79 * You might want this if your program won't run too long and the nlist
80 * call is too expensive. Yeah, it's pretty horrible.
82 int
83 FT_Init(int printErrors, int notReally)
87 * This is in case explicit initialization occurs after automatic
88 * initialization
90 if (initState != notTried && !notReally)
91 return (initState == done ? 0 : -1);
93 initState = tried;
94 if (notReally)
95 return 0; /* fake success, but leave initState
96 * wrong. */
97 return (-1);
101 * Call this to get the time of day. It will automatically initialize the
102 * first time you call it. If you want error messages when you initialize,
103 * call FT_Init yourself. If the initialization failed, this will just
104 * call gettimeofday. If you ask for the timezone info, this routine will
105 * punt to gettimeofday.
108 FT_GetTimeOfDay(struct timeval * tv, struct timezone * tz)
110 int ret;
112 ret = gettimeofday(tv, tz);
113 if (!ret) {
116 * need to bounds check 'cause Unix can fail these checks, (esp on
117 * Suns) and time package can generate invalid (to select syscall)
118 * values for the time until the next interesting event if it
119 * encounters out of range microsecond fields
121 if (tv->tv_usec < 0)
122 tv->tv_usec = 0;
123 if (tv->tv_usec > 999999)
124 tv->tv_usec = 999999;
125 FT_LastTime.tv_sec = tv->tv_sec;
126 FT_LastTime.tv_usec = tv->tv_usec;
128 return ret;
132 /* For compatibility. Should go away. */
134 TM_GetTimeOfDay(struct timeval * tv, struct timezone * tz)
136 return FT_GetTimeOfDay(tv, tz);
140 FT_AGetTimeOfDay(struct timeval * tv, struct timezone * tz)
142 if (FT_LastTime.tv_sec) {
143 tv->tv_sec = FT_LastTime.tv_sec;
144 tv->tv_usec = FT_LastTime.tv_usec;
145 return 0;
147 return FT_GetTimeOfDay(tv, tz);
150 unsigned int
151 FT_ApproxTime(void)
153 if (!FT_LastTime.tv_sec) {
154 FT_GetTimeOfDay(&FT_LastTime, 0);
156 return FT_LastTime.tv_sec;