git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <bonzini@gnu.org>
To: <git@vger.kernel.org>
Cc: j.sixt@viscovery.net
Subject: [RFT PATCH 2/2] win32: optimize pthread_cond_broadcast
Date: Mon,  7 Jun 2010 15:38:12 +0200	[thread overview]
Message-ID: <1275917892-16437-3-git-send-email-bonzini@gnu.org> (raw)
In-Reply-To: <1275917892-16437-1-git-send-email-bonzini@gnu.org>

If there is a single waiting thread, pthread_cond_signal is the
same as pthread_cond_broadcast and no extra synchronization is
necessary.

Signed-off-by: Paolo Bonzini <bonzini@gnu.org>
---
 compat/win32/pthread.c |   19 ++++++++++---------
 1 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c
index 1a38981..d46a51c 100644
--- a/compat/win32/pthread.c
+++ b/compat/win32/pthread.c
@@ -172,9 +172,10 @@ int pthread_cond_broadcast(pthread_cond_t *cond)
 	 * 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)) {
+	if (cond->waiters > 0) {
 		BOOLEAN result;
+		cond->was_broadcast = cond->waiters > 1;
+
 		/* wake up all waiters */
 		result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
 		if (!result)
@@ -187,14 +188,14 @@ int pthread_cond_broadcast(pthread_cond_t *cond)
 		 * yet. For this reason, we can be sure that no thread gets
 		 * a chance to eat *more* than one slice. OTOH, it means
 		 * that the last waiter must send us a wake-up.
+		 *
+		 * As an optimization, when there was exactly one waiter
+		 * broadcast is the same as signal and we can skip this step.
 		 */
-		WaitForSingleObject(cond->continue_broadcast, INFINITE);
-		/*
-		 * Since the external mutex is held, no thread can enter
-		 * cond_wait, and, hence, it is safe to reset this flag
-		 * without cond->waiters_lock held.
-		 */
-		cond->was_broadcast = 0;
+		if (cond->was_broadcast) {
+			WaitForSingleObject(cond->continue_broadcast, INFINITE);
+			cond->was_broadcast = 0;
+		}
 	}
 	return 0;
 }
-- 
1.7.0.1

  parent reply	other threads:[~2010-06-07 13:38 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
2010-06-08 16:27     ` Paolo Bonzini
2010-06-07 13:38 ` Paolo Bonzini [this message]
2010-06-08 16:30   ` [RFT PATCH 2/2] win32: optimize pthread_cond_broadcast 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=1275917892-16437-3-git-send-email-bonzini@gnu.org \
    --to=bonzini@gnu.org \
    --cc=git@vger.kernel.org \
    --cc=j.sixt@viscovery.net \
    /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).