xref: /linux/include/linux/rwbase_rt.h (revision 3a07362fab1653d3aca31a9155c8cc776138fd02)
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 
rw_base_is_locked(const struct rwbase_rt * rwb)29f70405afSMatthew 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 
rw_base_is_write_locked(const struct rwbase_rt * rwb)34*fa1f5116SSebastian Andrzej Siewior static __always_inline bool rw_base_is_write_locked(const struct rwbase_rt *rwb)
35f70405afSMatthew Wilcox (Oracle) {
36*fa1f5116SSebastian Andrzej Siewior 	return atomic_read(&rwb->readers) == WRITER_BIAS;
37f70405afSMatthew Wilcox (Oracle) }
38f70405afSMatthew Wilcox (Oracle) 
rw_base_is_contended(const struct rwbase_rt * rwb)39f70405afSMatthew 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