On Thu, Aug 07, 2025 at 01:49:09PM +0200, Juraj Marcin wrote: > From: Juraj Marcin <jmar...@redhat.com> > > Currently, QEMU threads abstraction supports both joinable and detached > threads, but once a thread is marked as joinable it must be joined using > qemu_thread_join() and cannot be detached later.
IMHO it is a good thing to avoid the concept of turning a joinable thread into a detached thread at runtime. Such a change makes it harder to reason about the correctness of the code, as you need to fully understand the global picture of what code runs at each phase of the thread's life, to decide if you need to join or not. So I'd really encourage looking at whether the migration code can be made to *always* join, rather than mixing joinable/detached for the same thread. > > For POSIX implementation, pthread_detach() is used. For Windows, marking > the thread as detached and releasing critical section is enough as > thread handle is released by qemu_thread_create(). > > Signed-off-by: Juraj Marcin <jmar...@redhat.com> > --- > include/qemu/thread.h | 1 + > util/qemu-thread-posix.c | 8 ++++++++ > util/qemu-thread-win32.c | 10 ++++++++++ > 3 files changed, 19 insertions(+) > > diff --git a/include/qemu/thread.h b/include/qemu/thread.h > index f0302ed01f..8a6d1ba98e 100644 > --- a/include/qemu/thread.h > +++ b/include/qemu/thread.h > @@ -212,6 +212,7 @@ int qemu_thread_set_affinity(QemuThread *thread, unsigned > long *host_cpus, > int qemu_thread_get_affinity(QemuThread *thread, unsigned long **host_cpus, > unsigned long *nbits); > void *qemu_thread_join(QemuThread *thread); > +void qemu_thread_detach(QemuThread *thread); > void qemu_thread_get_self(QemuThread *thread); > bool qemu_thread_is_self(QemuThread *thread); > G_NORETURN void qemu_thread_exit(void *retval); > diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c > index ba725444ba..20442456b5 100644 > --- a/util/qemu-thread-posix.c > +++ b/util/qemu-thread-posix.c > @@ -536,3 +536,11 @@ void *qemu_thread_join(QemuThread *thread) > } > return ret; > } > + > +void qemu_thread_detach(QemuThread *thread) > +{ > + int err = pthread_detach(thread->thread); > + if (err) { > + error_exit(err, __func__); > + } > +} > diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c > index ca2e0b512e..bdfb7b4aee 100644 > --- a/util/qemu-thread-win32.c > +++ b/util/qemu-thread-win32.c > @@ -328,6 +328,16 @@ void *qemu_thread_join(QemuThread *thread) > return ret; > } > > +void qemu_thread_detach(QemuThread *thread) > +{ > + QemuThreadData *data; > + > + if (data->mode == QEMU_THREAD_JOINABLE) { > + data->mode = QEMU_THREAD_DETACHED; > + DeleteCriticalSection(&data->cs); > + } > +} > + > static bool set_thread_description(HANDLE h, const char *name) > { > HRESULT hr; > -- > 2.50.1 > > 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 :|