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.
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