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

Rémi Denis-Courmont remi at remlab.net
Wed Nov 11 18:56:30 CET 2020


Le tiistaina 10. marraskuuta 2020, 13.05.47 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 posix variants comes from dav1d.
> ---
>  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 21f4e46c278..46490334eb8 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.

I think it spells nul.

> + */
> +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 79132be2e46..02dad44d083 100644
> --- a/src/android/thread.c
> +++ b/src/android/thread.c
> @@ -195,6 +195,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);
> +}
> +

This is not Android-specific and should be in linux/thread.c.

>  void vlc_cancel (vlc_thread_t thread_id)
>  {
>      atomic_uint *addr;
> 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);
> +}
> +#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..b2787a08adf 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))


-- 
Реми Дёни-Курмон
http://www.remlab.net/





More information about the vlc-devel mailing list