Hello, Attached are four patches for libgomp.
The first patch is not specific to any target. The pthread handle that is stored by `pthread_create()` is read in the thread routine, but the new thread may begin execution before `pthread_create()` stores the handle, so this is a race condition and has to be fixed.
The second patch attempts to increase precision of `omp_get_wtime()` to 10**-7 second by using modern APIs on Windows 8 and above.
The third patch defines `SONAME_SUFFIX` according to the naming convention of DLLs on Windows. The fourth patch implements `cpu_relax()`, and copies `doacross_spin()` from the posix header. Your feedback will be appreciated. From f641adac6d35f241118fc715ffc40b2b82146a60 Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Tue, 30 Jun 2026 10:17:12 +0800 Subject: [PATCH 9201/9204] libgomp/team: Fix a race condition `gomp_thread_start_data::handle` is set by `pthread_create()`. If the thread starts execution before `pthread_create()` stores the handle, it may be left uninitialized. The fix is to obtain the handle from `pthread_self()`. libgomp/ChangeLog: * team.c (gomp_thread_start): Get reflexive handle with `pthread_self()`. Signed-off-by: LIU Hao <[email protected]> --- libgomp/team.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libgomp/team.c b/libgomp/team.c index 5282a3133ba9..08c28a5b8765 100644 --- a/libgomp/team.c +++ b/libgomp/team.c @@ -93,7 +93,7 @@ gomp_thread_start (void *xdata) thr->num_teams = data->num_teams; thr->team_num = data->team_num; #ifdef GOMP_NEEDS_THREAD_HANDLE - thr->handle = data->handle; + thr->handle = pthread_self (); #endif #if !(defined HAVE_TLS || defined USE_EMUTLS) pthread_setspecific (gomp_tls_key, thr); -- 2.54.0 From b270cbbc6347178ed311423fc6140b4191e9bcf5 Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Sat, 27 Jun 2026 20:20:44 +0800 Subject: [PATCH 9202/9204] libgomp/mingw32: Increase precision of `omp_get_wtime()` for Windows 8+ This commit uses an ifunc pattern to try to obtain wall-clock time with `GetSystemTimePreciseAsFileTime()`, and when it is not available, falls back to `GetSystemTimeAsFileTime()`. Both of these functions have a resolution of 100 nanoseconds. libgomp/ChangeLog: * config/mingw32/time.c (typeof_GetSystemTimeAsFileTime): Declare. (do_GetSystemTimeAsFileTime): Define. (do_resolve_GetSystemTimeAsFileTime): Define ifunc resolver. (omp_get_wtime): Replace `_ftime` with `do_GetSystemTimeAsFileTime`. (omp_get_wtick): Increase precision from 1 ms to 100 ns, Signed-off-by: LIU Hao <[email protected]> --- libgomp/config/mingw32/time.c | 39 +++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/libgomp/config/mingw32/time.c b/libgomp/config/mingw32/time.c index cd83c1ee09d1..1de4c537a532 100644 --- a/libgomp/config/mingw32/time.c +++ b/libgomp/config/mingw32/time.c @@ -1,5 +1,6 @@ /* Copyright (C) 2006-2026 Free Software Foundation, Inc. Contributed by Francois-Xavier Coudert <[email protected]> + Contributed by LIU Hao <[email protected]> This file is part of the GNU Offloading and Multi Processing Library (libgomp). @@ -26,21 +27,47 @@ /* This file contains timer routines for mingw32. */ #include "libgomp.h" -#include <unistd.h> -#include <sys/timeb.h> +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +typedef void WINAPI typeof_GetSystemTimeAsFileTime (LPFILETIME); +static typeof_GetSystemTimeAsFileTime *do_GetSystemTimeAsFileTime; + +static void WINAPI +do_resolve_GetSystemTimeAsFileTime (LPFILETIME outp) +{ + typeof_GetSystemTimeAsFileTime *fn = NULL; + + /* Use `GetSystemTimePreciseAsFileTime` for Windows 8+, and use + `GetSystemTimeAsFileTime` as a fallback. These two functions have the + same signature. */ + HMODULE kernel32 = GetModuleHandleW (L"kernel32.dll"); + if (kernel32) + fn = (typeof_GetSystemTimeAsFileTime *)(INT_PTR) + GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime"); + + if (!fn) + fn = GetSystemTimeAsFileTime; + + __atomic_store_n (&do_GetSystemTimeAsFileTime, fn, MEMMODEL_RELAXED); + return (*fn) (outp); +} + +static typeof_GetSystemTimeAsFileTime *do_GetSystemTimeAsFileTime + = do_resolve_GetSystemTimeAsFileTime; double omp_get_wtime (void) { - struct _timeb timebuf; - _ftime (&timebuf); - return (timebuf.time + (long)(timebuf.millitm) / 1e3); + ULARGE_INTEGER uli; + (*do_GetSystemTimeAsFileTime) ((FILETIME *) &uli); + return (double)(INT64) uli.QuadPart * 1.0e-7; } double omp_get_wtick (void) { - return 1e-3; + return 1.0e-7; } ialias (omp_get_wtime) -- 2.54.0 From ff292d1a7cca82c85bd56f2a50ff7087c75ce6af Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Sat, 27 Jun 2026 20:47:25 +0800 Subject: [PATCH 9203/9204] libgomp/mingw32: Fix plugin suffix On Windows a shared library is named like `lib<name>-<major>.dll`. libgomp/ChangeLog: * config/mingw32/plugin-suffix.h: New file. Signed-off-by: LIU Hao <[email protected]> --- libgomp/config/mingw32/plugin-suffix.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 libgomp/config/mingw32/plugin-suffix.h diff --git a/libgomp/config/mingw32/plugin-suffix.h b/libgomp/config/mingw32/plugin-suffix.h new file mode 100644 index 000000000000..9425a3951c88 --- /dev/null +++ b/libgomp/config/mingw32/plugin-suffix.h @@ -0,0 +1,26 @@ +/* Copyright (C) 2015-2026 Free Software Foundation, Inc. + Contributed by LIU Hao <[email protected]> + + This file is part of the GNU Offloading and Multi Processing Library + (libgomp). + + Libgomp is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for + more details. + + Under Section 7 of GPL version 3, you are granted additional + permissions described in the GCC Runtime Library Exception, version + 3.1, as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License and + a copy of the GCC Runtime Library Exception along with this program; + see the files COPYING3 and COPYING.RUNTIME respectively. If not, see + <http://www.gnu.org/licenses/>. */ + +#define SONAME_SUFFIX(n) ("-" #n ".dll") -- 2.54.0 From 74009540420870dffb243a4ffbc95d094e537df5 Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Sat, 27 Jun 2026 22:24:36 +0800 Subject: [PATCH 9204/9204] libgomp/mingw32: Implement `cpu_relax()` and `doacross_spin()` for Windows `cpu_relax()` uses the macro `YieldProcessor()` which is available on all architectures. It expands to PAUSE for x86-family processors, and to DMB ISHST for ARM-family processors. `doacross_spin()` is the same as the pthread one. libgomp/ChangeLog: * config/mingw32/doacross.h: New file. Signed-off-by: LIU Hao <[email protected]> --- libgomp/config/mingw32/doacross.h | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 libgomp/config/mingw32/doacross.h diff --git a/libgomp/config/mingw32/doacross.h b/libgomp/config/mingw32/doacross.h new file mode 100644 index 000000000000..601afa5a08f0 --- /dev/null +++ b/libgomp/config/mingw32/doacross.h @@ -0,0 +1,55 @@ +/* Copyright (C) 2015-2026 Free Software Foundation, Inc. + Contributed by LIU Hao <[email protected]> + + This file is part of the GNU Offloading and Multi Processing Library + (libgomp). + + Libgomp is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for + more details. + + Under Section 7 of GPL version 3, you are granted additional + permissions described in the GCC Runtime Library Exception, version + 3.1, as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License and + a copy of the GCC Runtime Library Exception along with this program; + see the files COPYING3 and COPYING.RUNTIME respectively. If not, see + <http://www.gnu.org/licenses/>. */ + +/* This is the Windows implementation of doacross spinning. */ + +#ifndef GOMP_DOACROSS_H +#define GOMP_DOACROSS_H 1 + +#include "libgomp.h" +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +static inline void +cpu_relax (void) +{ + YieldProcessor (); +} + +static inline void doacross_spin (unsigned long *addr, unsigned long expected, + unsigned long cur) +{ + /* FIXME: back off depending on how large expected - cur is. */ + do + { + cpu_relax (); + cur = __atomic_load_n (addr, MEMMODEL_RELAXED); + if (expected < cur) + return; + } + while (1); +} + +#endif /* GOMP_DOACROSS_H */ -- 2.54.0
From f641adac6d35f241118fc715ffc40b2b82146a60 Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Tue, 30 Jun 2026 10:17:12 +0800 Subject: [PATCH 9201/9204] libgomp/team: Fix a race condition `gomp_thread_start_data::handle` is set by `pthread_create()`. If the thread starts execution before `pthread_create()` stores the handle, it may be left uninitialized. The fix is to obtain the handle from `pthread_self()`. libgomp/ChangeLog: * team.c (gomp_thread_start): Get reflexive handle with `pthread_self()`. Signed-off-by: LIU Hao <[email protected]> --- libgomp/team.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libgomp/team.c b/libgomp/team.c index 5282a3133ba9..08c28a5b8765 100644 --- a/libgomp/team.c +++ b/libgomp/team.c @@ -93,7 +93,7 @@ gomp_thread_start (void *xdata) thr->num_teams = data->num_teams; thr->team_num = data->team_num; #ifdef GOMP_NEEDS_THREAD_HANDLE - thr->handle = data->handle; + thr->handle = pthread_self (); #endif #if !(defined HAVE_TLS || defined USE_EMUTLS) pthread_setspecific (gomp_tls_key, thr); -- 2.54.0
From b270cbbc6347178ed311423fc6140b4191e9bcf5 Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Sat, 27 Jun 2026 20:20:44 +0800 Subject: [PATCH 9202/9204] libgomp/mingw32: Increase precision of `omp_get_wtime()` for Windows 8+ This commit uses an ifunc pattern to try to obtain wall-clock time with `GetSystemTimePreciseAsFileTime()`, and when it is not available, falls back to `GetSystemTimeAsFileTime()`. Both of these functions have a resolution of 100 nanoseconds. libgomp/ChangeLog: * config/mingw32/time.c (typeof_GetSystemTimeAsFileTime): Declare. (do_GetSystemTimeAsFileTime): Define. (do_resolve_GetSystemTimeAsFileTime): Define ifunc resolver. (omp_get_wtime): Replace `_ftime` with `do_GetSystemTimeAsFileTime`. (omp_get_wtick): Increase precision from 1 ms to 100 ns, Signed-off-by: LIU Hao <[email protected]> --- libgomp/config/mingw32/time.c | 39 +++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/libgomp/config/mingw32/time.c b/libgomp/config/mingw32/time.c index cd83c1ee09d1..1de4c537a532 100644 --- a/libgomp/config/mingw32/time.c +++ b/libgomp/config/mingw32/time.c @@ -1,5 +1,6 @@ /* Copyright (C) 2006-2026 Free Software Foundation, Inc. Contributed by Francois-Xavier Coudert <[email protected]> + Contributed by LIU Hao <[email protected]> This file is part of the GNU Offloading and Multi Processing Library (libgomp). @@ -26,21 +27,47 @@ /* This file contains timer routines for mingw32. */ #include "libgomp.h" -#include <unistd.h> -#include <sys/timeb.h> +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +typedef void WINAPI typeof_GetSystemTimeAsFileTime (LPFILETIME); +static typeof_GetSystemTimeAsFileTime *do_GetSystemTimeAsFileTime; + +static void WINAPI +do_resolve_GetSystemTimeAsFileTime (LPFILETIME outp) +{ + typeof_GetSystemTimeAsFileTime *fn = NULL; + + /* Use `GetSystemTimePreciseAsFileTime` for Windows 8+, and use + `GetSystemTimeAsFileTime` as a fallback. These two functions have the + same signature. */ + HMODULE kernel32 = GetModuleHandleW (L"kernel32.dll"); + if (kernel32) + fn = (typeof_GetSystemTimeAsFileTime *)(INT_PTR) + GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime"); + + if (!fn) + fn = GetSystemTimeAsFileTime; + + __atomic_store_n (&do_GetSystemTimeAsFileTime, fn, MEMMODEL_RELAXED); + return (*fn) (outp); +} + +static typeof_GetSystemTimeAsFileTime *do_GetSystemTimeAsFileTime + = do_resolve_GetSystemTimeAsFileTime; double omp_get_wtime (void) { - struct _timeb timebuf; - _ftime (&timebuf); - return (timebuf.time + (long)(timebuf.millitm) / 1e3); + ULARGE_INTEGER uli; + (*do_GetSystemTimeAsFileTime) ((FILETIME *) &uli); + return (double)(INT64) uli.QuadPart * 1.0e-7; } double omp_get_wtick (void) { - return 1e-3; + return 1.0e-7; } ialias (omp_get_wtime) -- 2.54.0
From ff292d1a7cca82c85bd56f2a50ff7087c75ce6af Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Sat, 27 Jun 2026 20:47:25 +0800 Subject: [PATCH 9203/9204] libgomp/mingw32: Fix plugin suffix On Windows a shared library is named like `lib<name>-<major>.dll`. libgomp/ChangeLog: * config/mingw32/plugin-suffix.h: New file. Signed-off-by: LIU Hao <[email protected]> --- libgomp/config/mingw32/plugin-suffix.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 libgomp/config/mingw32/plugin-suffix.h diff --git a/libgomp/config/mingw32/plugin-suffix.h b/libgomp/config/mingw32/plugin-suffix.h new file mode 100644 index 000000000000..9425a3951c88 --- /dev/null +++ b/libgomp/config/mingw32/plugin-suffix.h @@ -0,0 +1,26 @@ +/* Copyright (C) 2015-2026 Free Software Foundation, Inc. + Contributed by LIU Hao <[email protected]> + + This file is part of the GNU Offloading and Multi Processing Library + (libgomp). + + Libgomp is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for + more details. + + Under Section 7 of GPL version 3, you are granted additional + permissions described in the GCC Runtime Library Exception, version + 3.1, as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License and + a copy of the GCC Runtime Library Exception along with this program; + see the files COPYING3 and COPYING.RUNTIME respectively. If not, see + <http://www.gnu.org/licenses/>. */ + +#define SONAME_SUFFIX(n) ("-" #n ".dll") -- 2.54.0
From 74009540420870dffb243a4ffbc95d094e537df5 Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Sat, 27 Jun 2026 22:24:36 +0800 Subject: [PATCH 9204/9204] libgomp/mingw32: Implement `cpu_relax()` and `doacross_spin()` for Windows `cpu_relax()` uses the macro `YieldProcessor()` which is available on all architectures. It expands to PAUSE for x86-family processors, and to DMB ISHST for ARM-family processors. `doacross_spin()` is the same as the pthread one. libgomp/ChangeLog: * config/mingw32/doacross.h: New file. Signed-off-by: LIU Hao <[email protected]> --- libgomp/config/mingw32/doacross.h | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 libgomp/config/mingw32/doacross.h diff --git a/libgomp/config/mingw32/doacross.h b/libgomp/config/mingw32/doacross.h new file mode 100644 index 000000000000..601afa5a08f0 --- /dev/null +++ b/libgomp/config/mingw32/doacross.h @@ -0,0 +1,55 @@ +/* Copyright (C) 2015-2026 Free Software Foundation, Inc. + Contributed by LIU Hao <[email protected]> + + This file is part of the GNU Offloading and Multi Processing Library + (libgomp). + + Libgomp is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for + more details. + + Under Section 7 of GPL version 3, you are granted additional + permissions described in the GCC Runtime Library Exception, version + 3.1, as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License and + a copy of the GCC Runtime Library Exception along with this program; + see the files COPYING3 and COPYING.RUNTIME respectively. If not, see + <http://www.gnu.org/licenses/>. */ + +/* This is the Windows implementation of doacross spinning. */ + +#ifndef GOMP_DOACROSS_H +#define GOMP_DOACROSS_H 1 + +#include "libgomp.h" +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +static inline void +cpu_relax (void) +{ + YieldProcessor (); +} + +static inline void doacross_spin (unsigned long *addr, unsigned long expected, + unsigned long cur) +{ + /* FIXME: back off depending on how large expected - cur is. */ + do + { + cpu_relax (); + cur = __atomic_load_n (addr, MEMMODEL_RELAXED); + if (expected < cur) + return; + } + while (1); +} + +#endif /* GOMP_DOACROSS_H */ -- 2.54.0
OpenPGP_signature.asc
Description: OpenPGP digital signature
