Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 1 | #ifndef _LINUX_PERCPU_RWSEM_H |
| 2 | #define _LINUX_PERCPU_RWSEM_H |
| 3 | |
Oleg Nesterov | 9390ef0 | 2012-12-17 16:01:36 -0800 | [diff] [blame] | 4 | #include <linux/atomic.h> |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 5 | #include <linux/rwsem.h> |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 6 | #include <linux/percpu.h> |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 7 | #include <linux/wait.h> |
Oleg Nesterov | 8ebe347 | 2012-12-17 16:01:38 -0800 | [diff] [blame] | 8 | #include <linux/lockdep.h> |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 9 | |
| 10 | struct percpu_rw_semaphore { |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 11 | unsigned int __percpu *fast_read_ctr; |
Oleg Nesterov | 9390ef0 | 2012-12-17 16:01:36 -0800 | [diff] [blame] | 12 | atomic_t write_ctr; |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 13 | struct rw_semaphore rw_sem; |
| 14 | atomic_t slow_read_ctr; |
| 15 | wait_queue_head_t write_waitq; |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 16 | }; |
| 17 | |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 18 | extern void percpu_down_read(struct percpu_rw_semaphore *); |
Oleg Nesterov | 9287f69 | 2015-07-21 17:45:57 +0200 | [diff] [blame] | 19 | extern int percpu_down_read_trylock(struct percpu_rw_semaphore *); |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 20 | extern void percpu_up_read(struct percpu_rw_semaphore *); |
Mikulas Patocka | 5c1eabe | 2012-10-22 19:37:47 -0400 | [diff] [blame] | 21 | |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 22 | extern void percpu_down_write(struct percpu_rw_semaphore *); |
| 23 | extern void percpu_up_write(struct percpu_rw_semaphore *); |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 24 | |
Oleg Nesterov | 8ebe347 | 2012-12-17 16:01:38 -0800 | [diff] [blame] | 25 | extern int __percpu_init_rwsem(struct percpu_rw_semaphore *, |
| 26 | const char *, struct lock_class_key *); |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 27 | extern void percpu_free_rwsem(struct percpu_rw_semaphore *); |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 28 | |
Oleg Nesterov | 8ebe347 | 2012-12-17 16:01:38 -0800 | [diff] [blame] | 29 | #define percpu_init_rwsem(brw) \ |
| 30 | ({ \ |
| 31 | static struct lock_class_key rwsem_key; \ |
| 32 | __percpu_init_rwsem(brw, #brw, &rwsem_key); \ |
| 33 | }) |
| 34 | |
Oleg Nesterov | 55cc15650 | 2015-07-21 20:26:44 +0200 | [diff] [blame] | 35 | |
| 36 | #define percpu_rwsem_is_held(sem) lockdep_is_held(&(sem)->rw_sem) |
| 37 | |
| 38 | static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem, |
| 39 | bool read, unsigned long ip) |
| 40 | { |
| 41 | lock_release(&sem->rw_sem.dep_map, 1, ip); |
| 42 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
| 43 | if (!read) |
| 44 | sem->rw_sem.owner = NULL; |
| 45 | #endif |
| 46 | } |
| 47 | |
| 48 | static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem, |
| 49 | bool read, unsigned long ip) |
| 50 | { |
| 51 | lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip); |
| 52 | } |
| 53 | |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 54 | #endif |