ticket-2618-patches-20031207
[openafs.git] / src / rx / rx_clock_nt.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 #include <afsconfig.h>
14 #include <afs/param.h>
15
16 RCSID
17     ("$Header$");
18
19 #ifdef AFS_NT40_ENV
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include "rx_clock.h"
25
26 struct clock clock_now;         /* The last elapsed time ready by clock_GetTimer */
27
28 /* This is set to 1 whenever the time is read, and reset to 0 whenever
29  * clock_NewTime is called.  This is to allow the caller to control the
30  * frequency with which the actual time is re-evaluated.
31  */
32 int clock_haveCurrentTime;
33
34 int clock_nUpdates;             /* The actual number of clock updates */
35 static int clockInitialized = 0;
36
37 /* Timing tests show that we can compute times at about 4uS per call. */
38 LARGE_INTEGER rxi_clock0;
39 LARGE_INTEGER rxi_clockFreq;
40
41 #undef clock_UpdateTime
42 void clock_UpdateTime(void);
43
44 void
45 clock_Init(void)
46 {
47     if (!QueryPerformanceFrequency(&rxi_clockFreq)) {
48         printf("No High Performance clock, exiting.\n");
49         exit(1);
50     }
51
52     clockInitialized = 1;
53     (void)QueryPerformanceCounter(&rxi_clock0);
54
55     clock_UpdateTime();
56 }
57
58 #ifndef KERNEL
59 /* Make clock uninitialized. */
60 int
61 clock_UnInit(void)
62 {
63     clockInitialized = 0;
64         return 0;
65 }
66 #endif
67
68 void
69 clock_UpdateTime(void)
70 {
71     LARGE_INTEGER now, delta;
72     double seconds;
73
74     (void)QueryPerformanceCounter(&now);
75
76     delta.QuadPart = now.QuadPart - rxi_clock0.QuadPart;
77
78     seconds = (double)delta.QuadPart / (double)rxi_clockFreq.QuadPart;
79
80     clock_now.sec = (int)seconds;
81     clock_now.usec = (int)((seconds - (double)clock_now.sec)
82                            * (double)1000000);
83     clock_haveCurrentTime = 1;
84     clock_nUpdates++;
85
86 }
87 #endif /* AFS_NT40_ENV */