[PATCH 5/6] futex: handle timeout inside adaptive lock spin

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Darren Hart
Date: Monday, April 5, 2010 - 1:23 pm

Signed-off-by: Darren Hart <dvhltc@us.ibm.com>
---
 kernel/futex.c |   24 +++++++++++++++++++++---
 1 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index c33ac2a..af61dcd 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -2385,6 +2385,7 @@ out:
 /**
  * trylock_futex_adaptive() - Try to acquire the futex lock in a busy loop
  * @uaddr: the futex user address
+ * @timeout: absolute timeout or NULL if none
  *
  * Try to acquire a futex lock in a loop until the owner changes or the owner
  * is descheduled. To lock the futex, set the value to the current TID.
@@ -2394,10 +2395,11 @@ out:
  *  1 - Futex lock acquired
  * <0 - On error
  */
-static int trylock_futex_adaptive(u32 __user *uaddr)
+static int trylock_futex_adaptive(u32 __user *uaddr, ktime_t *timeout)
 {
 	int ret = 0;
 	u32 curval;
+	ktime_t now;
 
 	for (;;) {
 		struct thread_info *owner;
@@ -2433,6 +2435,22 @@ static int trylock_futex_adaptive(u32 __user *uaddr)
 		if (need_resched())
 			break;
 
+		if (timeout) {
+			now = ktime_get();
+/* FIXME: consider creating ktime_less_than(lhs, rhs) */
+#if (BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)
+			if (timeout->tv64 < now.tv64)
+				break;
+#else
+			if (timeout->sec < now.sec ||
+			    (timeout->sec == now.sec &&
+			     timeout->nsec < now.nsec)) {
+				ret = -ETIMEDOUT;
+				break;
+			}
+#endif
+		}
+
 		cpu_relax();
 	}
 	return ret;
@@ -2480,7 +2498,7 @@ retry:
 #ifdef CONFIG_SMP
 	if (flags & FLAGS_ADAPTIVE) {
 		preempt_disable();
-		ret = trylock_futex_adaptive(uaddr);
+		ret = trylock_futex_adaptive(uaddr, time);
 		preempt_enable();
 
 		/* We got the lock. */
@@ -2489,7 +2507,7 @@ retry:
 			goto out;
 		}
 
-		/* We encountered an error, -EFAULT most likely. */
+		/* We encountered an error, -EFAULT or -ETIMEDOUT */
 		if (ret)
 			goto out;
 	}
-- 
1.6.3.3

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 2/6] futex: add futex_q static initializer, Darren Hart, (Mon Apr 5, 1:23 pm)
[PATCH 3/6] futex: refactor futex_lock_pi_atomic, Darren Hart, (Mon Apr 5, 1:23 pm)
[PATCH 5/6] futex: handle timeout inside adaptive lock spin, Darren Hart, (Mon Apr 5, 1:23 pm)
[No subject], Ulrich Drepper, (Tue Apr 6, 7:47 am)
[No subject], Peter Zijlstra, (Tue Apr 6, 7:51 am)
[No subject], Darren Hart, (Tue Apr 6, 8:28 am)
[No subject], Peter Zijlstra, (Tue Apr 6, 8:29 am)
[No subject], Darren Hart, (Tue Apr 6, 8:33 am)
[No subject], Peter Zijlstra, (Tue Apr 6, 8:37 am)
[No subject], Avi Kivity, (Tue Apr 6, 9:06 am)
[No subject], Avi Kivity, (Tue Apr 6, 9:10 am)
[No subject], Thomas Gleixner, (Tue Apr 6, 9:14 am)
[No subject], Avi Kivity, (Tue Apr 6, 9:20 am)
[No subject], Alan Cox, (Tue Apr 6, 9:44 am)
[No subject], Alan Cox, (Tue Apr 6, 9:53 am)
[No subject], Alan Cox, (Tue Apr 6, 9:54 am)
[No subject], Thomas Gleixner, (Tue Apr 6, 9:55 am)
[No subject], Ulrich Drepper, (Tue Apr 6, 10:34 am)