public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] read/write semaphore trylock routines - 2.4.6
@ 2001-07-10  3:32 Brian J. Watson
  2001-07-10  7:32 ` David Howells
  0 siblings, 1 reply; 9+ messages in thread
From: Brian J. Watson @ 2001-07-10  3:32 UTC (permalink / raw)
  To: bcrl; +Cc: Linux Kernel

[-- 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 */

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

end of thread, other threads:[~2001-07-26 21:35 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2001-07-10  3:32 [PATCH] read/write semaphore trylock routines - 2.4.6 Brian J. Watson
2001-07-10  7:32 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox