On Saturday 17 January 2026 12:55:17 Pali Rohár wrote:
> On Friday 16 January 2026 10:36:17 LIU Hao wrote:
> > 在 2026-1-15 22:54, Martin Storsjö 写道:
> > > On Sat, 20 Dec 2025, Pali Rohár wrote:
> > > 
> > > > Per MS documentation [1] the SetThreadDescription function should be 
> > > > called
> > > > from the kernel32.dll library and not from the kernelbase.dll library.
> > > > 
> > > > [1] - 
> > > > https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-
> > > > setthreaddescription
> > > > ---
> > > > mingw-w64-libraries/winpthreads/src/misc.c | 10 +++-------
> > > > 1 file changed, 3 insertions(+), 7 deletions(-)
> > > 
> > > There may still be reasons for wanting to load this from kernelbase.dll
> > > - see https://code.ffmpeg.org/
> > > FFmpeg/FFmpeg/commit/1f4fed5cc3be0737305e342f753c42716d6bf432
> > > 
> > >       // Although SetThreadDescription lives in kernel32.dll, on Windows 
> > > Server 2016,
> > >       // Windows 10 LTSB 2016 and Windows 10 version 1607, it was only 
> > > available in
> > >       // kernelbase.dll. So, load it from there for maximum coverage.
> > 
> > Oh thanks for the information. I will revert this once the CI passes.
> 
> Thanks for information. I think that similar comment we can add also
> into winpthreads source code, so other people would not be surprise that
> it is really needed.

Hello, I would propose following attached change instead.
>From 910cb0d9b9a30c879079e263f96dc7ead416506a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 25 Jan 2026 12:22:31 +0100
Subject: [PATCH] winpthreads: Try to load SetThreadDescription from
 kernel32.dll and fallback to kernelbase.dll

The SetThreadDescription function should be called from the kernel32.dll library.
Document why the fallback to kernelbase.dll is needed for compatibility purposes.
---
 mingw-w64-libraries/winpthreads/src/misc.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/misc.c b/mingw-w64-libraries/winpthreads/src/misc.c
index d000dde4862a..b2d5cebc073a 100644
--- a/mingw-w64-libraries/winpthreads/src/misc.c
+++ b/mingw-w64-libraries/winpthreads/src/misc.c
@@ -55,6 +55,9 @@ static void winpthreads_init(void)
         _pthread_get_tick_count_64 =
             (ULONGLONG (WINAPI *)(VOID))(void*) GetProcAddress(mod, "GetTickCount64");
 
+        _pthread_set_thread_description =
+            (HRESULT (WINAPI *)(HANDLE, PCWSTR))(void*) GetProcAddress(mod, "SetThreadDescription");
+
         /* <1us precision on Windows 10 */
         _pthread_get_system_time_best_as_file_time =
             (void (WINAPI *)(LPFILETIME))(void*) GetProcAddress(mod, "GetSystemTimePreciseAsFileTime");
@@ -64,11 +67,18 @@ static void winpthreads_init(void)
         /* >15ms precision on Windows 10 */
         _pthread_get_system_time_best_as_file_time = GetSystemTimeAsFileTime;
 
-    mod = GetModuleHandleA("kernelbase.dll");
-    if (mod)
+    /* Although SetThreadDescription lives in kernel32.dll, on Windows Server 2016,
+     * Windows 10 LTSB 2016 and Windows 10 version 1607, it was only available in
+     * kernelbase.dll. So, load it from there for maximum coverage.
+     */
+    if (!_pthread_set_thread_description)
     {
-        _pthread_set_thread_description =
-            (HRESULT (WINAPI *)(HANDLE, PCWSTR))(void*) GetProcAddress(mod, "SetThreadDescription");
+        mod = GetModuleHandleA("kernelbase.dll");
+        if (mod)
+        {
+            _pthread_set_thread_description =
+                (HRESULT (WINAPI *)(HANDLE, PCWSTR))(void*) GetProcAddress(mod, "SetThreadDescription");
+        }
     }
 }
 #if defined(__GNUC__) && __GNUC__ >= 9 && !defined(__clang__)
-- 
2.20.1

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

Reply via email to