@@ -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 *);
@@ -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_);
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@gmail.com> --- include/linux/percpu-rwsem.h | 1 + lib/percpu-rwsem.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+)