On Fri, Sep 19, 2025 at 10:59:18AM +0200, Markus Armbruster wrote:
> Daniel P. Berrangé <berra...@redhat.com> writes:
> 
> > This will be used to include the thread name in error reports
> > in a later patch. It returns a const string stored in a thread
> > local to avoid memory allocation when it is called repeatedly
> > in a single thread. This makes the assumption that the thread
> > name is set at the very start of the thread, which is the case
> > when using qemu_thread_create.
> 
> What happens when the assumption is violated?

You will get an operating system default thread name,
which on Linux will default to the unqualified binary
name based on argv[0]

> 
> > Signed-off-by: Daniel P. Berrangé <berra...@redhat.com>
> > ---
> >  include/qemu/thread.h    |  1 +
> >  meson.build              | 21 +++++++++++++++++
> >  util/qemu-thread-posix.c | 28 ++++++++++++++++++++++-
> >  util/qemu-thread-win32.c | 49 ++++++++++++++++++++++++++++++++++++----
> >  4 files changed, 94 insertions(+), 5 deletions(-)


> > diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
> > index 275445ed94..fbb94ca97b 100644
> > --- a/util/qemu-thread-posix.c
> > +++ b/util/qemu-thread-posix.c
> > @@ -18,7 +18,7 @@
> >  #include "qemu/tsan.h"
> >  #include "qemu/bitmap.h"
> >  
> > -#ifdef CONFIG_PTHREAD_SET_NAME_NP
> > +#if defined(CONFIG_PTHREAD_SET_NAME_NP) || 
> > defined(CONFIG_PTHREAD_GET_NAME_NP)
> >  #include <pthread_np.h>
> >  #endif
> >  
> > @@ -532,3 +532,29 @@ void *qemu_thread_join(QemuThread *thread)
> >      }
> >      return ret;
> >  }
> > +
> > +#ifndef PTHREAD_MAX_NAMELEN_NP
> > +#define PTHREAD_MAX_NAMELEN_NP 16
> 
> Feels a bit tight.  32?

On Linux this constant is not defined, but the manpage says

  The thread name is a meaningful C language string, whose length is
  restricted to 16 characters, including the terminating null byte ('\0')

so I defined the constant to match the non-Linux constant
name, but with the Linux documented max length.

> 
> > +#endif
> > +
> > +static __thread char namebuf[PTHREAD_MAX_NAMELEN_NP];
> > +
> > +const char *qemu_thread_get_name(void)
> > +{
> > +    int rv;
> > +    if (namebuf[0] != '\0') {
> > +        return namebuf;
> > +    }
> > +
> > +# if defined(CONFIG_PTHREAD_GETNAME_NP)
> > +    rv = pthread_getname_np(pthread_self(), namebuf, sizeof(namebuf));
> > +# elif defined(CONFIG_PTHREAD_GET_NAME_NP)
> > +    rv = pthread_get_name_np(pthread_self(), namebuf, sizeof(namebuf));
> > +# else
> > +    rv = -1;
> > +# endif
> > +    if (rv != 0) {
> > +        strlcpy(namebuf, "unnamed", G_N_ELEMENTS(namebuf));
> > +    }
> > +    return namebuf;
> > +}

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Reply via email to