[vlc-commits] [Git][videolan/vlc][master] 2 commits: thread: add a function to give names to threads

Steve Lhomme (@robUx4) gitlab at videolan.org
Wed Apr 27 13:05:58 UTC 2022



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
a94b5e54 by Steve Lhomme at 2022-04-27T12:45:10+00:00
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.

- - - - -
f896e42e by Steve Lhomme at 2022-04-27T12:45:10+00:00
core: set names to threads created in the core

- - - - -


25 changed files:

- include/vlc_threads.h
- src/Makefile.am
- + src/darwin/thread.c
- src/freebsd/thread.c
- src/input/decoder.c
- src/input/demux_chained.c
- src/input/es_out_timeshift.c
- src/input/input.c
- src/input/vlm.c
- src/libvlccore.sym
- src/linux/thread.c
- src/misc/addons.c
- src/misc/executor.c
- src/misc/update.c
- src/network/httpd.c
- src/os2/thread.c
- src/player/player.c
- src/posix/getaddrinfo.c
- src/posix/thread.c
- src/posix/timer.c
- src/stream_output/sap.c
- src/video_output/video_output.c
- src/video_output/vout_subpictures.c
- src/win32/mta_holder.h
- src/win32/thread.c


Changes:

=====================================
include/vlc_threads.h
=====================================
@@ -631,6 +631,16 @@ VLC_API void *vlc_threadvar_get(vlc_threadvar_t);
 VLC_API int vlc_clone(vlc_thread_t *th, void *(*entry)(void *),
                       void *data) 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-byte long, including the terminating
+ *       nul character. If larger, the name will be truncated.
+ */
+VLC_API void vlc_thread_set_name(const char *name);
+
 /**
  * Marks a thread as cancelled.
  *


=====================================
src/Makefile.am
=====================================
@@ -466,7 +466,8 @@ endif
 if HAVE_DARWIN
 libvlccore_la_SOURCES += \
 	darwin/error.c \
-	darwin/specific.c
+	darwin/specific.c \
+	darwin/thread.c
 libvlccore_objc_la_SOURCES = \
 	darwin/dirs.m \
 	darwin/netconf.m


=====================================
src/darwin/thread.c
=====================================
@@ -0,0 +1,31 @@
+/*****************************************************************************
+ * darwin/thread.c: Darwin specifics for threading
+ *****************************************************************************
+ * Copyright (C) 2022 - VideoLabs, VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_threads.h>
+
+void vlc_thread_set_name(const char *name)
+{
+    pthread_setname_np(name);
+}


=====================================
src/freebsd/thread.c
=====================================
@@ -28,6 +28,7 @@
 #include <limits.h>
 #include <sys/types.h>
 #include <pthread.h>
+#include <pthread_np.h>
 #include <sys/thr.h>
 #include <sys/umtx.h>
 
@@ -44,6 +45,11 @@ unsigned long vlc_thread_id(void)
      return tid;
 }
 
+void vlc_thread_set_name(const char *name)
+{
+    pthread_set_name_np(pthread_self(), name);
+}
+
 static int vlc_umtx_wake(void *addr, int nr)
 {
     return _umtx_op(addr, UMTX_OP_WAKE_PRIVATE, nr, NULL, NULL);


=====================================
src/input/decoder.c
=====================================
@@ -1652,6 +1652,8 @@ static void *DecoderThread( void *p_data )
     vlc_tick_t delay = 0;
     bool paused = false;
 
+    vlc_thread_set_name("vlc-decoder");
+
     /* The decoder's main loop */
     vlc_fifo_Lock( p_owner->p_fifo );
 


=====================================
src/input/demux_chained.c
=====================================
@@ -53,6 +53,8 @@ struct vlc_demux_chained_t
 
 static void *vlc_demux_chained_Thread(void *data)
 {
+    vlc_thread_set_name("vlc-demux-chain");
+
     vlc_demux_chained_t *dc = data;
     demux_t *demux = demux_New(VLC_OBJECT(dc->reader), dc->name, "vlc://nop",
                                dc->reader, dc->out);


=====================================
src/input/es_out_timeshift.c
=====================================
@@ -1089,6 +1089,8 @@ static int TsChangeRate( ts_thread_t *p_ts, float src_rate, float rate )
 
 static void *TsRun( void *p_data )
 {
+    vlc_thread_set_name("vlc-timeshift");
+
     ts_thread_t *p_ts = p_data;
     vlc_tick_t i_buffering_date = -1;
 


=====================================
src/input/input.c
=====================================
@@ -419,6 +419,8 @@ static void *Run( void *data )
     input_thread_private_t *priv = data;
     input_thread_t *p_input = &priv->input;
 
+    vlc_thread_set_name("vlc-input");
+
     vlc_interrupt_set(&priv->interrupt);
 
     if( !Init( p_input ) )
@@ -438,6 +440,8 @@ static void *Preparse( void *data )
     input_thread_private_t *priv = data;
     input_thread_t *p_input = &priv->input;
 
+    vlc_thread_set_name("vlc-preparse");
+
     vlc_interrupt_set(&priv->interrupt);
 
     if( !Init( p_input ) )


=====================================
src/input/vlm.c
=====================================
@@ -238,6 +238,8 @@ int vlm_ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
  *****************************************************************************/
 static void* Manage( void* p_object )
 {
+    vlc_thread_set_name("vlc-vlm");
+
     vlm_t *vlm = (vlm_t*)p_object;
     time_t lastcheck;
     bool exiting;
@@ -773,7 +775,7 @@ static int vlm_ControlMediaInstanceGetTimePosition( vlm_t *p_vlm, int64_t id, co
 
     vlc_player_Lock(p_instance->player);
     if( pi_time )
-        *pi_time = US_FROM_VLC_TICK(vlc_player_GetTime(p_instance->player)); 
+        *pi_time = US_FROM_VLC_TICK(vlc_player_GetTime(p_instance->player));
     if( pd_position )
         *pd_position = vlc_player_GetPosition(p_instance->player);
     vlc_player_Unlock(p_instance->player);


=====================================
src/libvlccore.sym
=====================================
@@ -636,6 +636,7 @@ vlc_interrupt_register
 vlc_interrupt_unregister
 vlc_killed
 vlc_join
+vlc_thread_set_name
 vlc_list_children
 vlc_meta_AddExtra
 vlc_meta_CopyExtraNames


=====================================
src/linux/thread.c
=====================================
@@ -28,6 +28,7 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <sys/prctl.h>
 #include <sys/syscall.h>
 #include <linux/futex.h>
 
@@ -50,6 +51,11 @@ unsigned long vlc_thread_id(void)
      return tid;
 }
 
+void vlc_thread_set_name(const char *name)
+{
+    prctl(PR_SET_NAME, name);
+}
+
 static int sys_futex(void *addr, int op, unsigned val,
                      const struct timespec *to, void *addr2, int val3)
 {


=====================================
src/misc/addons.c
=====================================
@@ -330,6 +330,8 @@ static void finder_thread_interrupted( void* p_data )
 
 static void *FinderThread( void *p_data )
 {
+    vlc_thread_set_name("vlc-addon-find");
+
     addons_manager_t *p_manager = p_data;
     int i_cancel = vlc_savecancel();
     vlc_interrupt_set( p_manager->p_priv->finder.p_interrupt );
@@ -456,6 +458,8 @@ static void installer_thread_interrupted( void* p_data )
 
 static void *InstallerThread( void *p_data )
 {
+    vlc_thread_set_name("vlc-addon-instl");
+
     addons_manager_t *p_manager = p_data;
     int i_cancel = vlc_savecancel();
     vlc_interrupt_set( p_manager->p_priv->installer.p_interrupt );


=====================================
src/misc/executor.c
=====================================
@@ -119,6 +119,8 @@ ThreadRun(void *userdata)
     struct vlc_executor_thread *thread = userdata;
     vlc_executor_t *executor = thread->owner;
 
+    vlc_thread_set_name("vlc-exec-runner");
+
     vlc_mutex_lock(&executor->lock);
 
     struct vlc_runnable *runnable;


=====================================
src/misc/update.c
=====================================
@@ -412,6 +412,8 @@ void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void
 
 void* update_CheckReal( void *obj )
 {
+    vlc_thread_set_name("vlc-updater-chk");
+
     update_check_thread_t *p_uct = (update_check_thread_t *)obj;
     bool b_ret;
     int canc;
@@ -524,6 +526,8 @@ void update_Download( update_t *p_update, const char *psz_destdir )
 
 static void* update_DownloadReal( void *obj )
 {
+    vlc_thread_set_name("vlc-updater-dl");
+
     update_download_thread_t *p_udt = (update_download_thread_t *)obj;
     uint64_t l_size;
     uint64_t l_downloaded = 0;


=====================================
src/network/httpd.c
=====================================
@@ -2054,6 +2054,8 @@ static void httpdLoop(httpd_host_t *host)
 
 static void* httpd_HostThread(void *data)
 {
+    vlc_thread_set_name("vlc-httpd");
+
     httpd_host_t *host = data;
 
     while (atomic_load_explicit(&host->ref, memory_order_relaxed) > 0)


=====================================
src/os2/thread.c
=====================================
@@ -509,6 +509,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 */


=====================================
src/player/player.c
=====================================
@@ -200,6 +200,8 @@ vlc_player_destructor_Thread(void *data)
 {
     vlc_player_t *player = data;
 
+    vlc_thread_set_name("vlc-player-end");
+
     vlc_mutex_lock(&player->lock);
 
     /* Terminate this thread when the player is deleting (vlc_player_Delete()


=====================================
src/posix/getaddrinfo.c
=====================================
@@ -42,6 +42,8 @@ struct vlc_gai_req
 
 static void *vlc_gai_thread(void *data)
 {
+    vlc_thread_set_name("vlc-getaddrinfo");
+
     struct vlc_gai_req *req = data;
 
     req->error = EAI_SYSTEM;


=====================================
src/posix/thread.c
=====================================
@@ -204,6 +204,11 @@ VLC_WEAK unsigned long vlc_thread_id(void)
      return (uintptr_t)(void *)&dummy;
 }
 
+VLC_WEAK void vlc_thread_set_name(const char *name)
+{
+    VLC_UNUSED(name);
+}
+
 void vlc_cancel(vlc_thread_t th)
 {
     pthread_cancel(th.handle);


=====================================
src/posix/timer.c
=====================================
@@ -52,6 +52,8 @@ struct vlc_timer
 
 static void *vlc_timer_thread (void *data)
 {
+    vlc_thread_set_name("vlc-timer");
+
     struct vlc_timer *timer = data;
 
     vlc_mutex_lock (&timer->lock);


=====================================
src/stream_output/sap.c
=====================================
@@ -123,6 +123,8 @@ static void AddressDestroy (sap_address_t *addr)
  */
 static void *RunThread (void *self)
 {
+    vlc_thread_set_name("vlc-sap");
+
     sap_address_t *addr = self;
 
     vlc_mutex_lock(&sap_mutex);


=====================================
src/video_output/video_output.c
=====================================
@@ -1717,6 +1717,8 @@ static void *Thread(void *object)
     vout_thread_sys_t *vout = object;
     vout_thread_sys_t *sys = vout;
 
+    vlc_thread_set_name("vlc-vout");
+
     vlc_tick_t deadline = VLC_TICK_INVALID;
 
     for (;;) {


=====================================
src/video_output/vout_subpictures.c
=====================================
@@ -1518,6 +1518,8 @@ static void * spu_PrerenderThread(void *priv)
     spu_private_t *sys = spu->p;
     vlc_fourcc_t chroma_list[SPU_CHROMALIST_COUNT+1];
 
+    vlc_thread_set_name("vlc-spu-prerend");
+
     chroma_list[SPU_CHROMALIST_COUNT] = 0;
 
     vlc_mutex_lock(&sys->prerender.lock);


=====================================
src/win32/mta_holder.h
=====================================
@@ -39,6 +39,8 @@ typedef struct vlc_mta_holder
 
 static inline void* MtaMainLoop( void* opaque )
 {
+    vlc_thread_set_name("vlc-mta");
+
     vlc_mta_holder* p_mta = (vlc_mta_holder*)opaque;
     CoInitializeEx( NULL, COINIT_MULTITHREADED );
 


=====================================
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>
@@ -160,6 +161,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_)
@@ -405,6 +407,16 @@ unsigned long vlc_thread_id (void)
     return GetCurrentThreadId ();
 }
 
+void vlc_thread_set_name(const char *name)
+{
+    if (SetThreadDescription_)
+    {
+        wchar_t *wname = ToWide(name);
+        SetThreadDescription_(GetCurrentThread(), wname);
+        free(wname);
+    }
+}
+
 /*** Thread cancellation ***/
 
 /* APC procedure for thread cancellation */
@@ -723,8 +735,17 @@ BOOL WINAPI DllMain (HANDLE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
     {
         case DLL_PROCESS_ATTACH:
         {
+            HMODULE h;
+            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;
+
 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
-            HMODULE h = GetModuleHandle(TEXT("api-ms-win-core-synch-l1-2-0.dll"));
+            h = GetModuleHandle(TEXT("api-ms-win-core-synch-l1-2-0.dll"));
             if (h == NULL || !LOOKUP(WaitOnAddress)
              || !LOOKUP(WakeByAddressAll) || !LOOKUP(WakeByAddressSingle))
             {



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/ec4f3c10b70b208c1f6ce2ff0e11d84b46fb284f...f896e42e7a97ce971d64821c6a68784f47444de2

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/ec4f3c10b70b208c1f6ce2ff0e11d84b46fb284f...f896e42e7a97ce971d64821c6a68784f47444de2
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list