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