win32/osal.c
Go to the documentation of this file.
1 /*
2  * Licensed under the GNU General Public License version 2 with exceptions. See
3  * LICENSE file in the project root for full license information
4  */
5 
6 #include <winsock2.h>
7 #include <osal.h>
8 #include "osal_win32.h"
9 
11 static double qpc2usec;
12 
13 #define USECS_PER_SEC 1000000
14 
15 int osal_getrelativetime(struct timeval *tv, struct timezone *tz)
16 {
17  int64_t wintime, usecs;
18  if(!sysfrequency)
19  {
20  timeBeginPeriod(1);
21  QueryPerformanceFrequency((LARGE_INTEGER *)&sysfrequency);
22  qpc2usec = 1000000.0 / sysfrequency;
23  }
24  QueryPerformanceCounter((LARGE_INTEGER *)&wintime);
25  usecs = (int64_t)((double)wintime * qpc2usec);
26  tv->tv_sec = (long)(usecs / 1000000);
27  tv->tv_usec = (long)(usecs - (tv->tv_sec * 1000000));
28 
29  return 1;
30 }
31 
32 int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
33 {
34  FILETIME system_time;
35  int64 system_time64, usecs;
36 
37  /* The offset variable is required to switch from Windows epoch (January 1, 1601) to
38  * Unix epoch (January 1, 1970). Number of days between both epochs: 134.774
39  *
40  * The time returned by GetSystemTimeAsFileTime() changes in 100 ns steps, so the
41  * following factors are required for the conversion from days to 100 ns steps:
42  *
43  * 86.400 seconds per day; 1.000.000 microseconds per second; 10 * 100 ns per microsecond
44  */
45  int64 offset = -134774LL * 86400LL * 1000000LL * 10LL;
46 
47  GetSystemTimeAsFileTime(&system_time);
48 
49  system_time64 = ((int64)(system_time.dwHighDateTime) << 32) + (int64)system_time.dwLowDateTime;
50  system_time64 += offset;
51  usecs = system_time64 / 10;
52 
53  tv->tv_sec = (long)(usecs / 1000000);
54  tv->tv_usec = (long)(usecs - (tv->tv_sec * 1000000));
55 
56  return 1;
57 }
58 
60 {
61  struct timeval current_time;
62  ec_timet return_value;
63 
64  osal_gettimeofday (&current_time, 0);
65  return_value.sec = current_time.tv_sec;
66  return_value.usec = current_time.tv_usec;
67  return return_value;
68 }
69 
70 void osal_time_diff(ec_timet *start, ec_timet *end, ec_timet *diff)
71 {
72  if (end->usec < start->usec) {
73  diff->sec = end->sec - start->sec - 1;
74  diff->usec = end->usec + 1000000 - start->usec;
75  }
76  else {
77  diff->sec = end->sec - start->sec;
78  diff->usec = end->usec - start->usec;
79  }
80 }
81 
82 void osal_timer_start (osal_timert *self, uint32 timeout_usec)
83 {
84  struct timeval start_time;
85  struct timeval timeout;
86  struct timeval stop_time;
87 
88  osal_getrelativetime (&start_time, 0);
89  timeout.tv_sec = timeout_usec / USECS_PER_SEC;
90  timeout.tv_usec = timeout_usec % USECS_PER_SEC;
91  timeradd (&start_time, &timeout, &stop_time);
92 
93  self->stop_time.sec = stop_time.tv_sec;
94  self->stop_time.usec = stop_time.tv_usec;
95 }
96 
98 {
99  struct timeval current_time;
100  struct timeval stop_time;
101  int is_not_yet_expired;
102 
103  osal_getrelativetime (&current_time, 0);
104  stop_time.tv_sec = self->stop_time.sec;
105  stop_time.tv_usec = self->stop_time.usec;
106  is_not_yet_expired = timercmp (&current_time, &stop_time, <);
107 
108  return is_not_yet_expired == FALSE;
109 }
110 
112 {
113  osal_timert qtime;
114  osal_timer_start(&qtime, usec);
115  if(usec >= 1000)
116  {
117  SleepEx(usec / 1000, FALSE);
118  }
119  while(!osal_timer_is_expired(&qtime));
120  return 1;
121 }
122 
123 void *osal_malloc(size_t size)
124 {
125  return malloc(size);
126 }
127 
128 void osal_free(void *ptr)
129 {
130  free(ptr);
131 }
132 
133 int osal_thread_create(void **thandle, int stacksize, void *func, void *param)
134 {
135  *thandle = CreateThread(NULL, stacksize, func, param, 0, NULL);
136  if(!thandle)
137  {
138  return 0;
139  }
140  return 1;
141 }
142 
143 int osal_thread_create_rt(void **thandle, int stacksize, void *func, void *param)
144 {
145  int ret;
146  ret = osal_thread_create(thandle, stacksize, func, param);
147  if (ret)
148  {
149  ret = SetThreadPriority(*thandle, THREAD_PRIORITY_TIME_CRITICAL);
150  }
151  return ret;
152 }
ec_timet osal_current_time(void)
Definition: win32/osal.c:59
int osal_usleep(uint32 usec)
Definition: win32/osal.c:111
uint32 usec
Definition: osal.h:39
void osal_free(void *ptr)
Definition: win32/osal.c:128
boolean osal_timer_is_expired(osal_timert *self)
Definition: win32/osal.c:97
int osal_getrelativetime(struct timeval *tv, struct timezone *tz)
Definition: win32/osal.c:15
Definition: osal.h:36
int64_t int64
Definition: osal.h:31
#define FALSE
Definition: osal.h:22
int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
Definition: win32/osal.c:32
#define timercmp(a, b, CMP)
Definition: rtk/osal.c:12
#define USECS_PER_SEC
Definition: win32/osal.c:13
#define timeradd(a, b, result)
Definition: rtk/osal.c:16
void osal_time_diff(ec_timet *start, ec_timet *end, ec_timet *diff)
Definition: win32/osal.c:70
void osal_timer_start(osal_timert *self, uint32 timeout_usec)
Definition: win32/osal.c:82
static int64_t sysfrequency
Definition: win32/osal.c:10
signed __int64 int64_t
Definition: stdint.h:89
void * osal_malloc(size_t size)
Definition: win32/osal.c:123
uint32 sec
Definition: osal.h:38
uint32_t uint32
Definition: osal.h:30
int osal_thread_create(void **thandle, int stacksize, void *func, void *param)
Definition: win32/osal.c:133
static double qpc2usec
Definition: win32/osal.c:11
int osal_thread_create_rt(void **thandle, int stacksize, void *func, void *param)
Definition: win32/osal.c:143


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Mon Feb 28 2022 23:46:57