darwin-head-build-fixes-20020821
[openafs.git] / src / rx / rx_clock.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 /* Elapsed time package */
11 /* See rx_clock.h for calling conventions */
12
13 #ifdef  KERNEL
14 #include "../afs/param.h"
15 #else
16 #include <afs/param.h>
17 #endif
18 #include <afsconfig.h>
19
20 #ifdef AFS_SUN59_ENV
21 #include <sys/time_impl.h>
22 #endif
23
24 RCSID("$Header$");
25
26 #ifdef KERNEL
27 #ifndef UKERNEL
28 #include "../rx/rx_clock.h"
29 #include "../h/types.h"
30 #include "../h/time.h"
31 #else /* !UKERNEL */
32 #include "../afs/sysincludes.h"
33 #include "../afs/afsincludes.h"
34 #include "../rx/rx.h"
35 #include "../rx/rx_clock.h"
36 #endif /* !UKERNEL */
37 #else /* KERNEL */
38 #include <sys/time.h>
39 #include <stdio.h>
40 #include <errno.h>
41 #include <stdlib.h>
42 #include "rx.h"
43 #include "rx_clock.h"
44 #endif
45
46 #if !defined(AFS_USE_GETTIMEOFDAY)
47 /*use this package only if gettimeofday is much much costlier than getitime */
48
49 #ifndef KERNEL
50
51 #if defined(AFS_GFS_ENV)
52 #define STARTVALUE 8000000      /* Ultrix bounds smaller, too small for general use */
53 #else
54 #define STARTVALUE 100000000    /* Max number of seconds setitimer allows, for some reason */
55 #endif
56
57 struct clock clock_now;         /* The last elapsed time ready by clock_GetTimer */
58
59 /* This is set to 1 whenever the time is read, and reset to 0 whenever clock_NewTime is called.  This is to allow the caller to control the frequency with which the actual time is re-evaluated (an expensive operation) */
60 int clock_haveCurrentTime;
61
62 int clock_nUpdates;             /* The actual number of clock updates */
63 static int clockInitialized = 0;
64
65 /* Initialize the clock */
66 void clock_Init(void)
67 {
68     struct itimerval itimer, otimer;
69
70     if (!clockInitialized) {
71         itimer.it_value.tv_sec = STARTVALUE;
72         itimer.it_value.tv_usec = 0;
73         itimer.it_interval.tv_sec = 0;
74         itimer.it_interval.tv_usec = 0;
75
76         if (setitimer(ITIMER_REAL, &itimer, &otimer) != 0) {
77             fprintf(stderr, "clock:  could not set interval timer; \
78                                 aborted(errno=%d)\n", errno);
79             fflush (stderr);
80             exit(1);
81         }
82         clockInitialized = 1;
83     }
84
85     clock_UpdateTime();
86 }
87
88 /* Make clock uninitialized. */
89 int clock_UnInit(void)
90 {
91     clockInitialized = 0;
92     return 0;
93
94
95 /* Compute the current time.  The timer gets the current total elapsed time since startup, expressed in seconds and microseconds.  This call is almost 200 usec on an APC RT */
96 void clock_UpdateTime(void)
97 {
98     struct itimerval itimer;
99     getitimer(ITIMER_REAL, &itimer);
100     clock_now.sec = STARTVALUE - 1 - itimer.it_value.tv_sec; /* The "-1" makes up for adding 1000000 usec, on the next line */
101     clock_now.usec = 1000000 - itimer.it_value.tv_usec;
102     if (clock_now.usec == 1000000) clock_now.usec = 0, clock_now.sec++;
103     clock_haveCurrentTime = 1;
104     clock_nUpdates++;
105 }
106 #else /* KERNEL */
107 #endif /* KERNEL */
108
109 #endif /* AFS_USE_GETTIMEOFDAY */