public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Brian J. Watson" <Brian.J.Watson@compaq.com>
To: David Howells <dhowells@redhat.com>
Cc: Linux Kernel <linux-kernel@vger.kernel.org>
Subject: [PATCH] read/write semaphore trylock routines - 2.4.6
Date: Wed, 18 Jul 2001 16:22:56 -0700	[thread overview]
Message-ID: <3B561A50.A1B35FBC@compaq.com> (raw)
In-Reply-To: <16572.994750374@warthog.cambridge.redhat.com> <3B4BC60E.85C12357@compaq.com>

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

"Brian J. Watson" wrote:
> I've added the generic spinlock versions. I've also renamed the i386
> versions to __down_*_trylock() and added stubs to
> include/linux/rwsem.h, as you did for the up_*() and down_*()
> routines.
> 
> The generic versions were compiled but not tested. Probably not a big
> deal since I snagged most of the code from your __down_*() routines.
> 

David-

Is this patch ready to go into the kernel? Is there anything else I
should do to get it ready? I noticed it didn't make it into
2.4.7-pre7.

-- 
Brian Watson             | "The common people of England... so 
Linux Kernel Developer   |  jealous of their liberty, but like the 
Open SSI Clustering Lab  |  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: 4197 bytes --]

diff -ur linux/include/asm-i386/rwsem.h rwsem_trylock/include/asm-i386/rwsem.h
--- linux/include/asm-i386/rwsem.h	Fri Apr 27 15:48:24 2001
+++ rwsem_trylock/include/asm-i386/rwsem.h	Tue Jul 10 19:32:05 2001
@@ -117,6 +117,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)
@@ -141,6 +159,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 -ur linux/include/linux/rwsem-spinlock.h rwsem_trylock/include/linux/rwsem-spinlock.h
--- linux/include/linux/rwsem-spinlock.h	Wed Jul  4 14:20:44 2001
+++ rwsem_trylock/include/linux/rwsem-spinlock.h	Tue Jul 10 19:32:05 2001
@@ -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 -ur linux/include/linux/rwsem.h rwsem_trylock/include/linux/rwsem.h
--- linux/include/linux/rwsem.h	Wed Jul  4 14:20:44 2001
+++ rwsem_trylock/include/linux/rwsem.h	Tue Jul 10 19:32:05 2001
@@ -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 -ur linux/lib/rwsem-spinlock.c rwsem_trylock/lib/rwsem-spinlock.c
--- linux/lib/rwsem-spinlock.c	Wed Apr 25 13:31:03 2001
+++ rwsem_trylock/lib/rwsem-spinlock.c	Tue Jul 10 18:59:24 2001
@@ -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++;
+		spin_unlock(&sem->wait_lock);
+		ret = 1;
+	}
+
+	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;
+		spin_unlock(&sem->wait_lock);
+		ret = 1;
+	}
+
+	rwsemtrace(sem,"Leaving __down_write_trylock");
+	return ret;
 }
 
 /*

  reply	other threads:[~2001-07-18 23:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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=3B561A50.A1B35FBC@compaq.com \
    --to=brian.j.watson@compaq.com \
    --cc=dhowells@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