public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Brian J. Watson" <Brian.J.Watson@compaq.com>
To: bcrl@redhat.com
Cc: Linux Kernel <linux-kernel@vger.kernel.org>
Subject: [PATCH] read/write semaphore trylock routines - 2.4.6
Date: Mon, 09 Jul 2001 20:32:13 -0700	[thread overview]
Message-ID: <3B4A773D.50632896@compaq.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1110 bytes --]

Ben-

A few months ago, I sent you a couple of trylock routines for
read/write semaphores. Here's an updated version for 2.4.6. We use
them for deadlock avoidance in the clustering work we're doing. We
thought that others might be able to use them, as well.

A caveat for these trylock routines is that they use the cmpxchg
instruction, which limits their usefulness on x86 to 486 and above. I
noticed that the RISC architectures, however, have no problem rigging
up a cmpxchg() equivalent (at least Alpha and PPC don't).

Please let me know if you have any objections to folding them in with
the rest of the read/write semaphore implementation.

Thanks.

-- 
Brian Watson               | "The common people of England... so 
Linux Kernel Developer     |  jealous of their liberty, but like the 
SSI Clustering Laboratory  |  common people of most other countries 
Compaq Computer Corp       |  never rightly considering wherein it 
Los Angeles, CA            |  consists..."
                           |      -Adam Smith, Wealth of Nations, 1776

mailto:Brian.J.Watson@compaq.com
http://opensource.compaq.com/

[-- Attachment #2: patch-rwsem-trylock --]
[-- Type: text/plain, Size: 1168 bytes --]

Index: include/asm-i386/rwsem.h
===================================================================
RCS file: /src/nsc_linux/src/kernel/include/asm-i386/rwsem.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 rwsem.h
--- include/asm-i386/rwsem.h	2001/04/27 22:48:24	1.1.1.1
+++ include/asm-i386/rwsem.h	2001/07/06 20:54:13
@@ -222,5 +222,34 @@
 	return tmp+delta;
 }
 
+#if __HAVE_ARCH_CMPXCHG
+/* returns 1 if it successfully obtained the semaphore for write */
+static inline int down_write_trylock(struct rw_semaphore *sem)
+{
+	signed long ret = cmpxchg(&sem->count,
+				  RWSEM_UNLOCKED_VALUE, 
+				  RWSEM_ACTIVE_WRITE_BIAS);
+	if (ret == RWSEM_UNLOCKED_VALUE)
+		return 1;
+	return 0;
+}
+
+/* returns 1 if it successfully obtained the semaphore for read */
+static inline int down_read_trylock(struct rw_semaphore *sem)
+{
+	signed long old, new;
+
+repeat:
+	old = (volatile signed long)sem->count;
+	if (old < RWSEM_UNLOCKED_VALUE)
+		return 0;
+	new = old + RWSEM_ACTIVE_READ_BIAS;
+	if (cmpxchg(&sem->count, old, new) == old)
+		return 1;
+	else
+		goto repeat;
+}
+#endif /* __HAVE_ARCH_CMPXCHG */
+
 #endif /* __KERNEL__ */
 #endif /* _I386_RWSEM_H */

             reply	other threads:[~2001-07-10  3:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-10  3:32 Brian J. Watson [this message]
2001-07-10  7:32 ` [PATCH] read/write semaphore trylock routines - 2.4.6 David Howells
2001-07-11  3:20   ` Brian J. Watson
2001-07-18 23:22     ` Brian J. Watson
2001-07-19  7:41       ` David Howells
2001-07-25 17:56         ` [BUG 2.4.7] enabling RWSEM_DEBUG Brian J. Watson
2001-07-26 13:28           ` David Howells
2001-07-26 16:13           ` David Howells
2001-07-26 21:21             ` Brian J. Watson

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=3B4A773D.50632896@compaq.com \
    --to=brian.j.watson@compaq.com \
    --cc=bcrl@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    /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