shithub: dav1d

Download patch

ref: 6c3e85de57884820ee5f2a4e5cd8ff7a2ee73b79
parent: b0d00020e06a3528977b977c61a252e91969b1a0
author: Henrik Gramner <gramner@twoorioles.com>
date: Mon Jul 29 11:12:35 EDT 2019

Set thread names on Windows 10

--- a/src/lib.c
+++ b/src/lib.c
@@ -50,6 +50,7 @@
     dav1d_init_wedge_masks();
     dav1d_init_interintra_masks();
     dav1d_init_qm_tables();
+    dav1d_init_thread();
 }
 
 COLD const char *dav1d_version(void) {
--- a/src/thread.h
+++ b/src/thread.h
@@ -48,6 +48,10 @@
 typedef CONDITION_VARIABLE pthread_cond_t;
 typedef INIT_ONCE pthread_once_t;
 
+void dav1d_init_thread(void);
+void dav1d_set_thread_name(const wchar_t *name);
+#define dav1d_set_thread_name(name) dav1d_set_thread_name(L##name)
+
 int dav1d_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*func)(void*), void *arg);
 int dav1d_pthread_join(pthread_t *thread, void **res);
@@ -126,7 +130,7 @@
 
 #include <pthread.h>
 
-#endif
+#define dav1d_init_thread() while (0)
 
 /* Thread naming support */
 
@@ -159,7 +163,9 @@
 
 #else
 
-#define dav1d_set_thread_name(name)
+#define dav1d_set_thread_name(name) while (0)
+
+#endif
 
 #endif
 
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -37,6 +37,20 @@
 
 #include "src/thread.h"
 
+static HRESULT (WINAPI *set_thread_description)(HANDLE, PCWSTR);
+
+COLD void dav1d_init_thread(void) {
+    set_thread_description =
+        (void*)GetProcAddress(GetModuleHandleW(L"kernel32.dll"),
+                              "SetThreadDescription");
+}
+
+#undef dav1d_set_thread_name
+COLD void dav1d_set_thread_name(const wchar_t *const name) {
+    if (set_thread_description) /* Only available since Windows 10 1607 */
+        set_thread_description(GetCurrentThread(), name);
+}
+
 static COLD unsigned __stdcall thread_entrypoint(void *const data) {
     pthread_t *const t = data;
     t->arg = t->func(t->arg);