All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
target/Darwin-i386/include/citrusleaf/cf_clock_win.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2008-2013 by Aerospike.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  *****************************************************************************/
22 #pragma once
23 
24 #include <WinSock2.h> // for struct timeval
25 #include <pthread.h> // for struct timespec
26 
27 #define CLOCK_REALTIME 0
28 #define CLOCK_MONOTONIC 1
29 #define CLOCK_PROCESS_CPUTIME_ID 2
30 
31 
32 inline static LARGE_INTEGER getFILETIMEoffset()
33 {
34  SYSTEMTIME s;
35  FILETIME f;
36  LARGE_INTEGER t;
37 
38  s.wYear = 1970;
39  s.wMonth = 1;
40  s.wDay = 1;
41  s.wHour = 0;
42  s.wMinute = 0;
43  s.wSecond = 0;
44  s.wMilliseconds = 0;
45  SystemTimeToFileTime(&s, &f);
46  t.QuadPart = f.dwHighDateTime;
47  t.QuadPart <<= 32;
48  t.QuadPart |= f.dwLowDateTime;
49 
50  return (t);
51 }
52 
53 static inline int clock_gettime(int clock_type, struct timespec* p_ts)
54 {
55  LARGE_INTEGER t;
56  FILETIME f;
57  double nanoseconds;
58  static LARGE_INTEGER offset;
59  static double frequencyToNanoseconds;
60  static int initialized = 0;
61  static BOOL usePerformanceCounter = 0;
62 
63  if (!initialized) {
64  LARGE_INTEGER performanceFrequency;
65  initialized = 1;
66  usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
67  if (usePerformanceCounter) {
68  QueryPerformanceCounter(&offset);
69  frequencyToNanoseconds = (double)performanceFrequency.QuadPart / 1000000000.;
70  } else {
71  offset = getFILETIMEoffset();
72  frequencyToNanoseconds = 10000.;
73  }
74  }
75 
76  if (usePerformanceCounter) {
77  QueryPerformanceCounter(&t);
78  }
79  else {
80  GetSystemTimeAsFileTime(&f);
81  t.QuadPart = f.dwHighDateTime;
82  t.QuadPart <<= 32;
83  t.QuadPart |= f.dwLowDateTime;
84  }
85 
86  t.QuadPart -= offset.QuadPart;
87  nanoseconds = (double)t.QuadPart / frequencyToNanoseconds;
88  t.QuadPart = (LONGLONG)nanoseconds;
89  p_ts->tv_sec = t.QuadPart / 1000000000;
90  p_ts->tv_nsec = t.QuadPart % 1000000000;
91 
92  return 0;
93 }
static LARGE_INTEGER getFILETIMEoffset()
static int clock_gettime(int clock_type, struct timespec *p_ts)
cl_msg_field f