From nobody Mon May 13 20:47:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1645521240; cv=none; d=zohomail.com; s=zohoarc; b=HX9L5Fx7QBhYcvGEwID0FKfDRinKMsv6O4YAf6UCIReYDu/hLhxFAG0GDCnYcqY5sv0e1CCxA+Y0BG2SUsM5OIJZwrjrAVOkMDPJf/Kgxl8XlOGG1PU9EW1J8KMT07wndVhy36LjrfI3MWyZYX3T1AuIzxniMWTYihfEOdFPLaY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1645521240; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=ULifeiXxTksR91e7B5vixyvAw+Ofu01Yo9R8o0O2TK0=; b=cQc5h2GxwXXK2aOxgPG55/RvAJNKxtHtsDnjU20mbAS+FKwdpH2W0TPINrnkXjbzlV7zyefoRyfCNR6oJGiEF8I2R1mHG91y+F/StbCQMcbQa8rBG/+qln6RYhRyc4Yt38dvT3fZg4CKxYLYD/yXPzvY7uPnuHV6sNyTCWW20jI= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1645521240162363.50897895665787; Tue, 22 Feb 2022 01:14:00 -0800 (PST) Received: from localhost ([::1]:43018 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nMRFA-0000vs-D1 for importer@patchew.org; Tue, 22 Feb 2022 04:14:00 -0500 Received: from eggs.gnu.org ([209.51.188.92]:39236) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMR6y-0004pW-Fq for qemu-devel@nongnu.org; Tue, 22 Feb 2022 04:05:32 -0500 Received: from szxga02-in.huawei.com ([45.249.212.188]:5104) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMR6i-0001ft-5Z for qemu-devel@nongnu.org; Tue, 22 Feb 2022 04:05:22 -0500 Received: from dggpeml500026.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4K2tW45crPz9sxw; Tue, 22 Feb 2022 17:03:28 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggpeml500026.china.huawei.com (7.185.36.106) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 22 Feb 2022 17:05:11 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 22 Feb 2022 17:05:10 +0800 To: , , CC: , , , "Longpeng(Mike)" Subject: [PATCH v2 1/3] sem-posix: remove the posix semaphore support Date: Tue, 22 Feb 2022 17:05:04 +0800 Message-ID: <20220222090507.2028-2-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20220222090507.2028-1-longpeng2@huawei.com> References: <20220222090507.2028-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=45.249.212.188; envelope-from=longpeng2@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_HELO_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: "Longpeng(Mike)" From: "Longpeng(Mike)" via X-ZM-MESSAGEID: 1645521249848100001 Content-Type: text/plain; charset="utf-8" POSIX specifies an absolute time for sem_timedwait(), it would be affected if the system time is changing, but there is not a relative time or monotonic clock version of sem_timedwait, so we cannot gain from POSIX semaphore any more. An alternative way is to use sem_trywait + usleep, maybe we can remove CONFIG_SEM_TIMEDWAIT in this way? No, because some systems (e.g. mac os) mark the sem_xxx API as deprecated. So maybe remove the usage of POSIX semaphore and turn to use the pthread variant for all systems looks better. Signed-off-by: Longpeng(Mike) --- include/qemu/thread-posix.h | 4 ---- meson.build | 1 - util/qemu-thread-posix.c | 54 -----------------------------------------= ---- 3 files changed, 59 deletions(-) diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h index b792e6e..5466608 100644 --- a/include/qemu/thread-posix.h +++ b/include/qemu/thread-posix.h @@ -27,13 +27,9 @@ struct QemuCond { }; =20 struct QemuSemaphore { -#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_t lock; pthread_cond_t cond; unsigned int count; -#else - sem_t sem; -#endif bool initialized; }; =20 diff --git a/meson.build b/meson.build index 762d7ce..3ccb110 100644 --- a/meson.build +++ b/meson.build @@ -1557,7 +1557,6 @@ config_host_data.set('CONFIG_POSIX_FALLOCATE', cc.has= _function('posix_fallocate' config_host_data.set('CONFIG_POSIX_MEMALIGN', cc.has_function('posix_memal= ign')) config_host_data.set('CONFIG_PPOLL', cc.has_function('ppoll')) config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#= include ')) -config_host_data.set('CONFIG_SEM_TIMEDWAIT', cc.has_function('sem_timedwai= t', dependencies: threads)) config_host_data.set('CONFIG_SENDFILE', cc.has_function('sendfile')) config_host_data.set('CONFIG_SETNS', cc.has_function('setns') and cc.has_f= unction('unshare')) config_host_data.set('CONFIG_SYNCFS', cc.has_function('syncfs')) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index e1225b6..1ad2503 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -219,7 +219,6 @@ void qemu_sem_init(QemuSemaphore *sem, int init) { int rc; =20 -#ifndef CONFIG_SEM_TIMEDWAIT rc =3D pthread_mutex_init(&sem->lock, NULL); if (rc !=3D 0) { error_exit(rc, __func__); @@ -232,12 +231,6 @@ void qemu_sem_init(QemuSemaphore *sem, int init) error_exit(EINVAL, __func__); } sem->count =3D init; -#else - rc =3D sem_init(&sem->sem, 0, init); - if (rc < 0) { - error_exit(errno, __func__); - } -#endif sem->initialized =3D true; } =20 @@ -247,7 +240,6 @@ void qemu_sem_destroy(QemuSemaphore *sem) =20 assert(sem->initialized); sem->initialized =3D false; -#ifndef CONFIG_SEM_TIMEDWAIT rc =3D pthread_cond_destroy(&sem->cond); if (rc < 0) { error_exit(rc, __func__); @@ -256,12 +248,6 @@ void qemu_sem_destroy(QemuSemaphore *sem) if (rc < 0) { error_exit(rc, __func__); } -#else - rc =3D sem_destroy(&sem->sem); - if (rc < 0) { - error_exit(errno, __func__); - } -#endif } =20 void qemu_sem_post(QemuSemaphore *sem) @@ -269,7 +255,6 @@ void qemu_sem_post(QemuSemaphore *sem) int rc; =20 assert(sem->initialized); -#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_lock(&sem->lock); if (sem->count =3D=3D UINT_MAX) { rc =3D EINVAL; @@ -281,12 +266,6 @@ void qemu_sem_post(QemuSemaphore *sem) if (rc !=3D 0) { error_exit(rc, __func__); } -#else - rc =3D sem_post(&sem->sem); - if (rc < 0) { - error_exit(errno, __func__); - } -#endif } =20 int qemu_sem_timedwait(QemuSemaphore *sem, int ms) @@ -295,7 +274,6 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms) struct timespec ts; =20 assert(sem->initialized); -#ifndef CONFIG_SEM_TIMEDWAIT rc =3D 0; compute_abs_deadline(&ts, ms); pthread_mutex_lock(&sem->lock); @@ -313,29 +291,6 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms) } pthread_mutex_unlock(&sem->lock); return (rc =3D=3D ETIMEDOUT ? -1 : 0); -#else - if (ms <=3D 0) { - /* This is cheaper than sem_timedwait. */ - do { - rc =3D sem_trywait(&sem->sem); - } while (rc =3D=3D -1 && errno =3D=3D EINTR); - if (rc =3D=3D -1 && errno =3D=3D EAGAIN) { - return -1; - } - } else { - compute_abs_deadline(&ts, ms); - do { - rc =3D sem_timedwait(&sem->sem, &ts); - } while (rc =3D=3D -1 && errno =3D=3D EINTR); - if (rc =3D=3D -1 && errno =3D=3D ETIMEDOUT) { - return -1; - } - } - if (rc < 0) { - error_exit(errno, __func__); - } - return 0; -#endif } =20 void qemu_sem_wait(QemuSemaphore *sem) @@ -343,7 +298,6 @@ void qemu_sem_wait(QemuSemaphore *sem) int rc; =20 assert(sem->initialized); -#ifndef CONFIG_SEM_TIMEDWAIT pthread_mutex_lock(&sem->lock); while (sem->count =3D=3D 0) { rc =3D pthread_cond_wait(&sem->cond, &sem->lock); @@ -353,14 +307,6 @@ void qemu_sem_wait(QemuSemaphore *sem) } --sem->count; pthread_mutex_unlock(&sem->lock); -#else - do { - rc =3D sem_wait(&sem->sem); - } while (rc =3D=3D -1 && errno =3D=3D EINTR); - if (rc < 0) { - error_exit(errno, __func__); - } -#endif } =20 #ifdef __linux__ --=20 1.8.3.1 From nobody Mon May 13 20:47:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1645520981; cv=none; d=zohomail.com; s=zohoarc; b=buP3Bp2SVsmainjq2q5iXIxPZbocyigsBuNGiQqcWe5EllAs8gu3JccXoeVZ+gM99yWrttrXfaKYIZQFa1w6EdbE8gBHhBz9TZBeXevh3UEbIKciuu6d0ClLdDSb8SBja/gGZeQ4yliC+THhOTrGJ/55PhIGTELbB0e8kudx0Nk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1645520981; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=LE1lEZ0xfZWia/w+McZpMaBHNxzUeHLDKvY7irlXyHw=; b=SMyFWsZufmHPRBG/LXoppxMyHBTd7UAnUSlpRB7RijIKNz8CVLyzoM0eSdpJTvVEyZ0He3ig+dgljKLK1+JwTgFd52q6NuKGwX7ccEkIFalcwe5BQA0C2IKL/Ys3cvdtYnoY8P5Fuj4BIDTGYR2vNdN5yc8+v2+15SIhZ90XRg0= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1645520981791814.3084958848725; Tue, 22 Feb 2022 01:09:41 -0800 (PST) Received: from localhost ([::1]:39586 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nMRAx-0006tQ-Oe for importer@patchew.org; Tue, 22 Feb 2022 04:09:39 -0500 Received: from eggs.gnu.org ([209.51.188.92]:39232) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMR6x-0004pU-Ie for qemu-devel@nongnu.org; Tue, 22 Feb 2022 04:05:31 -0500 Received: from szxga02-in.huawei.com ([45.249.212.188]:5105) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMR6h-0001fu-HC for qemu-devel@nongnu.org; Tue, 22 Feb 2022 04:05:23 -0500 Received: from dggpeml500025.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4K2tW51zYLz9snB; Tue, 22 Feb 2022 17:03:29 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggpeml500025.china.huawei.com (7.185.36.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 22 Feb 2022 17:05:11 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 22 Feb 2022 17:05:11 +0800 To: , , CC: , , , "Longpeng(Mike)" Subject: [PATCH v2 2/3] sem-posix: use monotonic clock instead Date: Tue, 22 Feb 2022 17:05:05 +0800 Message-ID: <20220222090507.2028-3-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20220222090507.2028-1-longpeng2@huawei.com> References: <20220222090507.2028-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=45.249.212.188; envelope-from=longpeng2@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: "Longpeng(Mike)" From: "Longpeng(Mike)" via X-ZM-MESSAGEID: 1645520982519100001 Content-Type: text/plain; charset="utf-8" Use CLOCK_MONOTONIC, so the timeout isn't affected by changes to the system time. It depends on the pthread_condattr_setclock(), while some systems(e.g. mac os) does not support it, so the behavior won't change in these systems. Signed-off-by: Longpeng(Mike) --- meson.build | 11 +++++++++++ util/qemu-thread-posix.c | 49 +++++++++++++++++++++++++++++++++++++++++---= ---- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/meson.build b/meson.build index 3ccb110..2bab94f 100644 --- a/meson.build +++ b/meson.build @@ -1688,6 +1688,17 @@ config_host_data.set('CONFIG_PTHREAD_SETNAME_NP_WO_T= ID', cc.links(gnu_source_pre pthread_create(&thread, 0, f, 0); return 0; }''', dependencies: threads)) +config_host_data.set('CONFIG_PTHREAD_CONDATTR_SETCLOCK', cc.links(gnu_sour= ce_prefix + ''' + #include + #include + + int main(void) + { + pthread_condattr_t attr + pthread_condattr_init(&attr); + pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); + return 0; + }''', dependencies: threads)) =20 config_host_data.set('CONFIG_SIGNALFD', cc.links(gnu_source_prefix + ''' #include diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 1ad2503..44446ce 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -38,12 +38,20 @@ static void error_exit(int err, const char *msg) abort(); } =20 +static inline clockid_t qemu_timedwait_clockid(void) +{ +#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK + return CLOCK_MONOTONIC; +#else + return CLOCK_REALTIME; +#endif +} + static void compute_abs_deadline(struct timespec *ts, int ms) { - struct timeval tv; - gettimeofday(&tv, NULL); - ts->tv_nsec =3D tv.tv_usec * 1000 + (ms % 1000) * 1000000; - ts->tv_sec =3D tv.tv_sec + ms / 1000; + clock_gettime(qemu_timedwait_clockid(), ts); + ts->tv_nsec +=3D (ms % 1000) * 1000000; + ts->tv_sec +=3D ms / 1000; if (ts->tv_nsec >=3D 1000000000) { ts->tv_sec++; ts->tv_nsec -=3D 1000000000; @@ -147,11 +155,25 @@ void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, = const char *file, int line) =20 void qemu_cond_init(QemuCond *cond) { + pthread_condattr_t attr; int err; =20 - err =3D pthread_cond_init(&cond->cond, NULL); - if (err) + err =3D pthread_condattr_init(&attr); + if (err) { + error_exit(err, __func__); + } + err =3D pthread_condattr_setclock(&attr, qemu_timedwait_clockid()); + if (err) { + error_exit(err, __func__); + } + err =3D pthread_cond_init(&cond->cond, &attr); + if (err) { error_exit(err, __func__); + } + err =3D pthread_condattr_destroy(&attr); + if (err) { + error_exit(err, __func__); + } cond->initialized =3D true; } =20 @@ -217,16 +239,29 @@ bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMut= ex *mutex, int ms, =20 void qemu_sem_init(QemuSemaphore *sem, int init) { + pthread_condattr_t attr; int rc; =20 rc =3D pthread_mutex_init(&sem->lock, NULL); if (rc !=3D 0) { error_exit(rc, __func__); } - rc =3D pthread_cond_init(&sem->cond, NULL); + rc =3D pthread_condattr_init(&attr); + if (rc !=3D 0) { + error_exit(rc, __func__); + } + rc =3D pthread_condattr_setclock(&attr, qemu_timedwait_clockid()); if (rc !=3D 0) { error_exit(rc, __func__); } + rc =3D pthread_cond_init(&sem->cond, &attr); + if (rc !=3D 0) { + error_exit(rc, __func__); + } + rc =3D pthread_condattr_destroy(&attr); + if (rc < 0) { + error_exit(rc, __func__); + } if (init < 0) { error_exit(EINVAL, __func__); } --=20 1.8.3.1 From nobody Mon May 13 20:47:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1645521624; cv=none; d=zohomail.com; s=zohoarc; b=LjylU1gEz2LsXgTHOPBt+dyU/9Z8cA8GeUIMq5AeKF5Cihhyh8UbYXVPAZebPQwNZVNpXo0f4YkkOuAqrvTQDMT96h4rLLSejJkLjadhumfwU/oWlxVJvjqg+U6Z4SLWpgZpyYG2jQ6apHEIF7LLbXV0ve4oz6VuOca/CP6z94g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1645521624; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=lEyvqIbYSyRnPTyjdXsFS+wTmXVNMGKwjHSWdkpnVIs=; b=nkpoMaoYhTBPsMwzfntpvzsnC1MkiSknwmHNBw/jItYArTJFGXr9e+PN/Nih97aWSOKq+LCgU8Da7juBt+tMwvJ/7SzUhKC+03ywGFCkbvX4vnGVgw0iN2iveuTCjmi/vPC/wlWcA/SqXIA8rv36LGDH1XPKPo2sYQf+L2n92Eo= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1645521624009682.6320984844479; Tue, 22 Feb 2022 01:20:24 -0800 (PST) Received: from localhost ([::1]:47508 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nMRLK-00047v-Es for importer@patchew.org; Tue, 22 Feb 2022 04:20:22 -0500 Received: from eggs.gnu.org ([209.51.188.92]:39342) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMR7A-0004yr-Hu for qemu-devel@nongnu.org; Tue, 22 Feb 2022 04:05:46 -0500 Received: from szxga02-in.huawei.com ([45.249.212.188]:3084) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nMR74-0001jF-7Z for qemu-devel@nongnu.org; Tue, 22 Feb 2022 04:05:43 -0500 Received: from dggpeml500020.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4K2tWj22GFzcn1L; Tue, 22 Feb 2022 17:04:01 +0800 (CST) Received: from dggpeml100016.china.huawei.com (7.185.36.216) by dggpeml500020.china.huawei.com (7.185.36.88) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 22 Feb 2022 17:05:12 +0800 Received: from DESKTOP-27KDQMV.china.huawei.com (10.174.148.223) by dggpeml100016.china.huawei.com (7.185.36.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Tue, 22 Feb 2022 17:05:11 +0800 To: , , CC: , , , "Longpeng(Mike)" Subject: [PATCH v2 3/3] sem-posix: refactor qemu-sem with qemu-cond and qemu-mutex Date: Tue, 22 Feb 2022 17:05:06 +0800 Message-ID: <20220222090507.2028-4-longpeng2@huawei.com> X-Mailer: git-send-email 2.25.0.windows.1 In-Reply-To: <20220222090507.2028-1-longpeng2@huawei.com> References: <20220222090507.2028-1-longpeng2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.148.223] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml100016.china.huawei.com (7.185.36.216) X-CFilter-Loop: Reflected Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=45.249.212.188; envelope-from=longpeng2@huawei.com; helo=szxga02-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: "Longpeng(Mike)" From: "Longpeng(Mike)" via X-ZM-MESSAGEID: 1645521626035100001 Content-Type: text/plain; charset="utf-8" Now, qemu-sem is based on the pthread_cond only, we can use qemu-cond and qemu-mutex to make the code neater and the mutex trace can be supported in qemu-sem naturally. Signed-off-by: Longpeng(Mike) --- include/qemu/thread-posix.h | 5 +-- util/qemu-thread-posix.c | 103 ++++++++++++++--------------------------= ---- 2 files changed, 34 insertions(+), 74 deletions(-) diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h index 5466608..5f2f3d1 100644 --- a/include/qemu/thread-posix.h +++ b/include/qemu/thread-posix.h @@ -27,10 +27,9 @@ struct QemuCond { }; =20 struct QemuSemaphore { - pthread_mutex_t lock; - pthread_cond_t cond; + QemuMutex mutex; + QemuCond cond; unsigned int count; - bool initialized; }; =20 struct QemuEvent { diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 44446ce..f2ce47d 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -220,16 +220,15 @@ void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *m= utex, const char *file, con error_exit(err, __func__); } =20 -bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms, - const char *file, const int line) +static bool +qemu_cond_timedwait_ts(QemuCond *cond, QemuMutex *mutex, struct timespec *= ts, + const char *file, const int line) { int err; - struct timespec ts; =20 assert(cond->initialized); trace_qemu_mutex_unlock(mutex, file, line); - compute_abs_deadline(&ts, ms); - err =3D pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts); + err =3D pthread_cond_timedwait(&cond->cond, &mutex->lock, ts); trace_qemu_mutex_locked(mutex, file, line); if (err && err !=3D ETIMEDOUT) { error_exit(err, __func__); @@ -237,111 +236,73 @@ bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMu= tex *mutex, int ms, return err !=3D ETIMEDOUT; } =20 +bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms, + const char *file, const int line) +{ + struct timespec ts; + + compute_abs_deadline(&ts, ms); + return qemu_cond_timedwait_ts(cond, mutex, &ts, file, line); +} + void qemu_sem_init(QemuSemaphore *sem, int init) { - pthread_condattr_t attr; - int rc; + qemu_mutex_init(&sem->mutex); + qemu_cond_init(&sem->cond); =20 - rc =3D pthread_mutex_init(&sem->lock, NULL); - if (rc !=3D 0) { - error_exit(rc, __func__); - } - rc =3D pthread_condattr_init(&attr); - if (rc !=3D 0) { - error_exit(rc, __func__); - } - rc =3D pthread_condattr_setclock(&attr, qemu_timedwait_clockid()); - if (rc !=3D 0) { - error_exit(rc, __func__); - } - rc =3D pthread_cond_init(&sem->cond, &attr); - if (rc !=3D 0) { - error_exit(rc, __func__); - } - rc =3D pthread_condattr_destroy(&attr); - if (rc < 0) { - error_exit(rc, __func__); - } if (init < 0) { error_exit(EINVAL, __func__); } sem->count =3D init; - sem->initialized =3D true; } =20 void qemu_sem_destroy(QemuSemaphore *sem) { - int rc; - - assert(sem->initialized); - sem->initialized =3D false; - rc =3D pthread_cond_destroy(&sem->cond); - if (rc < 0) { - error_exit(rc, __func__); - } - rc =3D pthread_mutex_destroy(&sem->lock); - if (rc < 0) { - error_exit(rc, __func__); - } + qemu_cond_destroy(&sem->cond); + qemu_mutex_destroy(&sem->mutex); } =20 void qemu_sem_post(QemuSemaphore *sem) { - int rc; - - assert(sem->initialized); - pthread_mutex_lock(&sem->lock); + qemu_mutex_lock(&sem->mutex); if (sem->count =3D=3D UINT_MAX) { - rc =3D EINVAL; + error_exit(EINVAL, __func__); } else { sem->count++; - rc =3D pthread_cond_signal(&sem->cond); - } - pthread_mutex_unlock(&sem->lock); - if (rc !=3D 0) { - error_exit(rc, __func__); + qemu_cond_signal(&sem->cond); } + qemu_mutex_unlock(&sem->mutex); } =20 int qemu_sem_timedwait(QemuSemaphore *sem, int ms) { - int rc; + bool rc =3D true; struct timespec ts; =20 - assert(sem->initialized); - rc =3D 0; compute_abs_deadline(&ts, ms); - pthread_mutex_lock(&sem->lock); + qemu_mutex_lock(&sem->mutex); while (sem->count =3D=3D 0) { - rc =3D pthread_cond_timedwait(&sem->cond, &sem->lock, &ts); - if (rc =3D=3D ETIMEDOUT) { + rc =3D qemu_cond_timedwait_ts(&sem->cond, &sem->mutex, &ts, + __FILE__, __LINE__); + if (!rc) { /* timeout */ break; } - if (rc !=3D 0) { - error_exit(rc, __func__); - } } - if (rc !=3D ETIMEDOUT) { + if (rc) { --sem->count; } - pthread_mutex_unlock(&sem->lock); - return (rc =3D=3D ETIMEDOUT ? -1 : 0); + qemu_mutex_unlock(&sem->mutex); + return (rc ? 0 : -1); } =20 void qemu_sem_wait(QemuSemaphore *sem) { - int rc; - - assert(sem->initialized); - pthread_mutex_lock(&sem->lock); + qemu_mutex_lock(&sem->mutex); while (sem->count =3D=3D 0) { - rc =3D pthread_cond_wait(&sem->cond, &sem->lock); - if (rc !=3D 0) { - error_exit(rc, __func__); - } + qemu_cond_wait(&sem->cond, &sem->mutex); } --sem->count; - pthread_mutex_unlock(&sem->lock); + qemu_mutex_unlock(&sem->mutex); } =20 #ifdef __linux__ --=20 1.8.3.1