diff options
author | Ivan Maidanski <ivmai@mail.ru> | 2020-02-11 11:54:50 +0300 |
---|---|---|
committer | Ivan Maidanski <ivmai@mail.ru> | 2020-02-11 11:54:50 +0300 |
commit | 9d869bcd98bdd0a96ba86de6f26f80b9101cf135 (patch) | |
tree | 42bffdefe5190034ed34217754d106ad47c8a2cb /win32_threads.c | |
parent | 03a3fbbdd5cff52360facdfcc9f86adbdc111245 (diff) | |
download | bdwgc-9d869bcd98bdd0a96ba86de6f26f80b9101cf135.tar.gz |
Implement set_marker_thread_name on Windows 10
Issue #300 (bdwgc).
Call SetThreadDescription() if available.
* win32_threads.c [HAVE_PTHREAD_SETNAME_NP_WITH_TID]
(set_marker_thread_name): No-op unless GC_PTHREADS is defined.
* win32_threads.c [!(GC_PTHREADS && HAVE_PTHREAD_SETNAME_NP_WITH_TID)
&& !MSWINCE] (setThreadDescription_fn): New static variable.
* win32_threads.c [!(GC_PTHREADS && HAVE_PTHREAD_SETNAME_NP_WITH_TID)
&& !MSWINCE] (set_marker_thread_name): Compose thread name into
WCHAR buffer; call setThreadDescription_fn if non-null.
* win32_threads.c [(!HAVE_PTHREAD_SETNAME_NP_WITH_TID && !MSWINCE
&& PARALLEL_MARK) || WOW64_THREAD_CONTEXT_WORKAROUND] (GC_thr_init):
Declare and set hK32 local variable at the beginning of the function.
* win32_threads.c [PARALLEL_MARK && !HAVE_PTHREAD_SETNAME_NP_WITH_TID
&& !MSWINCE] (GC_thr_init): Set setThreadDescription_fn.
Diffstat (limited to 'win32_threads.c')
-rw-r--r-- | win32_threads.c | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/win32_threads.c b/win32_threads.c index 058b2864..a8ea02b1 100644 --- a/win32_threads.c +++ b/win32_threads.c @@ -2001,7 +2001,7 @@ GC_INNER void GC_get_next_stack(char *start, char *limit, /* only a few entries). */ # endif -# if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) +# if defined(GC_PTHREADS) && defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) static void set_marker_thread_name(unsigned id) { /* This code is the same as in pthread_support.c. */ @@ -2019,6 +2019,32 @@ GC_INNER void GC_get_next_stack(char *start, char *limit, if (pthread_setname_np(pthread_self(), name_buf) != 0) WARN("pthread_setname_np failed\n", 0); } + +# elif !defined(MSWINCE) + /* A pointer to SetThreadDescription() which is available since */ + /* Windows 10. The function prototype is in processthreadsapi.h. */ + static FARPROC setThreadDescription_fn; + + static void set_marker_thread_name(unsigned id) + { + WCHAR name_buf[16]; + int len = sizeof(L"GC-marker-") / sizeof(WCHAR) - 1; + HRESULT hr; + + if (!setThreadDescription_fn) return; /* missing SetThreadDescription */ + + /* Compose the name manually as swprintf may be unavailable. */ + BCOPY(L"GC-marker-", name_buf, len * sizeof(WCHAR)); + if (id >= 10) + name_buf[len++] = (WCHAR)('0' + (id / 10) % 10); + name_buf[len] = (WCHAR)('0' + id % 10); + name_buf[len + 1] = 0; + + hr = ((HRESULT (WINAPI *)(HANDLE, const WCHAR *)) + setThreadDescription_fn)(GetCurrentThread(), name_buf); + if (FAILED(hr)) + WARN("SetThreadDescription failed\n", 0); + } # else # define set_marker_thread_name(id) (void)(id) # endif @@ -2711,6 +2737,10 @@ GC_INNER void GC_get_next_stack(char *start, char *limit, GC_INNER void GC_thr_init(void) { struct GC_stack_base sb; +# if (!defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) && !defined(MSWINCE) \ + && defined(PARALLEL_MARK)) || defined(WOW64_THREAD_CONTEXT_WORKAROUND) + HMODULE hK32 = GetModuleHandle(TEXT("kernel32.dll")); +# endif GC_ASSERT(I_HOLD_LOCK()); GC_ASSERT(!GC_thr_initialized); @@ -2739,8 +2769,6 @@ GC_INNER void GC_thr_init(void) # ifdef WOW64_THREAD_CONTEXT_WORKAROUND /* Set isWow64 flag. */ - { - HMODULE hK32 = GetModuleHandle(TEXT("kernel32.dll")); if (hK32) { FARPROC pfn = GetProcAddress(hK32, "IsWow64Process"); if (pfn @@ -2748,7 +2776,6 @@ GC_INNER void GC_thr_init(void) &isWow64)) isWow64 = FALSE; /* IsWow64Process failed */ } - } # endif /* Add the initial thread, so we can stop it. */ @@ -2831,6 +2858,11 @@ GC_INNER void GC_thr_init(void) || mark_cv == (HANDLE)0) ABORT("CreateEvent failed"); # endif +# if !defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) && !defined(MSWINCE) + if (hK32) + setThreadDescription_fn = GetProcAddress(hK32, + "SetThreadDescription"); +# endif } # endif /* PARALLEL_MARK */ |