public inbox for libc-ports@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Avoid unnecessary busy loop in __lll_timedlock_wait on ARM.
@ 2013-01-31  8:42 katsuki.uwatoko
  2013-02-07 23:33 ` Joseph S. Myers
  0 siblings, 1 reply; 11+ messages in thread
From: katsuki.uwatoko @ 2013-01-31  8:42 UTC (permalink / raw)
  To: libc-ports

Hi,

I have found an issue in __lll_timedlock_wait on ARM.

The following sequence causes unnecessary busy loop. 

"A thread" gets the lock. (futex = 1)
"B thread" tries to get the lock, and has not called futex syscall yet. (futex = 2)
"A thread" releases the lock (futex = 0)
"C thread" gets the lock, and does not call futex syscall because of futex=0 (futex = 1)
"B thread" calls futex syscall, and returns with an error.
           Because futex syscall in Linux Kernel checks if the val is changed 
           from 2, which is the 3rd arg of the syscall at futex_wait_setup(),
           but in this case futex is 1.
"B thread" tries to get the lock in userspace but cannot get it 
           because futex is not 0.
           After all the thread keeps calling futex syscall 
           until "C thread" will release it (futex = 0) or timeout.

Therefore I think that the value should be set 2 in every loop 
the same as __lll_lock_wait_private, and attached a patch for this issue.
-- 
UWATOKO Katsuki

---
 .../unix/sysv/linux/arm/nptl/lowlevellock.c        |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/ports/sysdeps/unix/sysv/linux/arm/nptl/lowlevellock.c b/ports/sysdeps/unix/sysv/linux/arm/nptl/lowlevellock.c
index 756f39f..a495d85 100644
--- a/ports/sysdeps/unix/sysv/linux/arm/nptl/lowlevellock.c
+++ b/ports/sysdeps/unix/sysv/linux/arm/nptl/lowlevellock.c
@@ -65,6 +65,7 @@ __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
   do
     {
       struct timeval tv;
+      int oldval;
 
       /* Get the current time.  */
       (void) __gettimeofday (&tv, NULL);
@@ -82,8 +83,10 @@ __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
       if (rt.tv_sec < 0)
 	return ETIMEDOUT;
 
+      oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
       // XYZ: Lost the lock to check whether it was private.
-      lll_futex_timed_wait (futex, 2, &rt, private);
+      if (oldval != 0)
+	lll_futex_timed_wait (futex, 2, &rt, private);
     }
   while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
 
-- 
1.7.4.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-02-12 21:57 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-31  8:42 [PATCH] Avoid unnecessary busy loop in __lll_timedlock_wait on ARM katsuki.uwatoko
2013-02-07 23:33 ` Joseph S. Myers
2013-02-08  0:13   ` katsuki.uwatoko
2013-02-09  4:18   ` Carlos O'Donell
2013-02-09  6:49     ` David Miller
2013-02-09 14:56       ` Carlos O'Donell
2013-02-10  4:56         ` David Miller
2013-02-10 17:55           ` Carlos O'Donell
2013-02-12 21:41             ` Daniel Jacobowitz
2013-02-12 21:41               ` Daniel Jacobowitz
2013-02-12 21:57               ` Carlos O'Donell

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 read-only IMAP folder(s) and NNTP newsgroup(s).