diff mbox

[FFmpeg-devel] lavu: add ff_pthread_setname() and use it in various places

Message ID 20170623174804.89943-1-atomnuker@gmail.com
State Superseded
Headers show

Commit Message

Rostislav Pehlivanov June 23, 2017, 5:48 p.m. UTC
This is a re-posting of an old patch by Clément Bœsch <u@pkh.me>,
with the addition it'll define _GNU_SOURCE in the configure system,
making the functionality usable on modern glibc systems.

Signed-off-by: Rostislav Pehlivanov <atomnuker@gmail.com>
---
 configure                         |  2 ++
 libavcodec/frame_thread_encoder.c |  2 ++
 libavcodec/pthread_frame.c        |  2 ++
 libavcodec/pthread_slice.c        |  2 ++
 libavfilter/pthread.c             |  2 ++
 libavformat/async.c               |  2 ++
 libavformat/udp.c                 |  3 +++
 libavutil/thread.h                | 15 +++++++++++++++
 8 files changed, 30 insertions(+)

Comments

wm4 June 23, 2017, 5:51 p.m. UTC | #1
On Fri, 23 Jun 2017 18:48:04 +0100
Rostislav Pehlivanov <atomnuker@gmail.com> wrote:

> This is a re-posting of an old patch by Clément Bœsch <u@pkh.me>,
> with the addition it'll define _GNU_SOURCE in the configure system,
> making the functionality usable on modern glibc systems.
> 
> Signed-off-by: Rostislav Pehlivanov <atomnuker@gmail.com>
> ---
>  configure                         |  2 ++
>  libavcodec/frame_thread_encoder.c |  2 ++
>  libavcodec/pthread_frame.c        |  2 ++
>  libavcodec/pthread_slice.c        |  2 ++
>  libavfilter/pthread.c             |  2 ++
>  libavformat/async.c               |  2 ++
>  libavformat/udp.c                 |  3 +++
>  libavutil/thread.h                | 15 +++++++++++++++
>  8 files changed, 30 insertions(+)
> 
> diff --git a/configure b/configure
> index dd9608540e..319f2ef52c 100755
> --- a/configure
> +++ b/configure
> @@ -1976,6 +1976,7 @@ SYSTEM_FUNCS="
>      PeekNamedPipe
>      posix_memalign
>      pthread_cancel
> +    pthread_setname_np
>      sched_getaffinity
>      SetConsoleTextAttribute
>      SetConsoleCtrlHandler
> @@ -5765,6 +5766,7 @@ fi
>  
>  if enabled pthreads; then
>    check_func pthread_cancel
> +  check_func pthread_setname_np && add_cppflags -D_GNU_SOURCE
>  fi
>  
>  enabled pthreads &&
> diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
> index 6cf1a68fe7..7cee23760e 100644
> --- a/libavcodec/frame_thread_encoder.c
> +++ b/libavcodec/frame_thread_encoder.c
> @@ -62,6 +62,8 @@ static void * attribute_align_arg worker(void *v){
>      ThreadContext *c = avctx->internal->frame_thread_encoder;
>      AVPacket *pkt = NULL;
>  
> +    ff_thread_setname("lavc-frame-enc");
> +
>      while(!c->exit){
>          int got_packet, ret;
>          AVFrame *frame;
> diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
> index 363b139f71..8e69e93f71 100644
> --- a/libavcodec/pthread_frame.c
> +++ b/libavcodec/pthread_frame.c
> @@ -169,6 +169,8 @@ static attribute_align_arg void *frame_worker_thread(void *arg)
>      AVCodecContext *avctx = p->avctx;
>      const AVCodec *codec = avctx->codec;
>  
> +    ff_thread_setname("lavc-frame");
> +
>      pthread_mutex_lock(&p->mutex);
>      while (1) {
>          while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
> diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c
> index 60f5b78891..5d0d10d8d8 100644
> --- a/libavcodec/pthread_slice.c
> +++ b/libavcodec/pthread_slice.c
> @@ -70,6 +70,8 @@ static void* attribute_align_arg worker(void *v)
>      int thread_count = avctx->thread_count;
>      int self_id;
>  
> +    ff_thread_setname("lavc-slice");
> +
>      pthread_mutex_lock(&c->current_job_lock);
>      self_id = c->current_job++;
>      for (;;){
> diff --git a/libavfilter/pthread.c b/libavfilter/pthread.c
> index c7a00210d6..87da83ecaa 100644
> --- a/libavfilter/pthread.c
> +++ b/libavfilter/pthread.c
> @@ -61,6 +61,8 @@ static void* attribute_align_arg worker(void *v)
>      unsigned int last_execute = 0;
>      int ret, self_id;
>  
> +    ff_thread_setname("lavfi-worker");
> +
>      pthread_mutex_lock(&c->current_job_lock);
>      self_id = c->current_job++;
>  
> diff --git a/libavformat/async.c b/libavformat/async.c
> index 54dbd2312a..cd54c248ff 100644
> --- a/libavformat/async.c
> +++ b/libavformat/async.c
> @@ -181,6 +181,8 @@ static void *async_buffer_task(void *arg)
>      int           ret  = 0;
>      int64_t       seek_ret;
>  
> +    ff_thread_setname("lavf-async-buf");
> +
>      while (1) {
>          int fifo_space, to_copy;
>  
> diff --git a/libavformat/udp.c b/libavformat/udp.c
> index 3835f989c4..c0a61002fd 100644
> --- a/libavformat/udp.c
> +++ b/libavformat/udp.c
> @@ -36,6 +36,7 @@
>  #include "libavutil/avstring.h"
>  #include "libavutil/opt.h"
>  #include "libavutil/log.h"
> +#include "libavutil/thread.h"
>  #include "libavutil/time.h"
>  #include "internal.h"
>  #include "network.h"
> @@ -502,6 +503,8 @@ static void *circular_buffer_task_rx( void *_URLContext)
>      UDPContext *s = h->priv_data;
>      int old_cancelstate;
>  
> +    ff_thread_setname("udp-circ-buf");
> +
>      pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
>      pthread_mutex_lock(&s->mutex);
>      if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
> diff --git a/libavutil/thread.h b/libavutil/thread.h
> index f108e20052..24a4f1b083 100644
> --- a/libavutil/thread.h
> +++ b/libavutil/thread.h
> @@ -127,10 +127,23 @@ static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_
>  #define pthread_once           strict_pthread_once
>  #endif
>  
> +static inline void ff_thread_setname(const char *name)
> +{
> +#if HAVE_PTHREAD_SETNAME_NP
> +#if defined(__APPLE__)
> +    pthread_setname_np(name);
> +#elif defined(__linux__) && defined(__GLIBC__)
> +    pthread_setname_np(pthread_self(), name);
> +#endif
> +#endif
> +}
> +
>  #elif HAVE_OS2THREADS
>  #include "compat/os2threads.h"
> +#define ff_thread_setname(name) (0)
>  #else
>  #include "compat/w32pthreads.h"
> +#define ff_thread_setname(name) (0)
>  #endif
>  
>  #define AVMutex pthread_mutex_t
> @@ -157,6 +170,8 @@ static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_
>  #define AVOnce char
>  #define AV_ONCE_INIT 0
>  
> +#define ff_thread_setname(name) (0)
> +
>  static inline int ff_thread_once(char *control, void (*routine)(void))
>  {
>      if (!*control) {

These should use a ff prefix or so.
Moritz Barsnick June 24, 2017, 3:54 p.m. UTC | #2
On Fri, Jun 23, 2017 at 18:48:04 +0100, Rostislav Pehlivanov wrote:
> with the addition it'll define _GNU_SOURCE in the configure system,

Does setting _GNU_SOURCE have any implications on how other functions
work? Other than making this (and other unused functions) available.

> +  check_func pthread_setname_np && add_cppflags -D_GNU_SOURCE

This might even enable _GNU_SOURCE on e.g. BSD, and then:

> +#if HAVE_PTHREAD_SETNAME_NP
> +#if defined(__APPLE__)
> +    pthread_setname_np(name);
> +#elif defined(__linux__) && defined(__GLIBC__)
> +    pthread_setname_np(pthread_self(), name);
> +#endif
> +#endif

... ultimately not even use this patch's functionality. (Though
_GNU_SOURCE might not harm there, if it's not GNU.)

I'm not saying this is all wrong, it may be quite correct. I'm just
hinting "here (could) be dragons". ;-)

Moritz
Moritz Barsnick June 24, 2017, 3:57 p.m. UTC | #3
On Sat, Jun 24, 2017 at 17:54:50 +0200, Moritz Barsnick wrote:
> On Fri, Jun 23, 2017 at 18:48:04 +0100, Rostislav Pehlivanov wrote:
> > with the addition it'll define _GNU_SOURCE in the configure system,
> 
> Does setting _GNU_SOURCE have any implications on how other functions
> work? Other than making this (and other unused functions) available.

Sorry, I didn't see there was an updated patch in a different thread.
D'uh.

Moritz
diff mbox

Patch

diff --git a/configure b/configure
index dd9608540e..319f2ef52c 100755
--- a/configure
+++ b/configure
@@ -1976,6 +1976,7 @@  SYSTEM_FUNCS="
     PeekNamedPipe
     posix_memalign
     pthread_cancel
+    pthread_setname_np
     sched_getaffinity
     SetConsoleTextAttribute
     SetConsoleCtrlHandler
@@ -5765,6 +5766,7 @@  fi
 
 if enabled pthreads; then
   check_func pthread_cancel
+  check_func pthread_setname_np && add_cppflags -D_GNU_SOURCE
 fi
 
 enabled pthreads &&
diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
index 6cf1a68fe7..7cee23760e 100644
--- a/libavcodec/frame_thread_encoder.c
+++ b/libavcodec/frame_thread_encoder.c
@@ -62,6 +62,8 @@  static void * attribute_align_arg worker(void *v){
     ThreadContext *c = avctx->internal->frame_thread_encoder;
     AVPacket *pkt = NULL;
 
+    ff_thread_setname("lavc-frame-enc");
+
     while(!c->exit){
         int got_packet, ret;
         AVFrame *frame;
diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index 363b139f71..8e69e93f71 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -169,6 +169,8 @@  static attribute_align_arg void *frame_worker_thread(void *arg)
     AVCodecContext *avctx = p->avctx;
     const AVCodec *codec = avctx->codec;
 
+    ff_thread_setname("lavc-frame");
+
     pthread_mutex_lock(&p->mutex);
     while (1) {
         while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c
index 60f5b78891..5d0d10d8d8 100644
--- a/libavcodec/pthread_slice.c
+++ b/libavcodec/pthread_slice.c
@@ -70,6 +70,8 @@  static void* attribute_align_arg worker(void *v)
     int thread_count = avctx->thread_count;
     int self_id;
 
+    ff_thread_setname("lavc-slice");
+
     pthread_mutex_lock(&c->current_job_lock);
     self_id = c->current_job++;
     for (;;){
diff --git a/libavfilter/pthread.c b/libavfilter/pthread.c
index c7a00210d6..87da83ecaa 100644
--- a/libavfilter/pthread.c
+++ b/libavfilter/pthread.c
@@ -61,6 +61,8 @@  static void* attribute_align_arg worker(void *v)
     unsigned int last_execute = 0;
     int ret, self_id;
 
+    ff_thread_setname("lavfi-worker");
+
     pthread_mutex_lock(&c->current_job_lock);
     self_id = c->current_job++;
 
diff --git a/libavformat/async.c b/libavformat/async.c
index 54dbd2312a..cd54c248ff 100644
--- a/libavformat/async.c
+++ b/libavformat/async.c
@@ -181,6 +181,8 @@  static void *async_buffer_task(void *arg)
     int           ret  = 0;
     int64_t       seek_ret;
 
+    ff_thread_setname("lavf-async-buf");
+
     while (1) {
         int fifo_space, to_copy;
 
diff --git a/libavformat/udp.c b/libavformat/udp.c
index 3835f989c4..c0a61002fd 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -36,6 +36,7 @@ 
 #include "libavutil/avstring.h"
 #include "libavutil/opt.h"
 #include "libavutil/log.h"
+#include "libavutil/thread.h"
 #include "libavutil/time.h"
 #include "internal.h"
 #include "network.h"
@@ -502,6 +503,8 @@  static void *circular_buffer_task_rx( void *_URLContext)
     UDPContext *s = h->priv_data;
     int old_cancelstate;
 
+    ff_thread_setname("udp-circ-buf");
+
     pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
     pthread_mutex_lock(&s->mutex);
     if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
diff --git a/libavutil/thread.h b/libavutil/thread.h
index f108e20052..24a4f1b083 100644
--- a/libavutil/thread.h
+++ b/libavutil/thread.h
@@ -127,10 +127,23 @@  static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_
 #define pthread_once           strict_pthread_once
 #endif
 
+static inline void ff_thread_setname(const char *name)
+{
+#if HAVE_PTHREAD_SETNAME_NP
+#if defined(__APPLE__)
+    pthread_setname_np(name);
+#elif defined(__linux__) && defined(__GLIBC__)
+    pthread_setname_np(pthread_self(), name);
+#endif
+#endif
+}
+
 #elif HAVE_OS2THREADS
 #include "compat/os2threads.h"
+#define ff_thread_setname(name) (0)
 #else
 #include "compat/w32pthreads.h"
+#define ff_thread_setname(name) (0)
 #endif
 
 #define AVMutex pthread_mutex_t
@@ -157,6 +170,8 @@  static inline int strict_pthread_once(pthread_once_t *once_control, void (*init_
 #define AVOnce char
 #define AV_ONCE_INIT 0
 
+#define ff_thread_setname(name) (0)
+
 static inline int ff_thread_once(char *control, void (*routine)(void))
 {
     if (!*control) {