Skip to contentSkip to conversation

sched: completion: lock-free checking of the blocking case

RFC PATCH New Nicholas Mc Guire 1 participant 1 message Jan 24, 2015 Scheduler

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 sched: completion: lock-free checking of the blocking case

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

---

v2: return type is boot thus return should be false not 0 here
Reported-by kbuild test robot <[email protected]>

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(+)
kernel/sched/completion.c +9 -0
index 7c5cd70..5f7cf31 100644
@@ -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 false;
+
 	spin_lock_irqsave(&x->wait.lock, flags);
 	if (!x->done)
 		ret = 0;
Signature · 1 line
1.7.10.4