From: Laszlo Ersek <lersek@redhat.com>
To: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v2] semaphore: fix a hangup problem under load on NetBSD hosts.
Date: Wed, 03 Jul 2013 10:25:12 +0200 [thread overview]
Message-ID: <51D3DFE8.7080202@redhat.com> (raw)
In-Reply-To: <1372784047-12468-1-git-send-email-tsutsui@ceres.dti.ne.jp>
On 07/02/13 18:54, Izumi Tsutsui wrote:
> Fix following bugs in "fallback implementation of counting semaphores
> with mutex+condvar" added in c166cb72f1676855816340666c3b618beef4b976:
> - waiting threads are not restarted properly if more than one threads
> are waiting unblock signals in qemu_sem_timedwait()
> - possible missing pthread_cond_signal(3) calls when waiting threads
> are returned by ETIMEDOUT
> - fix an uninitialized variable
> The problem is analyzed by and fix is provided by Noriyuki Soda.
>
> Also put additional cleanup suggested by Laszlo Ersek:
> - make QemuSemaphore.count unsigned (it won't be negative)
> - check a return value of in pthread_cond_wait() in qemu_sem_wait()
>
> Signed-off-by: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
> Reviewed-by: Laszlo Ersek <lersek@redhat.com>
> ---
>
> v2:
> - make QemuSemaphore.count unsigned (it won't be negative)
> - also eliminate checks for negative count values
> - check a return value of in pthread_cond_wait() in qemu_sem_wait()
I've compared this patch against v1. I can see one problem near the end:
>
> include/qemu/thread-posix.h | 2 +-
> util/qemu-thread-posix.c | 26 +++++++++++++++-----------
> 2 files changed, 16 insertions(+), 12 deletions(-)
>
> diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h
> index 0f30dcc..361566a 100644
> --- a/include/qemu/thread-posix.h
> +++ b/include/qemu/thread-posix.h
> @@ -15,7 +15,7 @@ struct QemuSemaphore {
> #if defined(__APPLE__) || defined(__NetBSD__)
> pthread_mutex_t lock;
> pthread_cond_t cond;
> - int count;
> + unsigned int count;
> #else
> sem_t sem;
> #endif
> diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
> index 4489abf..44f6f30 100644
> --- a/util/qemu-thread-posix.c
> +++ b/util/qemu-thread-posix.c
> @@ -170,12 +170,11 @@ void qemu_sem_post(QemuSemaphore *sem)
>
> #if defined(__APPLE__) || defined(__NetBSD__)
> pthread_mutex_lock(&sem->lock);
> - if (sem->count == INT_MAX) {
> + if (sem->count == UINT_MAX) {
> rc = EINVAL;
> - } else if (sem->count++ < 0) {
> - rc = pthread_cond_signal(&sem->cond);
> } else {
> - rc = 0;
> + sem->count++;
> + rc = pthread_cond_signal(&sem->cond);
> }
> pthread_mutex_unlock(&sem->lock);
> if (rc != 0) {
> @@ -207,19 +206,21 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
> struct timespec ts;
>
> #if defined(__APPLE__) || defined(__NetBSD__)
> + rc = 0;
> compute_abs_deadline(&ts, ms);
> pthread_mutex_lock(&sem->lock);
> - --sem->count;
> - while (sem->count < 0) {
> + while (sem->count == 0) {
> rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts);
> if (rc == ETIMEDOUT) {
> - ++sem->count;
> break;
> }
> if (rc != 0) {
> error_exit(rc, __func__);
> }
> }
> + if (rc != ETIMEDOUT) {
> + --sem->count;
> + }
> pthread_mutex_unlock(&sem->lock);
> return (rc == ETIMEDOUT ? -1 : 0);
> #else
> @@ -249,16 +250,19 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
>
> void qemu_sem_wait(QemuSemaphore *sem)
> {
> + int rc;
> +
> #if defined(__APPLE__) || defined(__NetBSD__)
> pthread_mutex_lock(&sem->lock);
> - --sem->count;
> - while (sem->count < 0) {
> + while (sem->count == 0) {
> pthread_cond_wait(&sem->cond, &sem->lock);
> + if (rc != 0) {
> + error_exit(rc, __func__);
> + }
You forgot to store the retval of pthread_cond_wait() in "rc", the
(rc!=0) check refers to an indeterminate value.
Otherwise it's good IMO.
Thanks!
Laszlo
> }
> + --sem->count;
> pthread_mutex_unlock(&sem->lock);
> #else
> - int rc;
> -
> do {
> rc = sem_wait(&sem->sem);
> } while (rc == -1 && errno == EINTR);
>
next prev parent reply other threads:[~2013-07-03 8:23 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-02 16:54 [Qemu-devel] [PATCH v2] semaphore: fix a hangup problem under load on NetBSD hosts Izumi Tsutsui
2013-07-03 8:25 ` Laszlo Ersek [this message]
2013-07-03 8:50 ` [Qemu-devel] [PATCH v2] semaphore: fix a hangup problem underload " Izumi Tsutsui
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=51D3DFE8.7080202@redhat.com \
--to=lersek@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=tsutsui@ceres.dti.ne.jp \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).