[vlc-devel] [PATCH 1/4] thread: add a function to give names to threads

Rémi Denis-Courmont remi at remlab.net
Mon Nov 9 16:01:37 CET 2020


Le maanantaina 9. marraskuuta 2020, 16.28.21 EET Steve Lhomme a écrit :
> 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 windows variant uses wchar to avoid converting strings to the heap.
> The posix variants comes from dav1d.
> ---
>  include/vlc_threads.h |  7 +++++++
>  src/libvlccore.sym    |  1 +
>  src/os2/thread.c      |  5 +++++
>  src/posix/thread.c    | 23 +++++++++++++++++++++++
>  src/win32/thread.c    | 19 +++++++++++++++++++
>  5 files changed, 55 insertions(+)
> 
> diff --git a/include/vlc_threads.h b/include/vlc_threads.h
> index 21f4e46c278..6ee03f02164 100644
> --- a/include/vlc_threads.h
> +++ b/include/vlc_threads.h
> @@ -750,6 +750,13 @@ 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
> + */
> +VLC_API void vlc_thread_set_name(const char *name);
> +
>  /**
>   * Marks a thread as cancelled.
>   *
> diff --git a/src/libvlccore.sym b/src/libvlccore.sym
> index c4d5273dbd5..8b149ae2b74 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 822b3f3b26c..cce326efc29 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 91eab74716d..930ba72e64d 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);

Missing casts.

This is limited to 15 characters, so the way you picked the names in the 
following patches is not going to work very well.

> +}
> +#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 60b4232e047..b2dedab692d 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,13 @@ 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)
> +                LOOKUP(SetThreadDescription);
> +            else
> +                SetThreadDescription_ = NULL;
>  #endif
>              thread_key = TlsAlloc();
>              if (unlikely(thread_key == TLS_OUT_OF_INDEXES))


-- 
雷米‧德尼-库尔蒙
http://www.remlab.net/





More information about the vlc-devel mailing list