percpu-rwsem: introduce percpu_down_read_trylock()
authorOleg Nesterov <oleg@redhat.com>
Tue, 21 Jul 2015 15:45:57 +0000 (17:45 +0200)
committerOleg Nesterov <oleg@redhat.com>
Sat, 15 Aug 2015 11:52:10 +0000 (13:52 +0200)
Add percpu_down_read_trylock(), it will have the user soon.

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
include/linux/percpu-rwsem.h
kernel/locking/percpu-rwsem.c

index 3e88c9a7d57f7fdaf5c1e8ee3194df6ae3677f13..16c30cd33501390094c7eec3b4ccf6321c01963f 100644 (file)
@@ -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 *);
index 652a8ee8efe95acaf1db28eeed40bba820787222..f3256725486738878d0f640216a95d91d16359d4 100644 (file)
@@ -88,6 +88,19 @@ void percpu_down_read(struct percpu_rw_semaphore *brw)
        __up_read(&brw->rw_sem);
 }
 
+int percpu_down_read_trylock(struct percpu_rw_semaphore *brw)
+{
+       if (unlikely(!update_fast_ctr(brw, +1))) {
+               if (!__down_read_trylock(&brw->rw_sem))
+                       return 0;
+               atomic_inc(&brw->slow_read_ctr);
+               __up_read(&brw->rw_sem);
+       }
+
+       rwsem_acquire_read(&brw->rw_sem.dep_map, 0, 1, _RET_IP_);
+       return 1;
+}
+
 void percpu_up_read(struct percpu_rw_semaphore *brw)
 {
        rwsem_release(&brw->rw_sem.dep_map, 1, _RET_IP_);