blob: c2fa3ecb0dce57dde9ad4a92a35dc4178d7a159e [file] [log] [blame]
Mikulas Patocka62ac6652012-09-26 07:46:43 +02001#ifndef _LINUX_PERCPU_RWSEM_H
2#define _LINUX_PERCPU_RWSEM_H
3
Oleg Nesterov9390ef02012-12-17 16:01:36 -08004#include <linux/atomic.h>
Oleg Nesterova1fd3e22012-12-17 16:01:32 -08005#include <linux/rwsem.h>
Mikulas Patocka62ac6652012-09-26 07:46:43 +02006#include <linux/percpu.h>
Oleg Nesterova1fd3e22012-12-17 16:01:32 -08007#include <linux/wait.h>
Oleg Nesterov001dac62015-08-21 19:42:57 +02008#include <linux/rcu_sync.h>
Oleg Nesterov8ebe3472012-12-17 16:01:38 -08009#include <linux/lockdep.h>
Mikulas Patocka62ac6652012-09-26 07:46:43 +020010
11struct percpu_rw_semaphore {
Oleg Nesterov001dac62015-08-21 19:42:57 +020012 struct rcu_sync rss;
Oleg Nesterova1fd3e22012-12-17 16:01:32 -080013 unsigned int __percpu *fast_read_ctr;
Oleg Nesterova1fd3e22012-12-17 16:01:32 -080014 struct rw_semaphore rw_sem;
15 atomic_t slow_read_ctr;
16 wait_queue_head_t write_waitq;
Mikulas Patocka62ac6652012-09-26 07:46:43 +020017};
18
Oleg Nesterova1fd3e22012-12-17 16:01:32 -080019extern void percpu_down_read(struct percpu_rw_semaphore *);
Oleg Nesterov9287f692015-07-21 17:45:57 +020020extern int percpu_down_read_trylock(struct percpu_rw_semaphore *);
Oleg Nesterova1fd3e22012-12-17 16:01:32 -080021extern void percpu_up_read(struct percpu_rw_semaphore *);
Mikulas Patocka5c1eabe2012-10-22 19:37:47 -040022
Oleg Nesterova1fd3e22012-12-17 16:01:32 -080023extern void percpu_down_write(struct percpu_rw_semaphore *);
24extern void percpu_up_write(struct percpu_rw_semaphore *);
Mikulas Patocka62ac6652012-09-26 07:46:43 +020025
Oleg Nesterov8ebe3472012-12-17 16:01:38 -080026extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
27 const char *, struct lock_class_key *);
Oleg Nesterova1fd3e22012-12-17 16:01:32 -080028extern void percpu_free_rwsem(struct percpu_rw_semaphore *);
Mikulas Patocka62ac6652012-09-26 07:46:43 +020029
Oleg Nesterov8ebe3472012-12-17 16:01:38 -080030#define percpu_init_rwsem(brw) \
31({ \
32 static struct lock_class_key rwsem_key; \
33 __percpu_init_rwsem(brw, #brw, &rwsem_key); \
34})
35
Oleg Nesterov55cc156502015-07-21 20:26:44 +020036
37#define percpu_rwsem_is_held(sem) lockdep_is_held(&(sem)->rw_sem)
38
39static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
40 bool read, unsigned long ip)
41{
42 lock_release(&sem->rw_sem.dep_map, 1, ip);
43#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
44 if (!read)
45 sem->rw_sem.owner = NULL;
46#endif
47}
48
49static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
50 bool read, unsigned long ip)
51{
52 lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);
53}
54
Mikulas Patocka62ac6652012-09-26 07:46:43 +020055#endif