[vlc-commits] thread: add a function to give names to threads
Steve Lhomme
git at videolan.org
Fri Nov 13 16:52:38 CET 2020
vlc | branch: master | Steve Lhomme <robux4 at ycbcr.xyz> | Mon Nov 9 14:23:53 2020 +0100| [5bc16beec1f3016cf69032700a6910d32707d628] | committer: Steve Lhomme
thread: add a function to give names to threads
Given the amount of threads used by VLC it can be useful to known which thread
is what when debugging or in a backtrace (especially if the stack is bogus, we
can get a hint at where the thread comes from).
This feature is supported in the Windows debugger when used on Windows 10 1607
and upper. There might be support in gdb on other platforms as well.
The posix variants comes from dav1d.
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5bc16beec1f3016cf69032700a6910d32707d628
---
include/vlc_threads.h | 10 ++++++++++
src/android/thread.c | 8 ++++++++
src/libvlccore.sym | 1 +
src/os2/thread.c | 5 +++++
src/posix/thread.c | 23 +++++++++++++++++++++++
src/win32/thread.c | 21 +++++++++++++++++++++
6 files changed, 68 insertions(+)
diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 21f4e46c27..46490334eb 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -750,6 +750,16 @@ void vlc_atomic_notify_all(void *addr);
VLC_API int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data,
int priority) VLC_USED;
+/**
+ * Set the thread name of the current thread.
+ *
+ * \param name the string to use as the thread name
+ *
+ * \note On Linux the name can be up to 16 bytes long, including the terminating
+ * null byte on Linux. If larger the name will be truncated.
+ */
+VLC_API void vlc_thread_set_name(const char *name);
+
/**
* Marks a thread as cancelled.
*
diff --git a/src/android/thread.c b/src/android/thread.c
index 60ddc9fae8..c15c82624e 100644
--- a/src/android/thread.c
+++ b/src/android/thread.c
@@ -164,6 +164,14 @@ int vlc_set_priority (vlc_thread_t th, int priority)
return VLC_SUCCESS;
}
+void vlc_thread_set_name(const char *name)
+{
+ char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
+ strncpy(buf, name, 15);
+ buf[15] = '\0';
+ pthread_setname_np(pthread_self(), buf);
+}
+
void vlc_cancel (vlc_thread_t thread_id)
{
atomic_uint *addr;
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index c4d5273dbd..8b149ae2b7 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -616,6 +616,7 @@ vlc_interrupt_register
vlc_interrupt_unregister
vlc_killed
vlc_join
+vlc_thread_set_name
vlc_list_children
vlc_meta_AddExtra
vlc_meta_CopyExtraNames
diff --git a/src/os2/thread.c b/src/os2/thread.c
index 822b3f3b26..cce326efc2 100644
--- a/src/os2/thread.c
+++ b/src/os2/thread.c
@@ -355,6 +355,11 @@ unsigned long vlc_thread_id (void)
return _gettid();
}
+void vlc_thread_set_name(const char *name)
+{
+ VLC_UNUSED(name);
+}
+
/*** Thread cancellation ***/
/* APC procedure for thread cancellation */
diff --git a/src/posix/thread.c b/src/posix/thread.c
index 91eab74716..930ba72e64 100644
--- a/src/posix/thread.c
+++ b/src/posix/thread.c
@@ -238,6 +238,29 @@ int vlc_set_priority (vlc_thread_t th, int priority)
return VLC_SUCCESS;
}
+#ifdef __linux__
+#include <sys/prctl.h>
+void vlc_thread_set_name(const char *name)
+{
+ prctl(PR_SET_NAME, name);
+}
+#elif defined(__APPLE__)
+void vlc_thread_set_name(const char *name)
+{
+ pthread_setname_np(name);
+}
+#elif defined(__NetBSD__)
+void vlc_thread_set_name(const char *name)
+{
+ pthread_setname_np(pthread_self(), "%s", (void*)name);
+}
+#else
+void vlc_thread_set_name(const char *name)
+{
+ VLC_UNUSED(name);
+}
+#endif
+
void vlc_cancel(vlc_thread_t th)
{
pthread_cancel(th.handle);
diff --git a/src/win32/thread.c b/src/win32/thread.c
index 60b4232e04..b2787a08ad 100644
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -31,6 +31,7 @@
#define _DECL_DLLMAIN
#include <vlc_common.h>
+#include <vlc_charset.h>
#include "libvlc.h"
#include <stdarg.h>
@@ -165,6 +166,7 @@ retry:
}
/*** Futeces^WAddress waits ***/
+static HRESULT (WINAPI *SetThreadDescription_)(HANDLE, PCWSTR);
#if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
static BOOL (WINAPI *WaitOnAddress_)(VOID volatile *, PVOID, SIZE_T, DWORD);
#define WaitOnAddress (*WaitOnAddress_)
@@ -429,6 +431,16 @@ int vlc_set_priority (vlc_thread_t th, int priority)
return VLC_SUCCESS;
}
+void vlc_thread_set_name(const char *name)
+{
+ if (SetThreadDescription_)
+ {
+ wchar_t *wname = ToWide(name);
+ SetThreadDescription_(GetCurrentThread(), wname);
+ free(wname);
+ }
+}
+
/*** Thread cancellation ***/
#if IS_INTERRUPTIBLE
@@ -809,6 +821,15 @@ BOOL WINAPI DllMain (HANDLE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
WakeByAddressAll_ = WakeByAddressFallback;
WakeByAddressSingle_ = WakeByAddressFallback;
}
+ LOOKUP(SetThreadDescription);
+#else
+ HMODULE h = GetModuleHandle(TEXT("kernel32.dll"));
+ if (h == NULL)
+ h = GetModuleHandle(TEXT("api-ms-win-core-processthreads-l1-1-3.dll"));
+ if (h != NULL)
+ LOOKUP(SetThreadDescription);
+ else
+ SetThreadDescription_ = NULL;
#endif
thread_key = TlsAlloc();
if (unlikely(thread_key == TLS_OUT_OF_INDEXES))
More information about the vlc-commits
mailing list