Allow to control if a QEMU thread is created joinable or not. Make it not joinable by default to avoid that we keep the associated resources around when terminating a thread without joining it (what we couldn't do so far for obvious reasons).
The audio subsystem will need the join feature when converting it to QEMU threading/locking abstractions, so provide that service. Join will only be provided for POSIX as there is no setup in sight yet where Windows would need it as well (+implementation is non-trivial). Signed-off-by: Jan Kiszka <jan.kis...@siemens.com> --- cpus.c | 6 ++++-- hw/ccid-card-emulated.c | 5 +++-- qemu-thread-posix.c | 31 +++++++++++++++++++++++++++---- qemu-thread-posix.h | 3 +++ qemu-thread-win32.c | 4 +++- qemu-thread.h | 7 +++++-- ui/vnc-jobs-async.c | 2 +- 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/cpus.c b/cpus.c index d0cfe91..3f7724a 100644 --- a/cpus.c +++ b/cpus.c @@ -822,7 +822,8 @@ static void qemu_tcg_init_vcpu(void *_env) env->halt_cond = g_malloc0(sizeof(QemuCond)); qemu_cond_init(env->halt_cond); tcg_halt_cond = env->halt_cond; - qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env); + qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env, + QEMU_THREAD_DETACHED); while (env->created == 0) { qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); } @@ -838,7 +839,8 @@ static void qemu_kvm_start_vcpu(CPUState *env) env->thread = g_malloc0(sizeof(QemuThread)); env->halt_cond = g_malloc0(sizeof(QemuCond)); qemu_cond_init(env->halt_cond); - qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env); + qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env, + QEMU_THREAD_DETACHED); while (env->created == 0) { qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); } diff --git a/hw/ccid-card-emulated.c b/hw/ccid-card-emulated.c index 092301b..9fe9db5 100644 --- a/hw/ccid-card-emulated.c +++ b/hw/ccid-card-emulated.c @@ -541,8 +541,9 @@ static int emulated_initfn(CCIDCardState *base) printf("%s: failed to initialize vcard\n", EMULATED_DEV_NAME); return -1; } - qemu_thread_create(&thread_id, event_thread, card); - qemu_thread_create(&thread_id, handle_apdu_thread, card); + qemu_thread_create(&thread_id, event_thread, card, QEMU_THREAD_DETACHED); + qemu_thread_create(&thread_id, handle_apdu_thread, card, + QEMU_THREAD_DETACHED); return 0; } diff --git a/qemu-thread-posix.c b/qemu-thread-posix.c index 2bd02ef..f76427e 100644 --- a/qemu-thread-posix.c +++ b/qemu-thread-posix.c @@ -117,20 +117,33 @@ void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) void qemu_thread_create(QemuThread *thread, void *(*start_routine)(void*), - void *arg) + void *arg, int mode) { + sigset_t set, oldset; + pthread_attr_t attr; int err; - /* Leave signal handling to the iothread. */ - sigset_t set, oldset; + err = pthread_attr_init(&attr); + if (err) { + error_exit(err, __func__); + } + if (mode == QEMU_THREAD_DETACHED) { + err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + if (err) { + error_exit(err, __func__); + } + } + /* Leave signal handling to the iothread. */ sigfillset(&set); pthread_sigmask(SIG_SETMASK, &set, &oldset); - err = pthread_create(&thread->thread, NULL, start_routine, arg); + err = pthread_create(&thread->thread, &attr, start_routine, arg); if (err) error_exit(err, __func__); pthread_sigmask(SIG_SETMASK, &oldset, NULL); + + pthread_attr_destroy(&attr); } void qemu_thread_get_self(QemuThread *thread) @@ -147,3 +160,13 @@ void qemu_thread_exit(void *retval) { pthread_exit(retval); } + +void qemu_thread_join(QemuThread *thread) +{ + int err; + + err = pthread_join(thread->thread, NULL); + if (err) { + error_exit(err, __func__); + } +} diff --git a/qemu-thread-posix.h b/qemu-thread-posix.h index ee4618e..540fa0b 100644 --- a/qemu-thread-posix.h +++ b/qemu-thread-posix.h @@ -14,4 +14,7 @@ struct QemuThread { pthread_t thread; }; +/* only provided for posix so far */ +void qemu_thread_join(QemuThread *thread); + #endif diff --git a/qemu-thread-win32.c b/qemu-thread-win32.c index a27332e..4819ff4 100644 --- a/qemu-thread-win32.c +++ b/qemu-thread-win32.c @@ -243,10 +243,12 @@ static inline void qemu_thread_init(void) void qemu_thread_create(QemuThread *thread, void *(*start_routine)(void *), - void *arg) + void *arg, int mode) { HANDLE hThread; + assert(mode == QEMU_THREAD_DETACHED); + struct QemuThreadData *data; qemu_thread_init(); data = g_malloc(sizeof *data); diff --git a/qemu-thread.h b/qemu-thread.h index 0a73d50..aa1ae55 100644 --- a/qemu-thread.h +++ b/qemu-thread.h @@ -13,6 +13,9 @@ typedef struct QemuThread QemuThread; #include "qemu-thread-posix.h" #endif +#define QEMU_THREAD_JOINABLE 0 +#define QEMU_THREAD_DETACHED 1 + void qemu_mutex_init(QemuMutex *mutex); void qemu_mutex_destroy(QemuMutex *mutex); void qemu_mutex_lock(QemuMutex *mutex); @@ -32,8 +35,8 @@ void qemu_cond_broadcast(QemuCond *cond); void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex); void qemu_thread_create(QemuThread *thread, - void *(*start_routine)(void*), - void *arg); + void *(*start_routine)(void *), + void *arg, int mode); void qemu_thread_get_self(QemuThread *thread); int qemu_thread_is_self(QemuThread *thread); void qemu_thread_exit(void *retval); diff --git a/ui/vnc-jobs-async.c b/ui/vnc-jobs-async.c index de5ea6b..9b3016c 100644 --- a/ui/vnc-jobs-async.c +++ b/ui/vnc-jobs-async.c @@ -318,7 +318,7 @@ void vnc_start_worker_thread(void) return ; q = vnc_queue_init(); - qemu_thread_create(&q->thread, vnc_worker_thread, q); + qemu_thread_create(&q->thread, vnc_worker_thread, q, QEMU_THREAD_DETACHED); queue = q; /* Set global queue */ } -- 1.7.3.4