From: Johannes Sixt <j.sixt@viscovery.net>
To: Paolo Bonzini <bonzini@gnu.org>
Cc: git@vger.kernel.org
Subject: Re: [RFT PATCH 1/2] win32: optimize condition variable implementation
Date: Tue, 08 Jun 2010 18:16:02 +0200 [thread overview]
Message-ID: <4C0E6CC2.1080605@viscovery.net> (raw)
In-Reply-To: <1275917892-16437-2-git-send-email-bonzini@gnu.org>
Am 07.06.2010 15:38, schrieb Paolo Bonzini:
> int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex)
> {
> - int last_waiter;
> + int num_waiters;
>
> - EnterCriticalSection(&cond->waiters_lock);
> + /*
> + * This access is protected under the mutex.
> + */
> cond->waiters++;
> - LeaveCriticalSection(&cond->waiters_lock);
>
> /*
> * Unlock external mutex and wait for signal.
> @@ -105,17 +104,17 @@ int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex)
> WaitForSingleObject(cond->sema, INFINITE);
>
> /*
> - * Decrease waiters count. If we are the last waiter, then we must
> + * Decrease waiters count. The mutex prevents concurrent increments,
> + * so doing this decrement atomically is enough.
> + */
> + num_waiters = InterlockedDecrement(&cond->waiters);
> +
> + /* If we are the last waiter, then we must
> * notify the broadcasting thread that it can continue.
> * But if we continued due to cond_signal, we do not have to do that
> * because the signaling thread knows that only one waiter continued.
> */
> - EnterCriticalSection(&cond->waiters_lock);
> - cond->waiters--;
> - last_waiter = cond->was_broadcast&& cond->waiters == 0;
> - LeaveCriticalSection(&cond->waiters_lock);
> -
> - if (last_waiter) {
> + if (num_waiters == 0&& cond->was_broadcast) {
> /*
> * cond_broadcast was issued while mutex was held. This means
> * that all other waiters have continued, but are contending
This is not correct. While it is not possible that two threads increment
waiters at the same time due to the external mutex, it is still possible
that on thread increments, and a different one decrements. You lost all
provisions to avoid that.
Furthermore, waiters_lock not only protects waiters, but also the combined
state of waiters and was_broadcast. You break this protection. See also here:
> @@ -168,12 +168,18 @@ int pthread_cond_signal(pthread_cond_t *cond)
> */
> int pthread_cond_broadcast(pthread_cond_t *cond)
> {
> - EnterCriticalSection(&cond->waiters_lock);
> + /*
> + * As in pthread_cond_signal, access to cond->waiters and
> + * cond->was_broadcast is locked via the external mutex.
> + */
>
> if ((cond->was_broadcast = cond->waiters> 0)) {
> + BOOLEAN result;
> /* wake up all waiters */
> - ReleaseSemaphore(cond->sema, cond->waiters, NULL);
> - LeaveCriticalSection(&cond->waiters_lock);
> + result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
> + if (!result)
> + return err_win_to_posix(GetLastError());
> +
> /*
> * At this point all waiters continue. Each one takes its
> * slice of the semaphor. Now it's our turn to wait: Since
-- Hannes
next prev parent reply other threads:[~2010-06-08 16:16 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-06-07 13:38 [RFT PATCH 0/2] win32: optimize emulation of condition variables Paolo Bonzini
2010-06-07 13:38 ` [RFT PATCH 1/2] win32: optimize condition variable implementation Paolo Bonzini
2010-06-08 16:16 ` Johannes Sixt [this message]
2010-06-08 16:27 ` Paolo Bonzini
2010-06-07 13:38 ` [RFT PATCH 2/2] win32: optimize pthread_cond_broadcast Paolo Bonzini
2010-06-08 16:30 ` Johannes Sixt
2010-06-08 16:37 ` Paolo Bonzini
2010-06-08 18:46 ` Johannes Sixt
2010-06-13 10:16 ` [PATCH 3/2] fix race in win32 pthread_cond_signal causing spurious wakeups Paolo Bonzini
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=4C0E6CC2.1080605@viscovery.net \
--to=j.sixt@viscovery.net \
--cc=bonzini@gnu.org \
--cc=git@vger.kernel.org \
/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).