From: Nicolas Pitre <nico@cam.org>
To: Ingo Molnar <mingo@elte.hu>
Cc: lkml <linux-kernel@vger.kernel.org>,
Arjan van de Ven <arjan@infradead.org>,
Russell King <rmk+lkml@arm.linux.org.uk>
Subject: Re: [patch 00/11] mutex subsystem, -V7
Date: Sat, 24 Dec 2005 00:23:14 -0500 (EST) [thread overview]
Message-ID: <Pine.LNX.4.64.0512240021380.26663@localhost.localdomain> (raw)
In-Reply-To: <Pine.LNX.4.64.0512240009270.26663@localhost.localdomain>
On Sat, 24 Dec 2005, Nicolas Pitre wrote:
> Here's another one, allowing architecture specific trylock
> implementations. New generic xchg-based and ARMv6 implementations are
> provided, while everything else remains the same.
Sorry, the previous patch was missing one file (mutex-dec.h).
Signed-off-by: Nicolas Pitre <nico@cam.org>
Index: linux-2.6/include/asm-generic/mutex-xchg.h
===================================================================
--- linux-2.6.orig/include/asm-generic/mutex-xchg.h
+++ linux-2.6/include/asm-generic/mutex-xchg.h
@@ -69,4 +69,38 @@ do { \
#define __mutex_slowpath_needs_to_unlock() 0
+/**
+ * __mutex_trylock - try to acquire the mutex, without waiting
+ *
+ * @count: pointer of type atomic_t
+ *
+ * Change the count from 1 to a value lower than 1, and return 0 (failure)
+ * if it wasn't 1 originally, or return 1 (success) otherwise. This function
+ * MUST leave the value lower than 1 even when the "1" assertion wasn't true.
+ * Additionally, if the value was < 0 originally, this function must not leave
+ * it to 0 on failure.
+ */
+static inline int
+__mutex_trylock(atomic_t *count)
+{
+ int prev = atomic_xchg(count, 0);
+
+ if (unlikely(prev < 0)) {
+ /*
+ * The lock was marked contended so we must restore that
+ * state. If while doing so we get back a prev value of 1
+ * then we just own it.
+ *
+ * IN all cases this has the potential to trigger the
+ * slowpath for the owner's unlock path - but this is not
+ * a big problem in practice.
+ */
+ prev = atomic_xchg(count, -1);
+ if (prev < 0)
+ prev = 0;
+ }
+
+ return prev;
+}
+
#endif
Index: linux-2.6/include/asm-i386/mutex.h
===================================================================
--- linux-2.6.orig/include/asm-i386/mutex.h
+++ linux-2.6/include/asm-i386/mutex.h
@@ -99,4 +99,38 @@ do { \
#define __mutex_slowpath_needs_to_unlock() 1
+/**
+ * __mutex_trylock - try to acquire the mutex, without waiting
+ *
+ * @count: pointer of type atomic_t
+ *
+ * Change the count from 1 to a value lower than 1, and return 0 (failure)
+ * if it wasn't 1 originally, or return 1 (success) otherwise. This function
+ * MUST leave the value lower than 1 even when the "1" assertion wasn't true.
+ * Additionally, if the value was < 0 originally, this function must not leave
+ * it to 0 on failure.
+ */
+static inline int
+__mutex_trylock(atomic_t *count)
+{
+ /*
+ * We have two variants here. The cmpxchg based one is the best one
+ * because it never induce a false contention state.
+ * If not available we fall back to the atomic_dec_return variant.
+ *
+ * The atomic_dec_return variant might end up making the counter
+ * negative in the failure case, which may trigger the slowpath
+ * for the owner's unlock path - but this is not a big problem
+ * in practice.
+ */
+#ifdef __HAVE_ARCH_CMPXCHG
+ if (likely(atomic_cmpxchg(count, 1, 0)) == 1)
+ return 1;
+#else
+ if (likely(atomic_dec_return(count) == 0))
+ return 1;
+#endif
+ return 0;
+}
+
#endif
Index: linux-2.6/include/asm-x86_64/mutex.h
===================================================================
--- linux-2.6.orig/include/asm-x86_64/mutex.h
+++ linux-2.6/include/asm-x86_64/mutex.h
@@ -71,4 +71,21 @@ do { \
#define __mutex_slowpath_needs_to_unlock() 1
+/**
+ * __mutex_trylock - try to acquire the mutex, without waiting
+ *
+ * @count: pointer of type atomic_t
+ *
+ * Change the count from 1 to 0 and return 1 (success), or return 0 (failure)
+ * if it wasn't 1 originally.
+ */
+static inline int
+__mutex_trylock(atomic_t *count)
+{
+ if (likely(atomic_cmpxchg(count, 1, 0)) == 1)
+ return 1;
+ else
+ return 0;
+}
+
#endif
Index: linux-2.6/kernel/mutex.c
===================================================================
--- linux-2.6.orig/kernel/mutex.c
+++ linux-2.6/kernel/mutex.c
@@ -219,19 +219,6 @@ __mutex_lock_interruptible_nonatomic(str
return -EINTR;
}
-/*
- * We have three mutex_trylock() variants. The cmpxchg based one is
- * the best one (because it has no side-effect on mutex_unlock()),
- * but cmpxchg is not available on every architecture, so we also
- * provide an atomic_dec_return based variant too. The debug variant
- * takes the internal lock.
- *
- * [ The atomic_dec_return variant might end up making the counter
- * negative in the failure case, which may trigger the slowpath
- * for the owner's unlock path - but this is not a big problem
- * in practice. ]
- */
-#ifndef CONFIG_DEBUG_MUTEXES
/***
* mutex_trylock - try acquire the mutex, without waiting
* @lock: the mutex to be acquired
@@ -248,24 +235,13 @@ __mutex_lock_interruptible_nonatomic(str
*/
int fastcall mutex_trylock(struct mutex *lock)
{
-#ifdef __HAVE_ARCH_CMPXCHG
- if (atomic_cmpxchg(&lock->count, 1, 0) == 1)
- return 1;
+#ifndef CONFIG_DEBUG_MUTEXES
+ return __mutex_trylock(&lock->count);
#else
- if (atomic_dec_return(&lock->count) == 0)
- return 1;
-#endif
- return 0;
-}
-
-#else /* CONFIG_DEBUG_MUTEXES: */
-
-/*
- * In the debug case we take the spinlock and check whether we can
- * get the lock:
- */
-int fastcall mutex_trylock(struct mutex *lock)
-{
+ /*
+ * In the debug case we take the spinlock and check whether we can
+ * get the lock:
+ */
struct thread_info *ti = current_thread_info();
int ret = 0;
@@ -280,10 +256,9 @@ int fastcall mutex_trylock(struct mutex
spin_unlock_mutex(&lock->wait_lock);
return ret;
+#endif
}
-#endif /* CONFIG_DEBUG_MUTEXES */
-
/*
* Release the lock, slowpath:
*/
Index: linux-2.6/include/asm-arm/mutex.h
===================================================================
--- linux-2.6.orig/include/asm-arm/mutex.h
+++ linux-2.6/include/asm-arm/mutex.h
@@ -98,5 +98,31 @@ do { \
*/
#define __mutex_slowpath_needs_to_unlock() 1
+/*
+ * For __mutex_trylock we use another construct which could be described
+ * as an "incomplete atomic decrement" or a "single value cmpxchg" since
+ * it has two modes of failure:
+ *
+ * 1) if the exclusive store fails we fail, and
+ *
+ * 2) if the decremented value is not zero we don't even attempt the store.
+ *
+ * This provides the needed trylock semantics like cmpxchg would, but it is
+ * lighter and less generic than a true cmpxchg implementation.
+ */
+static inline int __mutex_trylock(atomic_t *count)
+{
+ int __ex_flag, __res;
+ __asm__ (
+ "ldrex %0, [%2]\n\t"
+ "subs %0, %0, #1\n\t"
+ "strexeq %1, %0, [%2]"
+ : "=&r" (__res), "=&r" (__ex_flag)
+ : "r" (&count->counter)
+ : "cc","memory" );
+ __res |= __ex_flag;
+ return __res == 0;
+}
+
#endif
#endif
Index: linux-2.6/include/asm-generic/mutex-dec.h
===================================================================
--- linux-2.6.orig/include/asm-generic/mutex-dec.h
+++ linux-2.6/include/asm-generic/mutex-dec.h
@@ -63,4 +63,40 @@ do { \
#define __mutex_slowpath_needs_to_unlock() 1
+/**
+ * __mutex_trylock - try to acquire the mutex, without waiting
+ *
+ * @count: pointer of type atomic_t
+ *
+ * Change the count from 1 to a value lower than 1, and return 0 (failure)
+ * if it wasn't 1 originally, or return 1 (success) otherwise. This function
+ * MUST leave the value lower than 1 even when the "1" assertion wasn't true.
+ * Additionally, if the value was < 0 originally, this function must not leave
+ * it to 0 on failure.
+ */
+static inline int
+__mutex_trylock(atomic_t *count)
+{
+ /*
+ * We have two variants here. The cmpxchg based one is the best one
+ * because it never induce a false contention state. It is included
+ * here because architectures using the inc/dec algorithms over the
+ * xchg ones are much more likely to support cmpxchg natively.
+ * If not we fall back to the atomic_dec_return variant.
+ *
+ * The atomic_dec_return variant might end up making the counter
+ * negative in the failure case, which may trigger the slowpath
+ * for the owner's unlock path - but this is not a big problem
+ * in practice.
+ */
+#ifdef __HAVE_ARCH_CMPXCHG
+ if (likely(atomic_cmpxchg(count, 1, 0)) == 1)
+ return 1;
+#else
+ if (likely(atomic_dec_return(count) == 0))
+ return 1;
+#endif
+ return 0;
+}
+
#endif
next prev parent reply other threads:[~2005-12-24 5:23 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-12-23 16:16 Ingo Molnar
2005-12-24 5:15 ` Nicolas Pitre
2005-12-24 5:23 ` Nicolas Pitre [this message]
2005-12-26 19:24 ` Nicolas Pitre
2005-12-26 19:25 ` [patch 1/3] mutex subsystem: trylock Nicolas Pitre
2005-12-27 11:51 ` Ingo Molnar
2005-12-27 20:47 ` Nicolas Pitre
2005-12-28 7:48 ` Ingo Molnar
2005-12-28 8:13 ` Ingo Molnar
2005-12-28 16:29 ` Nicolas Pitre
2005-12-28 17:09 ` Ingo Molnar
2005-12-27 12:05 ` Arjan van de Ven
2005-12-27 13:15 ` Ingo Molnar
2005-12-29 4:06 ` Nicolas Pitre
2005-12-29 8:33 ` Ingo Molnar
2005-12-29 9:01 ` Nick Piggin
2005-12-29 17:15 ` Nicolas Pitre
2005-12-30 2:05 ` Nick Piggin
2005-12-29 16:46 ` Nicolas Pitre
2005-12-29 3:22 ` Nicolas Pitre
2005-12-26 19:25 ` [patch 2/3] mutex subsystem: fastpath inlining Nicolas Pitre
2005-12-27 11:55 ` Ingo Molnar
2005-12-27 21:59 ` Nicolas Pitre
2005-12-28 7:41 ` Ingo Molnar
2005-12-29 2:53 ` Nicolas Pitre
2005-12-29 8:41 ` Ingo Molnar
2006-01-06 21:20 ` Nicolas Pitre
2005-12-26 19:26 ` [patch 3/3] mutex subsystem: inline mutex_is_locked() Nicolas Pitre
2005-12-27 11:37 ` Ingo Molnar
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:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.64.0512240021380.26663@localhost.localdomain \
--to=nico@cam.org \
--cc=arjan@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=rmk+lkml@arm.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®