在 2026-10-17 19:19, Kirill Makurin 写道:
Subject: [PATCH 03/21] winpthreads: rename misc.c -> winpthreads.c

Likewise, rename misc.h -> winpthreads.h.

With this change, winpthreads.h becomes library's "main" include file;
ensure that each source file includes it directly or indirectly.

I don't quite agree with this change. The 'main' include file should be pthread.h; and besides, this isn't a public header that should be included indirectly, since it defines macros `likely`, `unlikely` and `UNREACHABLE` which are more likely to clash with user code (I've seem them several times).


Subject: [PATCH 07/21] winpthreads: winpthreads.c: new functions
 `winpthreads_set_thread_description_{ansi,unicode}`

These functions are portability wrappers for `SetThreadDescription`;
they call `SetThreadDescription` if it is available at runtime,
or a stub that simply fails otherwise.

Function `winpthreads_set_thread_description_unicode` simply calls
`SetThreadDescription`, while `winpthreads_set_thread_description_ansi`
converts its second argument to wide character string before calling
`SetThreadDescription`.

This changes `pthread_setname_np()` to use explicit ANSI encoding; the old implementation uses the CRT's locale.


Subject: [PATCH 12/21] winpthreads: time.c: improve support for
 `CLOCK_MONOTONIC`

winpthreads implemented `CLOCK_MONOTONIC` support using the performance counter
(`QueryPerformanceCounter`).

While this is an excellent choice (function `QueryPerformanceCounter`
is available on all Windows versions and provides <1us resolution),
it has a drawback that it may not be support on pre-XP systems due to
hardware limitations.

This would ultimately make `CLOCK_MONOTONIC` unusable on such systems.

In order to solve this issue, provide alternative implementation around
`GetTickCount*` functions; use `GetTickCount64` when available (>= Vista)
and `GetTickCount` otherwise.

Using `GetTickCount*` functions has a few drawbacks:

1. They use the same resolution as the system clock, which is 10~16ms,
  compared to <1us resolution of the performance counter.

2. They return number of milliseconds, which means that sub-millisecond
  part is lost; we workaround this by rounding up the returned value to
  the resolution of the system clock.

3. On pre-Vista systems, we use `GetTickCount` which wraps around to zero
  after ~49 days.

Signed-off-by: Kirill Makurin <[email protected]>
---
 mingw-w64-libraries/winpthreads/src/time.c | 168 +++++++++++++++++----
 1 file changed, 137 insertions(+), 31 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/time.c 
b/mingw-w64-libraries/winpthreads/src/time.c
index 522a34af7..8378803e5 100644
--- a/mingw-w64-libraries/winpthreads/src/time.c
+++ b/mingw-w64-libraries/winpthreads/src/time.c
@@ -468,16 +576,30 @@ unsigned winpthreads_wait_time_ms (const struct 
_timespec64 *ts)
unsigned __int64 winpthreads_windows_time_ms (void)
 {
-  if (WINPTHREADS_TIME_HAVE (HIGH_RESOLUTION_WINDOWS_TIME)) {
-    LARGE_INTEGER performanceFrequency = {.QuadPart = 
WinpthreadsTimeImpl.PerformanceCounter.Frequency};
-    LARGE_INTEGER performanceCounter   = {.QuadPart = 0};
+  struct _timespec64 ts;
- if (QueryPerformanceCounter (&performanceCounter)) {
-      return performanceCounter.QuadPart / (performanceFrequency.QuadPart / 
POW10_3);
-    }
+  WinpthreadsTimeImpl.TimeImplMonotonic (&ts, NULL);
+
+  unsigned __int64 msFromSec  = ts.tv_sec;
+  unsigned __int64 msFromNsec = ts.tv_nsec / POW10_6;
+
+  /**
+   * Check for overflow.
+   */
+  if (unlikely (msFromSec > (_UI64_MAX / POW10_3))) {
+    return _UI64_MAX;
   }
- return GetTickCount64 ();
+  msFromSec *= POW10_3;
+
+  /**
+   * Check for overflow.
+   */
+  if (unlikely (msFromNsec > (_UI64_MAX - msFromNsec))) {

                                             ^^^^^^^^^^

This should be `if (unlikely (msFromNsec > (_UI64_MAX - msFromSec))) {`


Subject: [PATCH 21/21] winphthreads: time.c: improve conversion logic in
 `clock_settime`

Function `clock_settime` is basically a wrapper around `SetSystemTime`;
it first converts `struct timespec` value to a `FILEITME` value,
and then to `SYSTEMTIME` value which is then passed to `SetSystemTime`.

Add new constant `FILETIME_TO_SYSTEMTIME_MAX`; this is the maximum
`FILEITME` vlaue that can be passed to `FileTimeToSystemTime`.

Ensure that intermediate `FILETIME` value does not exceed
`FILETIME_TO_SYSTEMTIME_MAX` and round that `FILETIME` value down to
the resolution of the system clock.

Signed-off-by: Kirill Makurin <[email protected]>
---
 mingw-w64-libraries/winpthreads/src/time.c | 76 +++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-libraries/winpthreads/src/time.c 
b/mingw-w64-libraries/winpthreads/src/time.c
index cb313ac40..a87d819cf 100644
--- a/mingw-w64-libraries/winpthreads/src/time.c
+++ b/mingw-w64-libraries/winpthreads/src/time.c
@@ -1152,9 +1163,65 @@ int clock_settime64 (clockid_t clock_id, const struct 
_timespec64 *tp)
       return -1;
   }
+ /**
+   * Obtain resolution of `clock_id` as a `FILETIME` value.
+   */
+  struct _timespec64 resAsTimeSpec;
+  unsigned __int64   resAsFileTime;
+
+  timeImpl (NULL, &resAsTimeSpec);
+  resAsFileTime = resAsTimeSpec.tv_sec * POW10_7 + resAsTimeSpec.tv_nsec / 100;

On a system which uses HPET as the high-resolution timer, `QueryPerformanceFrequency()` may return a value > 10 MHz, such as 14,318,180. `resAsTimeSpec` is the reciprocal of the frequency, and in this case it's < 100 ns, so `resAsFileTime` is zero. Later there's

  if ((fileTimeMax % resAsFileTime) != 0) {

so this is division by zero, and is undefined behavior.


I still haven't had time to read all of these patches; these are findings from 
AI.


--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to