From nobody Wed Nov 5 18:32:06 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1536059516323682.3711456338743; Tue, 4 Sep 2018 04:11:56 -0700 (PDT) Received: from localhost ([::1]:50250 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fx9FP-0003fk-9V for importer@patchew.org; Tue, 04 Sep 2018 07:11:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50311) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fx9DL-0001UG-1E for qemu-devel@nongnu.org; Tue, 04 Sep 2018 07:09:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fx9DK-0006TG-0J for qemu-devel@nongnu.org; Tue, 04 Sep 2018 07:09:43 -0400 Received: from smtp.nue.novell.com ([195.135.221.5]:47949) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fx9DJ-0006Rj-Lt for qemu-devel@nongnu.org; Tue, 04 Sep 2018 07:09:41 -0400 Received: from localhost.localdomain ([45.122.156.254]) by smtp.nue.novell.com with ESMTP (NOT encrypted); Tue, 04 Sep 2018 13:09:39 +0200 From: Fei Li To: qemu-devel@nongnu.org Date: Tue, 4 Sep 2018 19:08:21 +0800 Message-Id: <20180904110822.12863-5-fli@suse.com> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20180904110822.12863-1-fli@suse.com> References: <20180904110822.12863-1-fli@suse.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 195.135.221.5 Subject: [Qemu-devel] [PATCH 4/5] qemu_thread_create: propagate the error to callers to check X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fli@suse.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add a new Error paramater for qemu_thread_create() to indicate if it succeeds rather than failing with an error. And propagate the error to let the callers check it. Besides, directly return if thread->data is NULL to avoid the segmentation fault in qemu_thread_join in qemu-thread-win32.c. Signed-off-by: Fei Li --- include/qemu/thread.h | 2 +- util/qemu-thread-posix.c | 15 +++++++++++---- util/qemu-thread-win32.c | 12 +++++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index dacebcfff0..71d8be5851 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -137,7 +137,7 @@ void qemu_event_destroy(QemuEvent *ev); =20 void qemu_thread_create(QemuThread *thread, const char *name, void *(*start_routine)(void *), - void *arg, int mode); + void *arg, int mode, Error **errp); void *qemu_thread_join(QemuThread *thread); void qemu_thread_get_self(QemuThread *thread); bool qemu_thread_is_self(QemuThread *thread); diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index dfa66ff2fb..a31c00abfd 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -15,6 +15,7 @@ #include "qemu/atomic.h" #include "qemu/notify.h" #include "qemu-thread-common.h" +#include "qapi/error.h" =20 static bool name_threads; =20 @@ -506,16 +507,17 @@ static void *qemu_thread_start(void *args) =20 void qemu_thread_create(QemuThread *thread, const char *name, void *(*start_routine)(void*), - void *arg, int mode) + void *arg, int mode, Error **errp) { sigset_t set, oldset; int err; pthread_attr_t attr; QemuThreadArgs *qemu_thread_args; + Error *local_err =3D NULL; =20 err =3D pthread_attr_init(&attr); if (err) { - error_exit(err, __func__); + goto fail; } =20 if (mode =3D=3D QEMU_THREAD_DETACHED) { @@ -534,12 +536,17 @@ void qemu_thread_create(QemuThread *thread, const cha= r *name, err =3D pthread_create(&thread->thread, &attr, qemu_thread_start, qemu_thread_args); =20 - if (err) - error_exit(err, __func__); + if (err) { + goto fail; + } =20 pthread_sigmask(SIG_SETMASK, &oldset, NULL); =20 pthread_attr_destroy(&attr); + return; +fail: + error_setg(&local_err, "%s", strerror(err)); + error_propagate(errp, local_err); } =20 void qemu_thread_get_self(QemuThread *thread) diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c index 4a363ca675..725200abc9 100644 --- a/util/qemu-thread-win32.c +++ b/util/qemu-thread-win32.c @@ -20,6 +20,7 @@ #include "qemu/thread.h" #include "qemu/notify.h" #include "qemu-thread-common.h" +#include "qapi/error.h" #include =20 static bool name_threads; @@ -366,7 +367,7 @@ void *qemu_thread_join(QemuThread *thread) HANDLE handle; =20 data =3D thread->data; - if (data->mode =3D=3D QEMU_THREAD_DETACHED) { + if (data =3D=3D NULL || data->mode =3D=3D QEMU_THREAD_DETACHED) { return NULL; } =20 @@ -390,10 +391,11 @@ void *qemu_thread_join(QemuThread *thread) =20 void qemu_thread_create(QemuThread *thread, const char *name, void *(*start_routine)(void *), - void *arg, int mode) + void *arg, int mode, Error **errp) { HANDLE hThread; struct QemuThreadData *data; + Error *local_err =3D NULL; =20 data =3D g_malloc(sizeof *data); data->start_routine =3D start_routine; @@ -409,7 +411,11 @@ void qemu_thread_create(QemuThread *thread, const char= *name, hThread =3D (HANDLE) _beginthreadex(NULL, 0, win32_start_routine, data, 0, &thread->tid); if (!hThread) { - error_exit(GetLastError(), __func__); + error_setg_win32(&local_err, GetLastError(), + "failed to creat win32_start_routine"); + g_free(data); + error_propagate(errp, local_err); + return; } CloseHandle(hThread); thread->data =3D data; --=20 2.13.7