public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH] 2.4.18-pre9, trylock for read/write semaphores
@ 2002-02-21 18:04 Kendrick M. Smith
  2002-02-21 21:54 ` Brian J. Watson
  0 siblings, 1 reply; 10+ messages in thread
From: Kendrick M. Smith @ 2002-02-21 18:04 UTC (permalink / raw)
  To: bwatson; +Cc: linux-kernel


On Tue Feb 12 2002, 17:45:00 EST, Brian Watson wrote:

> Marcelo-
>
> Attached is a patch that adds trylock routines for read/write
> semaphores. David Howells saw it last August and thought it was ready
> to be submitted then, but I became distracted and haven't taken the
> time to submit it until now. My motivation is that Christoph Hellwig
> says he needs it for JFS.

I just returned from vacation and saw this thread.  I also need trylock()
routines for read-write semaphores for NFS version 4, but you're way ahead
of me: I hadn't even started to implement them yet, and have been working
around the deficiency.  So I would really like to see some variant of this
patch go into the 2.5.x series eventually.  Anything I can do to help out?

Cheers,
 Kendrick Smith
 Center for Information Technology and Integration, University of Michigan


^ permalink raw reply	[flat|nested] 10+ messages in thread
* [PATCH] 2.4.18-pre9, trylock for read/write semaphores
@ 2002-02-12 22:45 bwatson
  2002-02-12 23:29 ` Alan Cox
  2002-02-13  8:19 ` David Howells
  0 siblings, 2 replies; 10+ messages in thread
From: bwatson @ 2002-02-12 22:45 UTC (permalink / raw)
  To: marcelo; +Cc: linux-kernel, dhowells, hch

Marcelo-

Attached is a patch that adds trylock routines for read/write 
semaphores. David Howells saw it last August and thought it was ready 
to be submitted then, but I became distracted and haven't taken the 
time to submit it until now. My motivation is that Christoph Hellwig 
says he needs it for JFS.

I hammered on this patch with a test kernel based on 2.4.18-pre7. The
test kernel put a read/write semaphore under heavy contention with a
mix of down_read(), down_write(), down_read_trylock(), and 
down_write_trylock() calls. I made sure no process got a lock when
it shouldn't, and that no deadlocks occurred. I also made sure it 
applies cleanly against 2.4.18-pre9. Considering that it does not 
affect the rest of the kernel (since no one calls it), I think it's 
safe to include in 2.4.18.


Brian Watson                | "Now I don't know, but I been told it's
Linux Kernel Developer      |  hard to run with the weight of gold,
Open SSI Clustering Project |  Other hand I heard it said, it's
Compaq Computer Corp        |  just as hard with the weight of lead."
Los Angeles, CA             |     -Robert Hunter, 1970

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


diff -Naur linux-2.4.18-pre9/include/asm-i386/rwsem.h trylock-2.4.18-pre9/include/asm-i386/rwsem.h
--- linux-2.4.18-pre9/include/asm-i386/rwsem.h	Tue Feb 12 12:14:14 2002
+++ trylock-2.4.18-pre9/include/asm-i386/rwsem.h	Tue Feb 12 12:14:55 2002
@@ -121,6 +121,24 @@
 }
 
 /*
+ * trylock for reading -- returns 1 if successful, 0 if contention
+ */
+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;
+}
+
+/*
  * lock for writing
  */
 static inline void __down_write(struct rw_semaphore *sem)
@@ -148,6 +166,19 @@
 		: "+d"(tmp), "+m"(sem->count)
 		: "a"(sem)
 		: "memory", "cc");
+}
+
+/*
+ * trylock for writing -- returns 1 if successful, 0 if contention
+ */
+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;
 }
 
 /*
diff -Naur linux-2.4.18-pre9/include/linux/rwsem-spinlock.h trylock-2.4.18-pre9/include/linux/rwsem-spinlock.h
--- linux-2.4.18-pre9/include/linux/rwsem-spinlock.h	Thu Nov 22 11:46:19 2001
+++ trylock-2.4.18-pre9/include/linux/rwsem-spinlock.h	Tue Feb 12 12:14:55 2002
@@ -54,7 +54,9 @@
 
 extern void FASTCALL(init_rwsem(struct rw_semaphore *sem));
 extern void FASTCALL(__down_read(struct rw_semaphore *sem));
+extern int FASTCALL(__down_read_trylock(struct rw_semaphore *sem));
 extern void FASTCALL(__down_write(struct rw_semaphore *sem));
+extern int FASTCALL(__down_write_trylock(struct rw_semaphore *sem));
 extern void FASTCALL(__up_read(struct rw_semaphore *sem));
 extern void FASTCALL(__up_write(struct rw_semaphore *sem));
 
diff -Naur linux-2.4.18-pre9/include/linux/rwsem.h trylock-2.4.18-pre9/include/linux/rwsem.h
--- linux-2.4.18-pre9/include/linux/rwsem.h	Thu Nov 22 11:46:19 2001
+++ trylock-2.4.18-pre9/include/linux/rwsem.h	Tue Feb 12 12:14:55 2002
@@ -46,6 +46,18 @@
 }
 
 /*
+ * trylock for reading -- returns 1 if successful, 0 if contention
+ */
+static inline int down_read_trylock(struct rw_semaphore *sem)
+{
+	int ret;
+	rwsemtrace(sem,"Entering down_read_trylock");
+	ret = __down_read_trylock(sem);
+	rwsemtrace(sem,"Leaving down_read_trylock");
+	return ret;
+}
+
+/*
  * lock for writing
  */
 static inline void down_write(struct rw_semaphore *sem)
@@ -53,6 +65,18 @@
 	rwsemtrace(sem,"Entering down_write");
 	__down_write(sem);
 	rwsemtrace(sem,"Leaving down_write");
+}
+
+/*
+ * trylock for writing -- returns 1 if successful, 0 if contention
+ */
+static inline int down_write_trylock(struct rw_semaphore *sem)
+{
+	int ret;
+	rwsemtrace(sem,"Entering down_write_trylock");
+	ret = __down_write_trylock(sem);
+	rwsemtrace(sem,"Leaving down_write_trylock");
+	return ret;
 }
 
 /*
diff -Naur linux-2.4.18-pre9/lib/rwsem-spinlock.c trylock-2.4.18-pre9/lib/rwsem-spinlock.c
--- linux-2.4.18-pre9/lib/rwsem-spinlock.c	Wed Apr 25 13:31:03 2001
+++ trylock-2.4.18-pre9/lib/rwsem-spinlock.c	Tue Feb 12 12:14:55 2002
@@ -149,6 +149,28 @@
 }
 
 /*
+ * trylock for reading -- returns 1 if successful, 0 if contention
+ */
+int __down_read_trylock(struct rw_semaphore *sem)
+{
+	int ret = 0;
+	rwsemtrace(sem,"Entering __down_read_trylock");
+
+	spin_lock(&sem->wait_lock);
+
+	if (sem->activity>=0 && list_empty(&sem->wait_list)) {
+		/* granted */
+		sem->activity++;
+		ret = 1;
+	}
+
+	spin_unlock(&sem->wait_lock);
+
+	rwsemtrace(sem,"Leaving __down_read_trylock");
+	return ret;
+}
+
+/*
  * get a write lock on the semaphore
  * - note that we increment the waiting count anyway to indicate an exclusive lock
  */
@@ -192,6 +214,28 @@
 
  out:
 	rwsemtrace(sem,"Leaving __down_write");
+}
+
+/*
+ * trylock for writing -- returns 1 if successful, 0 if contention
+ */
+int __down_write_trylock(struct rw_semaphore *sem)
+{
+	int ret = 0;
+	rwsemtrace(sem,"Entering __down_write_trylock");
+
+	spin_lock(&sem->wait_lock);
+
+	if (sem->activity==0 && list_empty(&sem->wait_list)) {
+		/* granted */
+		sem->activity = -1;
+		ret = 1;
+	}
+
+	spin_unlock(&sem->wait_lock);
+
+	rwsemtrace(sem,"Leaving __down_write_trylock");
+	return ret;
 }
 
 /*

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

end of thread, other threads:[~2002-02-21 23:53 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-02-21 18:04 [PATCH] 2.4.18-pre9, trylock for read/write semaphores Kendrick M. Smith
2002-02-21 21:54 ` Brian J. Watson
2002-02-21 23:02   ` Kendrick M. Smith
2002-02-21 23:26     ` Brian J. Watson
  -- strict thread matches above, loose matches on Subject: below --
2002-02-12 22:45 bwatson
2002-02-12 23:29 ` Alan Cox
2002-02-13  1:47   ` Brian J. Watson
2002-02-13  7:47   ` David Howells
2002-02-13  8:19 ` David Howells
2002-02-14  0:13   ` 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