[PULL 12/27] audio: make jackaudio use qemu_thread_set_name

Daniel P. Berrangé via Devel posted 27 patches 2 weeks, 4 days ago
There is a newer version of this series
[PULL 12/27] audio: make jackaudio use qemu_thread_set_name
Posted by Daniel P. Berrangé 2 weeks, 4 days ago
This has greater portability than directly call pthread_setname_np,
which is only 1 out of 3 possible functions for pthreads that can
set the name.

The new API requires a trampoline function, since it can only set
the name of the current thread.

Reviewed-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 audio/jackaudio.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/audio/jackaudio.c b/audio/jackaudio.c
index f49fd82c45..be6fb378f7 100644
--- a/audio/jackaudio.c
+++ b/audio/jackaudio.c
@@ -633,18 +633,36 @@ static void qjack_enable_in(HWVoiceIn *hw, bool enable)
     ji->c.enabled = enable;
 }
 
-#if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
+#if !defined(WIN32)
+struct QJackThreadData {
+    void *(*function)(void *);
+    void *arg;
+};
+
+static void *qjack_thread_trampoline(void *targ)
+{
+    struct QJackThreadData *data = targ;
+    void *(*function)(void *) = data->function;
+    void *arg = data->arg;
+
+    g_free(data);
+    qemu_thread_set_name("jack-client");
+
+    return function(arg);
+}
+
 static int qjack_thread_creator(jack_native_thread_t *thread,
     const pthread_attr_t *attr, void *(*function)(void *), void *arg)
 {
-    int ret = pthread_create(thread, attr, function, arg);
+    struct QJackThreadData *data = g_new0(struct QJackThreadData, 1);
+    data->function = function;
+    data->arg = arg;
+    int ret = pthread_create(thread, attr, qjack_thread_trampoline, data);
     if (ret != 0) {
+        g_free(data);
         return ret;
     }
 
-    /* set the name of the thread */
-    pthread_setname_np(*thread, "jack-client");
-
     return ret;
 }
 #endif
-- 
2.53.0


Re: [PULL 12/27] audio: make jackaudio use qemu_thread_set_name
Posted by Philippe Mathieu-Daudé 2 weeks, 2 days ago
Hi,

On 5/3/26 18:47, Daniel P. Berrangé wrote:
> This has greater portability than directly call pthread_setname_np,
> which is only 1 out of 3 possible functions for pthreads that can
> set the name.
> 
> The new API requires a trampoline function, since it can only set
> the name of the current thread.
> 
> Reviewed-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   audio/jackaudio.c | 28 +++++++++++++++++++++++-----
>   1 file changed, 23 insertions(+), 5 deletions(-)
> 
> diff --git a/audio/jackaudio.c b/audio/jackaudio.c
> index f49fd82c45..be6fb378f7 100644
> --- a/audio/jackaudio.c
> +++ b/audio/jackaudio.c
> @@ -633,18 +633,36 @@ static void qjack_enable_in(HWVoiceIn *hw, bool enable)
>       ji->c.enabled = enable;
>   }
>   
> -#if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
> +#if !defined(WIN32)

I'm getting on macOS:

../../audio/jackaudio.c:654:12: error: unused function 
'qjack_thread_creator' [-Werror,-Wunused-function]
   654 | static int qjack_thread_creator(jack_native_thread_t *thread,
       |            ^~~~~~~~~~~~~~~~~~~~

No clue why this isn't covered on CI. My local build:

   Audio backends
     CoreAudio support               : YES
     PipeWire support                : NO
     JACK support                    : YES 1.9.22

> +struct QJackThreadData {
> +    void *(*function)(void *);
> +    void *arg;
> +};
> +
> +static void *qjack_thread_trampoline(void *targ)
> +{
> +    struct QJackThreadData *data = targ;
> +    void *(*function)(void *) = data->function;
> +    void *arg = data->arg;
> +
> +    g_free(data);
> +    qemu_thread_set_name("jack-client");
> +
> +    return function(arg);
> +}
> +
>   static int qjack_thread_creator(jack_native_thread_t *thread,
>       const pthread_attr_t *attr, void *(*function)(void *), void *arg)
>   {
> -    int ret = pthread_create(thread, attr, function, arg);
> +    struct QJackThreadData *data = g_new0(struct QJackThreadData, 1);
> +    data->function = function;
> +    data->arg = arg;
> +    int ret = pthread_create(thread, attr, qjack_thread_trampoline, data);
>       if (ret != 0) {
> +        g_free(data);
>           return ret;
>       }
>   
> -    /* set the name of the thread */
> -    pthread_setname_np(*thread, "jack-client");
> -
>       return ret;
>   }
>   #endif

Fixed adding:

-- >8 --
diff --git a/audio/jackaudio.c b/audio/jackaudio.c
index be6fb378f72..589aecede9a 100644
--- a/audio/jackaudio.c
+++ b/audio/jackaudio.c
@@ -712,7 +712,7 @@ static const TypeInfo audio_types[] = {
  static void __attribute__((__constructor__)) audio_jack_init(void)
  {
      qemu_mutex_init(&qjack_shutdown_lock);
-#if !defined(WIN32) && defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
+#if !defined(WIN32)
      jack_set_thread_creator(qjack_thread_creator);
  #endif
      jack_set_error_function(qjack_error);
---