5 * This translation unit implements miscellaneous thread functions.
7 * --------------------------------------------------------------------------
9 * Pthreads-win32 - POSIX Threads Library for Win32
10 * Copyright(C) 1998 John E. Bossom
11 * Copyright(C) 1999,2005 Pthreads-win32 contributors
13 * Contact Email: rpj@callisto.canberra.edu.au
15 * The current list of contributors is contained
16 * in the file CONTRIBUTORS included with the source
17 * code distribution. The list can also be seen at the
18 * following World Wide Web location:
19 * http://sources.redhat.com/pthreads-win32/contributors.html
21 * This library is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2 of the License, or (at your option) any later version.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library in the file COPYING.LIB;
33 * if not, write to the Free Software Foundation, Inc.,
34 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
38 #include "implement.h"
39 #if !defined(NEED_FTIME)
40 #include <sys/timeb.h>
44 #if defined(PTW32_BUILD_INLINED)
46 #endif /* PTW32_BUILD_INLINED */
48 ptw32_relmillisecs (const struct timespec
* abstime
)
50 const int64_t NANOSEC_PER_MILLISEC
= 1000000;
51 const int64_t MILLISEC_PER_SEC
= 1000;
53 int64_t tmpAbsMilliseconds
;
54 int64_t tmpCurrMilliseconds
;
55 #if defined(NEED_FTIME)
56 struct timespec currSysTime
;
59 #else /* ! NEED_FTIME */
60 #if ( defined(_MSC_VER) && _MSC_VER >= 1300 ) || \
61 ( (defined(__MINGW64__) || defined(__MINGW32__)) && __MSVCRT_VERSION__ >= 0x0601 )
62 struct __timeb64 currSysTime
;
64 struct _timeb currSysTime
;
66 #endif /* NEED_FTIME */
70 * Calculate timeout as milliseconds from current system time.
74 * subtract current system time from abstime in a way that checks
75 * that abstime is never in the past, or is never equivalent to the
76 * defined INFINITE value (0xFFFFFFFF).
78 * Assume all integers are unsigned, i.e. cannot test if less than 0.
80 tmpAbsMilliseconds
= (int64_t)abstime
->tv_sec
* MILLISEC_PER_SEC
;
81 tmpAbsMilliseconds
+= ((int64_t)abstime
->tv_nsec
+ (NANOSEC_PER_MILLISEC
/2)) / NANOSEC_PER_MILLISEC
;
83 /* get current system time */
85 #if defined(NEED_FTIME)
88 SystemTimeToFileTime(&st
, &ft
);
90 * GetSystemTimeAsFileTime(&ft); would be faster,
91 * but it does not exist on WinCE
94 ptw32_filetime_to_timespec(&ft
, &currSysTime
);
96 tmpCurrMilliseconds
= (int64_t)currSysTime
.tv_sec
* MILLISEC_PER_SEC
;
97 tmpCurrMilliseconds
+= ((int64_t)currSysTime
.tv_nsec
+ (NANOSEC_PER_MILLISEC
/2))
98 / NANOSEC_PER_MILLISEC
;
100 #else /* ! NEED_FTIME */
102 #if defined(_MSC_VER) && _MSC_VER >= 1400
103 _ftime64_s(&currSysTime
);
104 #elif ( defined(_MSC_VER) && _MSC_VER >= 1300 ) || \
105 ( (defined(__MINGW64__) || defined(__MINGW32__)) && __MSVCRT_VERSION__ >= 0x0601 )
106 _ftime64(&currSysTime
);
108 _ftime(&currSysTime
);
111 tmpCurrMilliseconds
= (int64_t) currSysTime
.time
* MILLISEC_PER_SEC
;
112 tmpCurrMilliseconds
+= (int64_t) currSysTime
.millitm
;
114 #endif /* NEED_FTIME */
116 if (tmpAbsMilliseconds
> tmpCurrMilliseconds
)
118 milliseconds
= (DWORD
) (tmpAbsMilliseconds
- tmpCurrMilliseconds
);
119 if (milliseconds
== INFINITE
)
121 /* Timeouts must be finite */
127 /* The abstime given is in the past */