[PATCH v2 06/14] util: set the name for the 'main' thread

Daniel P. Berrangé posted 14 patches 4 weeks, 1 day ago
There is a newer version of this series
[PATCH v2 06/14] util: set the name for the 'main' thread
Posted by Daniel P. Berrangé 4 weeks, 1 day ago
The default main thread name is undefined, so use a constructor to
explicitly set it to 'main'. This constructor is marked to run early
as the thread name is intended to be used in error reporting / logs
which may be triggered very early in QEMU execution.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 util/qemu-thread-posix.c | 6 ++++++
 util/qemu-thread-win32.c | 6 ++++++
 2 files changed, 12 insertions(+)

diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index ddaa1de4dd..275445ed94 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -22,6 +22,12 @@
 #include <pthread_np.h>
 #endif
 
+static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
+qemu_thread_init(void)
+{
+    qemu_thread_set_name("main");
+}
+
 static void error_exit(int err, const char *msg)
 {
     fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
index 62eaa11026..7a734a7a09 100644
--- a/util/qemu-thread-win32.c
+++ b/util/qemu-thread-win32.c
@@ -22,6 +22,12 @@ typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
 static pSetThreadDescription SetThreadDescriptionFunc;
 static HMODULE kernel32_module;
 
+static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
+qemu_thread_init(void)
+{
+    qemu_thread_set_name("main");
+}
+
 static bool load_set_thread_description(void)
 {
     static gsize _init_once = 0;
-- 
2.50.1


Re: [PATCH v2 06/14] util: set the name for the 'main' thread
Posted by Dr. David Alan Gilbert 4 weeks ago
* Daniel P. Berrangé (berrange@redhat.com) wrote:
> The default main thread name is undefined, so use a constructor to
> explicitly set it to 'main'. This constructor is marked to run early
> as the thread name is intended to be used in error reporting / logs
> which may be triggered very early in QEMU execution.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Initially I was unsure why it was in qemu-thread-* - given these
are identical between the posix/windows versions; but I'm not sure
where else it would go.  You'd probably want it in both system and user,
and probably the none-emulator binaries as well.
So this may be the best place.

I'd probably rename it though, it looks like an operation on
a qemu_thread;  like qemu_thread_create() and qemu_thread_start()

Other than that;


Reviewed-by: Dr. David Alan Gilbert <dave@treblig.org>

> ---
>  util/qemu-thread-posix.c | 6 ++++++
>  util/qemu-thread-win32.c | 6 ++++++
>  2 files changed, 12 insertions(+)
> 
> diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
> index ddaa1de4dd..275445ed94 100644
> --- a/util/qemu-thread-posix.c
> +++ b/util/qemu-thread-posix.c
> @@ -22,6 +22,12 @@
>  #include <pthread_np.h>
>  #endif
>  
> +static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
> +qemu_thread_init(void)
> +{
> +    qemu_thread_set_name("main");
> +}
> +
>  static void error_exit(int err, const char *msg)
>  {
>      fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
> diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
> index 62eaa11026..7a734a7a09 100644
> --- a/util/qemu-thread-win32.c
> +++ b/util/qemu-thread-win32.c
> @@ -22,6 +22,12 @@ typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
>  static pSetThreadDescription SetThreadDescriptionFunc;
>  static HMODULE kernel32_module;
>  
> +static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
> +qemu_thread_init(void)
> +{
> +    qemu_thread_set_name("main");
> +}
> +
>  static bool load_set_thread_description(void)
>  {
>      static gsize _init_once = 0;
> -- 
> 2.50.1
> 
-- 
 -----Open up your eyes, open up your mind, open up your code -------   
/ Dr. David Alan Gilbert    |       Running GNU/Linux       | Happy  \ 
\        dave @ treblig.org |                               | In Hex /
 \ _________________________|_____ http://www.treblig.org   |_______/
Re: [PATCH v2 06/14] util: set the name for the 'main' thread
Posted by Richard Henderson 4 weeks ago
On 8/30/25 04:03, Daniel P. Berrangé wrote:
> The default main thread name is undefined, so use a constructor to
> explicitly set it to 'main'. This constructor is marked to run early
> as the thread name is intended to be used in error reporting / logs
> which may be triggered very early in QEMU execution.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   util/qemu-thread-posix.c | 6 ++++++
>   util/qemu-thread-win32.c | 6 ++++++
>   2 files changed, 12 insertions(+)
> 
> diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
> index ddaa1de4dd..275445ed94 100644
> --- a/util/qemu-thread-posix.c
> +++ b/util/qemu-thread-posix.c
> @@ -22,6 +22,12 @@
>   #include <pthread_np.h>
>   #endif
>   
> +static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
> +qemu_thread_init(void)
> +{
> +    qemu_thread_set_name("main");
> +}
> +
>   static void error_exit(int err, const char *msg)
>   {
>       fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
> diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
> index 62eaa11026..7a734a7a09 100644
> --- a/util/qemu-thread-win32.c
> +++ b/util/qemu-thread-win32.c
> @@ -22,6 +22,12 @@ typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
>   static pSetThreadDescription SetThreadDescriptionFunc;
>   static HMODULE kernel32_module;
>   
> +static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY)))
> +qemu_thread_init(void)
> +{
> +    qemu_thread_set_name("main");
> +}
> +
>   static bool load_set_thread_description(void)
>   {
>       static gsize _init_once = 0;

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~