LKML Archive on
help / color / mirror / Atom feed
From: Nicholas Mc Guire <>
To: Ingo Molnar <>
Cc: Peter Zijlstra <>,,
	Nicholas Mc Guire <>
Subject: [PATCH RFC] sched: completion: lock-free checking of the blocking case
Date: Fri, 23 Jan 2015 12:41:47 +0100	[thread overview]
Message-ID: <> (raw)

The "thread would block" case can be checked without grabbing the lock

Signed-off-by: Nicholas Mc Guire <>

If the check does not return early then grab the lock and recheck.
A memory barrier is not needed as complete() and complete_all() imply
a barrier.

The ACCESS_ONCE is needed for calls in a loop that, if inlined, could
optimize out the re-fetching of x->done.

 kernel/sched/completion.c |    9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/kernel/sched/completion.c b/kernel/sched/completion.c
index 7c5cd70..5f7cf31 100644
--- a/kernel/sched/completion.c
+++ b/kernel/sched/completion.c
@@ -268,6 +268,15 @@ bool try_wait_for_completion(struct completion *x)
 	unsigned long flags;
 	int ret = 1;
+	/*
+	 * Since x->done will need to be locked only
+	 * in the non-blocking case, we check x->done
+	 * first without taking the lock so we can
+	 * return early in the blocking case.
+	 */
+	if (!ACCESS_ONCE(x->done))
+		return 0;
 	spin_lock_irqsave(&x->wait.lock, flags);
 	if (!x->done)
 		ret = 0;

             reply	other threads:[~2015-01-23 11:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-23 11:41 Nicholas Mc Guire [this message]
2015-02-04 14:38 ` [tip:locking/core] sched/completion: Add " tip-bot for Nicholas Mc Guire

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:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \
    --subject='Re: [PATCH RFC] sched: completion: lock-free checking of the blocking case' \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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).