revert-dont-spawn-kernel-thread-for-rxk-listener-20020426
[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 RCSID("$Header$");
21
22 #ifdef KERNEL
23 #ifndef UKERNEL
24 #include "../rx/rx_clock.h"
25 #include "../h/types.h"
26 #include "../h/time.h"
27 #else /* !UKERNEL */
28 #include "../afs/sysincludes.h"
29 #include "../afs/afsincludes.h"
30 #include "../rx/rx_clock.h"
31 #endif /* !UKERNEL */
32 #else /* KERNEL */
33 #include <sys/time.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include "rx_clock.h"
38 #endif
39
40 #if !defined(AFS_USE_GETTIMEOFDAY)
41 /*use this package only if gettimeofday is much much costlier than getitime */
42
43 #ifndef KERNEL
44
45 #if defined(AFS_GFS_ENV)
46 #define STARTVALUE 8000000      /* Ultrix bounds smaller, too small for general use */
47 #else
48 #define STARTVALUE 100000000    /* Max number of seconds setitimer allows, for some reason */
49 #endif
50
51 struct clock clock_now;         /* The last elapsed time ready by clock_GetTimer */
52
53 /* 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) */
54 int clock_haveCurrentTime;
55
56 int clock_nUpdates;             /* The actual number of clock updates */
57 static int clockInitialized = 0;
58
59 /* Initialize the clock */
60 void clock_Init(void) {
61     struct itimerval itimer, otimer;
62
63     if (!clockInitialized) {
64         itimer.it_value.tv_sec = STARTVALUE;
65         itimer.it_value.tv_usec = 0;
66         itimer.it_interval.tv_sec = 0;
67         itimer.it_interval.tv_usec = 0;
68
69         if (setitimer(ITIMER_REAL, &itimer, &otimer) != 0) {
70             fprintf(stderr, "clock:  could not set interval timer; \
71                                 aborted(errno=%d)\n", errno);
72             fflush (stderr);
73             exit(1);
74         }
75         clockInitialized = 1;
76     }
77
78     clock_UpdateTime();
79 }
80
81 #ifndef KERNEL
82 /* Make clock uninitialized. */
83 int
84 clock_UnInit()
85 {
86     clockInitialized = 0;
87     return 0;
88
89 #endif 
90
91 /* 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 */
92 void clock_UpdateTime()
93 {
94     struct itimerval itimer;
95     getitimer(ITIMER_REAL, &itimer);
96     clock_now.sec = STARTVALUE - 1 - itimer.it_value.tv_sec; /* The "-1" makes up for adding 1000000 usec, on the next line */
97     clock_now.usec = 1000000 - itimer.it_value.tv_usec;
98     if (clock_now.usec == 1000000) clock_now.usec = 0, clock_now.sec++;
99     clock_haveCurrentTime = 1;
100     clock_nUpdates++;
101 }
102 #else /* KERNEL */
103 #endif /* KERNEL */
104
105 #endif /* AFS_USE_GETTIMEOFDAY */