• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2    Copyright (c) 2011-2016  mingw-w64 project
3 
4    Permission is hereby granted, free of charge, to any person obtaining a
5    copy of this software and associated documentation files (the "Software"),
6    to deal in the Software without restriction, including without limitation
7    the rights to use, copy, modify, merge, publish, distribute, sublicense,
8    and/or sell copies of the Software, and to permit persons to whom the
9    Software is 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
20    DEALINGS IN THE SOFTWARE.
21 */
22 
23 #include "pthread.h"
24 #include "windows.h"
25 #include "misc.h"
26 
_pthread_time_in_ms(void)27 unsigned long long _pthread_time_in_ms(void)
28 {
29     FILETIME ft;
30 
31     GetSystemTimeAsFileTime(&ft);
32     return (((unsigned long long)ft.dwHighDateTime << 32) + ft.dwLowDateTime
33             - 0x19DB1DED53E8000ULL) / 10000ULL;
34 }
35 
_pthread_time_in_ms_from_timespec(const struct timespec * ts)36 unsigned long long _pthread_time_in_ms_from_timespec(const struct timespec *ts)
37 {
38     unsigned long long t = (unsigned long long) ts->tv_sec * 1000LL;
39     /* The +999999 is here to ensure that the division always rounds up */
40     t += (unsigned long long) (ts->tv_nsec + 999999) / 1000000;
41 
42     return t;
43 }
44 
_pthread_rel_time_in_ms(const struct timespec * ts)45 unsigned long long _pthread_rel_time_in_ms(const struct timespec *ts)
46 {
47     unsigned long long t1 = _pthread_time_in_ms_from_timespec(ts);
48     unsigned long long t2 = _pthread_time_in_ms();
49 
50     /* Prevent underflow */
51     if (t1 < t2) return 0;
52     return t1 - t2;
53 }
54 
55 static unsigned long long
_pthread_get_tick_count(long long * frequency)56 _pthread_get_tick_count (long long *frequency)
57 {
58 #if defined (_WIN32_WINNT) && (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
59   (void) frequency; /* unused */
60   return GetTickCount64 ();
61 #else
62   LARGE_INTEGER freq, timestamp;
63 
64   if (*frequency == 0)
65   {
66     if (QueryPerformanceFrequency (&freq))
67       *frequency = freq.QuadPart;
68     else
69       *frequency = -1;
70   }
71 
72   if (*frequency > 0 && QueryPerformanceCounter (&timestamp))
73     return timestamp.QuadPart / (*frequency / 1000);
74 
75   /* Fallback */
76   return GetTickCount ();
77 #endif
78 }
79 
80 /* A wrapper around WaitForSingleObject() that ensures that
81  * the wait function does not time out before the time
82  * actually runs out. This is needed because WaitForSingleObject()
83  * might have poor accuracy, returning earlier than expected.
84  * On the other hand, returning a bit *later* than expected
85  * is acceptable in a preemptive multitasking environment.
86  */
87 unsigned long
_pthread_wait_for_single_object(void * handle,unsigned long timeout)88 _pthread_wait_for_single_object (void *handle, unsigned long timeout)
89 {
90   DWORD result;
91   unsigned long long start_time, end_time;
92   unsigned long wait_time;
93   long long frequency = 0;
94 
95   if (timeout == INFINITE || timeout == 0)
96     return WaitForSingleObject ((HANDLE) handle, (DWORD) timeout);
97 
98   start_time = _pthread_get_tick_count (&frequency);
99   end_time = start_time + timeout;
100   wait_time = timeout;
101 
102   do
103   {
104     unsigned long long current_time;
105 
106     result = WaitForSingleObject ((HANDLE) handle, (DWORD) wait_time);
107     if (result != WAIT_TIMEOUT)
108       break;
109 
110     current_time = _pthread_get_tick_count (&frequency);
111     if (current_time >= end_time)
112       break;
113 
114     wait_time = (DWORD) (end_time - current_time);
115   } while (TRUE);
116 
117   return result;
118 }
119 
120 /* A wrapper around WaitForMultipleObjects() that ensures that
121  * the wait function does not time out before the time
122  * actually runs out. This is needed because WaitForMultipleObjects()
123  * might have poor accuracy, returning earlier than expected.
124  * On the other hand, returning a bit *later* than expected
125  * is acceptable in a preemptive multitasking environment.
126  */
127 unsigned long
_pthread_wait_for_multiple_objects(unsigned long count,void ** handles,unsigned int all,unsigned long timeout)128 _pthread_wait_for_multiple_objects (unsigned long count, void **handles, unsigned int all, unsigned long timeout)
129 {
130   DWORD result;
131   unsigned long long start_time, end_time;
132   unsigned long wait_time;
133   long long frequency = 0;
134 
135   if (timeout == INFINITE || timeout == 0)
136     return WaitForMultipleObjects ((DWORD) count, (HANDLE *) handles, all, (DWORD) timeout);
137 
138   start_time = _pthread_get_tick_count (&frequency);
139   end_time = start_time + timeout;
140   wait_time = timeout;
141 
142   do
143   {
144     unsigned long long current_time;
145 
146     result = WaitForMultipleObjects ((DWORD) count, (HANDLE *) handles, all, (DWORD) wait_time);
147     if (result != WAIT_TIMEOUT)
148       break;
149 
150     current_time = _pthread_get_tick_count (&frequency);
151     if (current_time >= end_time)
152       break;
153 
154     wait_time = (DWORD) (end_time - current_time);
155   } while (TRUE);
156 
157   return result;
158 }
159