Skip to content

Commit

Permalink
sched/completion: Add lock-free checking of the blocking case
Browse files Browse the repository at this point in the history
The "thread would block" case can be checked without grabbing ->wait.lock.

[ 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. ]

Signed-off-by: Nicholas Mc Guire <der.herr@hofr.at>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/1422013307-13200-1-git-send-email-der.herr@hofr.at
Signed-off-by: Ingo Molnar <mingo@kernel.org>
  • Loading branch information
hofrat authored and Ingo Molnar committed Feb 4, 2015
1 parent de30ec4 commit 7c34e31
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions kernel/sched/completion.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7c34e31

Please sign in to comment.