]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/percpu-rwsem.h
Update bcachefs sources to 9abf628c70 bcachefs: Fix a spurious error in fsck
[bcachefs-tools-debian] / include / linux / percpu-rwsem.h
1
2 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef _LINUX_PERCPU_RWSEM_H
4 #define _LINUX_PERCPU_RWSEM_H
5
6 #include <pthread.h>
7 #include <linux/preempt.h>
8
9 struct percpu_rw_semaphore {
10         pthread_rwlock_t        lock;
11 };
12
13 #define DEFINE_STATIC_PERCPU_RWSEM(name)                                \
14 static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name);          \
15 static struct percpu_rw_semaphore name = {                              \
16         .rss = __RCU_SYNC_INITIALIZER(name.rss, RCU_SCHED_SYNC),        \
17         .read_count = &__percpu_rwsem_rc_##name,                        \
18         .rw_sem = __RWSEM_INITIALIZER(name.rw_sem),                     \
19         .writer = __RCUWAIT_INITIALIZER(name.writer),                   \
20 }
21
22 extern int __percpu_down_read(struct percpu_rw_semaphore *, int);
23 extern void __percpu_up_read(struct percpu_rw_semaphore *);
24
25 static inline void percpu_down_read_preempt_disable(struct percpu_rw_semaphore *sem)
26 {
27         pthread_rwlock_rdlock(&sem->lock);
28         preempt_disable();
29 }
30
31 static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
32 {
33         pthread_rwlock_rdlock(&sem->lock);
34 }
35
36 static inline int percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
37 {
38         return !pthread_rwlock_tryrdlock(&sem->lock);
39 }
40
41 static inline void percpu_up_read_preempt_enable(struct percpu_rw_semaphore *sem)
42 {
43         preempt_enable();
44         pthread_rwlock_unlock(&sem->lock);
45 }
46
47 static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
48 {
49         pthread_rwlock_unlock(&sem->lock);
50 }
51
52 static inline void percpu_down_write(struct percpu_rw_semaphore *sem)
53 {
54         pthread_rwlock_wrlock(&sem->lock);
55 }
56
57 static inline void percpu_up_write(struct percpu_rw_semaphore *sem)
58 {
59         pthread_rwlock_unlock(&sem->lock);
60 }
61
62 static inline void percpu_free_rwsem(struct percpu_rw_semaphore *sem) {}
63
64 static inline int percpu_init_rwsem(struct percpu_rw_semaphore *sem)
65 {
66         pthread_rwlock_init(&sem->lock, NULL);
67         return 0;
68 }
69
70 #define percpu_rwsem_assert_held(sem)           do {} while (0)
71
72 #endif