[RFC v2 2/6] percpu_rw_semaphore: add percpu_down_read_trylock()

David Herrmann dh.herrmann at gmail.com
Tue Oct 22 00:33:24 CEST 2013


This is basically a copy of percpu_down_read() but does not sleep if a
writer is already active. Same semantics as classic down_read_trylock().

Signed-off-by: David Herrmann <dh.herrmann at gmail.com>
---
 include/linux/percpu-rwsem.h |  1 +
 lib/percpu-rwsem.c           | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/include/linux/percpu-rwsem.h b/include/linux/percpu-rwsem.h
index 3e88c9a..2f752bd 100644
--- a/include/linux/percpu-rwsem.h
+++ b/include/linux/percpu-rwsem.h
@@ -16,6 +16,7 @@ struct percpu_rw_semaphore {
 };
 
 extern void percpu_down_read(struct percpu_rw_semaphore *);
+extern int percpu_down_read_trylock(struct percpu_rw_semaphore *);
 extern void percpu_up_read(struct percpu_rw_semaphore *);
 
 extern void percpu_down_write(struct percpu_rw_semaphore *);
diff --git a/lib/percpu-rwsem.c b/lib/percpu-rwsem.c
index 893586c..596f44b 100644
--- a/lib/percpu-rwsem.c
+++ b/lib/percpu-rwsem.c
@@ -92,6 +92,26 @@ void percpu_down_read(struct percpu_rw_semaphore *brw)
 }
 EXPORT_SYMBOL(percpu_down_read);
 
+int percpu_down_read_trylock(struct percpu_rw_semaphore *brw)
+{
+	int r;
+
+	if (likely(update_fast_ctr(brw, +1))) {
+		rwsem_acquire_read(&brw->rw_sem.dep_map, 0, 0, _RET_IP_);
+		return 1;
+	}
+
+	r = down_read_trylock(&brw->rw_sem);
+	if (r) {
+		atomic_inc(&brw->slow_read_ctr);
+		/* avoid up_read()->rwsem_release() */
+		__up_read(&brw->rw_sem);
+	}
+
+	return r;
+}
+EXPORT_SYMBOL(percpu_down_read_trylock);
+
 void percpu_up_read(struct percpu_rw_semaphore *brw)
 {
 	rwsem_release(&brw->rw_sem.dep_map, 1, _RET_IP_);
-- 
1.8.4.1



More information about the dri-devel mailing list