libtrbase  1.0.2
Web server and task management solution.
timer.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2014 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <stdarg.h>
24 
25 #include "trbase.h"
26 
27 #ifdef _WIN32
28 #include <Windows.h>
29 
30 static
31 LARGE_INTEGER
32 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  return (t);
50 }
51 
52 int
53 clock_gettime(int X, struct timespec *tv)
54 {
55  LARGE_INTEGER t;
56  FILETIME f;
57  double microseconds;
58  static LARGE_INTEGER offset;
59  static double frequencyToMicroseconds;
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  frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
70  } else {
71  offset = getFILETIMEoffset();
72  frequencyToMicroseconds = 10.;
73  }
74  }
75  if (usePerformanceCounter) QueryPerformanceCounter(&t);
76  else {
77  GetSystemTimeAsFileTime(&f);
78  t.QuadPart = f.dwHighDateTime;
79  t.QuadPart <<= 32;
80  t.QuadPart |= f.dwLowDateTime;
81  }
82 
83  t.QuadPart -= offset.QuadPart;
84  microseconds = (double)t.QuadPart / frequencyToMicroseconds;
85  t.QuadPart = microseconds;
86  tv->tv_sec = t.QuadPart / 1000000;
87  tv->tv_nsec = t.QuadPart % 1000000;
88  return (0);
89 }
90 #endif
91 
92 static
93 int
94 timerCtor(void * _this, va_list * params)
95 {
96  TR_eTimeoutBase base = va_arg(*params, TR_eTimeoutBase);
97  unsigned long timeout = va_arg(*params, unsigned long);
98 
99  TR_timerSet((TR_Timer)_this, base, timeout);
100 
101  return 0;
102 }
103 
104 static void timerDtor(void * _this) {}
105 
106 TR_INIT_IFACE(TR_Class, timerCtor, timerDtor, NULL);
107 TR_CREATE_CLASS(TR_Timer, NULL, NULL, TR_IF(TR_Class));
108 
109 // vim: set ts=4 sw=4:
#define TR_IF(name)
Definition: interface.h:49
static void timerDtor(void *_this)
Definition: timer.c:104
TR_CREATE_CLASS(TR_Timer, NULL, NULL, TR_IF(TR_Class))
static int timerCtor(void *_this, va_list *params)
Definition: timer.c:94
void TR_timerSet(TR_Timer, TR_eTimeoutBase, unsigned long)
Definition: timer_set.c:30
TR_eTimeoutBase
Definition: timer.h:42
TR_INIT_IFACE(TR_Class, timerCtor, timerDtor, NULL)