util: expose qemu_thread_set_name
The ability to set the thread name needs to be used in a number of places, so expose the current impls as public methods. Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Dr. David Alan Gilbert <dave@treblig.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Daniel P. Berrangé committed
Aug 29, 2025 at 15:51 UTC
46255cc2be9cb4a013077ac73b4cca41bf883425
3 files changed
+19
-14
include/qemu/thread.h
+1
@@ -215,6 +215,7 @@ void *qemu_thread_join(QemuThread *thread);
215
void qemu_thread_get_self(QemuThread *thread);
216
bool qemu_thread_is_self(QemuThread *thread);
217
G_NORETURN void qemu_thread_exit(void *retval);
218
+void qemu_thread_set_name(const char *name);
219
220
struct Notifier;
221
/**
util/qemu-thread-posix.c
+16
-10
@@ -329,6 +329,21 @@ static void qemu_thread_atexit_notify(void *arg)
329
notifier_list_notify(&thread_exit, NULL);
330
}
331
332
+void qemu_thread_set_name(const char *name)
333
+{
334
+ /*
335
+ * Attempt to set the thread name; note that this is for debug, so
336
+ * we're not going to fail if we can't set it.
337
+ */
338
+# if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
339
+ pthread_setname_np(pthread_self(), name);
340
+# elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
341
+ pthread_setname_np(name);
342
+# elif defined(CONFIG_PTHREAD_SET_NAME_NP)
343
+ pthread_set_name_np(pthread_self(), name);
344
+# endif
345
+}
346
+
347
typedef struct {
348
void *(*start_routine)(void *);
349
void *arg;
@@ -342,17 +357,8 @@ static void *qemu_thread_start(void *args)
357
void *arg = qemu_thread_args->arg;
358
void *r;
359
345
- /* Attempt to set the threads name; note that this is for debug, so
346
- * we're not going to fail if we can't set it.
347
- */
360
if (qemu_thread_args->name) {
349
-# if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
350
- pthread_setname_np(pthread_self(), qemu_thread_args->name);
351
-# elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
352
- pthread_setname_np(qemu_thread_args->name);
353
-# elif defined(CONFIG_PTHREAD_SET_NAME_NP)
354
- pthread_set_name_np(pthread_self(), qemu_thread_args->name);
355
-# endif
361
+ qemu_thread_set_name(qemu_thread_args->name);
362
}
363
QEMU_TSAN_ANNOTATE_THREAD_NAME(qemu_thread_args->name);
364
g_free(qemu_thread_args->name);
util/qemu-thread-win32.c
+2
-4
@@ -22,8 +22,6 @@ typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
22
static pSetThreadDescription SetThreadDescriptionFunc;
23
static HMODULE kernel32_module;
24
25
-static void set_thread_description(const char *name);
26
-
25
static bool load_set_thread_description(void)
26
{
27
static gsize _init_once = 0;
@@ -270,7 +268,7 @@ static unsigned __stdcall win32_start_routine(void *arg)
268
void *thread_arg = data->arg;
269
270
if (data->name) {
273
- set_thread_description(data->name);
271
+ qemu_thread_set_name(data->name);
272
g_clear_pointer(&data->name, g_free);
273
}
274
qemu_thread_data = data;
@@ -323,7 +321,7 @@ void *qemu_thread_join(QemuThread *thread)
321
return ret;
322
}
323
326
-static void set_thread_description(const char *name)
324
+void qemu_thread_set_name(const char *name)
325
{
326
g_autofree wchar_t *namew = NULL;
327