xref: /linux/include/linux/rwbase_rt.h (revision f70405afc99b1e5a3a1e60b6c05456fde2dbe622)
1943f0edbSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2943f0edbSThomas Gleixner #ifndef _LINUX_RWBASE_RT_H
3943f0edbSThomas Gleixner #define _LINUX_RWBASE_RT_H
4943f0edbSThomas Gleixner 
5943f0edbSThomas Gleixner #include <linux/rtmutex.h>
6943f0edbSThomas Gleixner #include <linux/atomic.h>
7943f0edbSThomas Gleixner 
8943f0edbSThomas Gleixner #define READER_BIAS		(1U << 31)
9943f0edbSThomas Gleixner #define WRITER_BIAS		(1U << 30)
10943f0edbSThomas Gleixner 
11943f0edbSThomas Gleixner struct rwbase_rt {
12943f0edbSThomas Gleixner 	atomic_t		readers;
13943f0edbSThomas Gleixner 	struct rt_mutex_base	rtmutex;
14943f0edbSThomas Gleixner };
15943f0edbSThomas Gleixner 
16943f0edbSThomas Gleixner #define __RWBASE_INITIALIZER(name)				\
17943f0edbSThomas Gleixner {								\
18943f0edbSThomas Gleixner 	.readers = ATOMIC_INIT(READER_BIAS),			\
19943f0edbSThomas Gleixner 	.rtmutex = __RT_MUTEX_BASE_INITIALIZER(name.rtmutex),	\
20943f0edbSThomas Gleixner }
21943f0edbSThomas Gleixner 
22943f0edbSThomas Gleixner #define init_rwbase_rt(rwbase)					\
23943f0edbSThomas Gleixner 	do {							\
24943f0edbSThomas Gleixner 		rt_mutex_base_init(&(rwbase)->rtmutex);		\
25943f0edbSThomas Gleixner 		atomic_set(&(rwbase)->readers, READER_BIAS);	\
26943f0edbSThomas Gleixner 	} while (0)
27943f0edbSThomas Gleixner 
28943f0edbSThomas Gleixner 
29*f70405afSMatthew Wilcox (Oracle) static __always_inline bool rw_base_is_locked(const struct rwbase_rt *rwb)
30943f0edbSThomas Gleixner {
31943f0edbSThomas Gleixner 	return atomic_read(&rwb->readers) != READER_BIAS;
32943f0edbSThomas Gleixner }
33943f0edbSThomas Gleixner 
34*f70405afSMatthew Wilcox (Oracle) static inline void rw_base_assert_held_write(const struct rwbase_rt *rwb)
35*f70405afSMatthew Wilcox (Oracle) {
36*f70405afSMatthew Wilcox (Oracle) 	WARN_ON(atomic_read(&rwb->readers) != WRITER_BIAS);
37*f70405afSMatthew Wilcox (Oracle) }
38*f70405afSMatthew Wilcox (Oracle) 
39*f70405afSMatthew Wilcox (Oracle) static __always_inline bool rw_base_is_contended(const struct rwbase_rt *rwb)
40943f0edbSThomas Gleixner {
41943f0edbSThomas Gleixner 	return atomic_read(&rwb->readers) > 0;
42943f0edbSThomas Gleixner }
43943f0edbSThomas Gleixner 
44943f0edbSThomas Gleixner #endif /* _LINUX_RWBASE_RT_H */
45