xref: /linux-6.15/include/linux/spinlock_types.h (revision 31552385)
1fb1c8f93SIngo Molnar #ifndef __LINUX_SPINLOCK_TYPES_H
2fb1c8f93SIngo Molnar #define __LINUX_SPINLOCK_TYPES_H
3fb1c8f93SIngo Molnar 
4fb1c8f93SIngo Molnar /*
5fb1c8f93SIngo Molnar  * include/linux/spinlock_types.h - generic spinlock type definitions
6fb1c8f93SIngo Molnar  *                                  and initializers
7fb1c8f93SIngo Molnar  *
8fb1c8f93SIngo Molnar  * portions Copyright 2005, Red Hat, Inc., Ingo Molnar
9fb1c8f93SIngo Molnar  * Released under the General Public License (GPL).
10fb1c8f93SIngo Molnar  */
11fb1c8f93SIngo Molnar 
124f084ca7SThomas Gleixner #include <linux/spinlock_types_raw.h>
13c2f21ce2SThomas Gleixner 
14051790eeSThomas Gleixner #ifndef CONFIG_PREEMPT_RT
15051790eeSThomas Gleixner 
16051790eeSThomas Gleixner /* Non PREEMPT_RT kernels map spinlock to raw_spinlock */
17c2f21ce2SThomas Gleixner typedef struct spinlock {
18c2f21ce2SThomas Gleixner 	union {
19c2f21ce2SThomas Gleixner 		struct raw_spinlock rlock;
20c2f21ce2SThomas Gleixner 
21c2f21ce2SThomas Gleixner #ifdef CONFIG_DEBUG_LOCK_ALLOC
22c2f21ce2SThomas Gleixner # define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
23c2f21ce2SThomas Gleixner 		struct {
24c2f21ce2SThomas Gleixner 			u8 __padding[LOCK_PADSIZE];
25c2f21ce2SThomas Gleixner 			struct lockdep_map dep_map;
26c2f21ce2SThomas Gleixner 		};
27c2f21ce2SThomas Gleixner #endif
28c2f21ce2SThomas Gleixner 	};
29c2f21ce2SThomas Gleixner } spinlock_t;
30c2f21ce2SThomas Gleixner 
31de8f5e4fSPeter Zijlstra #define ___SPIN_LOCK_INITIALIZER(lockname)	\
32de8f5e4fSPeter Zijlstra 	{					\
33de8f5e4fSPeter Zijlstra 	.raw_lock = __ARCH_SPIN_LOCK_UNLOCKED,	\
34de8f5e4fSPeter Zijlstra 	SPIN_DEBUG_INIT(lockname)		\
35de8f5e4fSPeter Zijlstra 	SPIN_DEP_MAP_INIT(lockname) }
36de8f5e4fSPeter Zijlstra 
37c2f21ce2SThomas Gleixner #define __SPIN_LOCK_INITIALIZER(lockname) \
38de8f5e4fSPeter Zijlstra 	{ { .rlock = ___SPIN_LOCK_INITIALIZER(lockname) } }
39c2f21ce2SThomas Gleixner 
40c2f21ce2SThomas Gleixner #define __SPIN_LOCK_UNLOCKED(lockname) \
41c2f21ce2SThomas Gleixner 	(spinlock_t) __SPIN_LOCK_INITIALIZER(lockname)
42c2f21ce2SThomas Gleixner 
43e4d91918SIngo Molnar #define DEFINE_SPINLOCK(x)	spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
44ef12f109SThomas Gleixner 
45051790eeSThomas Gleixner #else /* !CONFIG_PREEMPT_RT */
46051790eeSThomas Gleixner 
47051790eeSThomas Gleixner /* PREEMPT_RT kernels map spinlock to rt_mutex */
48051790eeSThomas Gleixner #include <linux/rtmutex.h>
49051790eeSThomas Gleixner 
50051790eeSThomas Gleixner typedef struct spinlock {
51051790eeSThomas Gleixner 	struct rt_mutex_base	lock;
52051790eeSThomas Gleixner #ifdef CONFIG_DEBUG_LOCK_ALLOC
53051790eeSThomas Gleixner 	struct lockdep_map	dep_map;
54051790eeSThomas Gleixner #endif
55051790eeSThomas Gleixner } spinlock_t;
56051790eeSThomas Gleixner 
57051790eeSThomas Gleixner #define __SPIN_LOCK_UNLOCKED(name)				\
58051790eeSThomas Gleixner 	{							\
59051790eeSThomas Gleixner 		.lock = __RT_MUTEX_BASE_INITIALIZER(name.lock),	\
60051790eeSThomas Gleixner 		SPIN_DEP_MAP_INIT(name)				\
61051790eeSThomas Gleixner 	}
62051790eeSThomas Gleixner 
63*31552385SThomas Gleixner #define __LOCAL_SPIN_LOCK_UNLOCKED(name)			\
64*31552385SThomas Gleixner 	{							\
65*31552385SThomas Gleixner 		.lock = __RT_MUTEX_BASE_INITIALIZER(name.lock),	\
66*31552385SThomas Gleixner 		LOCAL_SPIN_DEP_MAP_INIT(name)			\
67*31552385SThomas Gleixner 	}
68*31552385SThomas Gleixner 
69051790eeSThomas Gleixner #define DEFINE_SPINLOCK(name)					\
70051790eeSThomas Gleixner 	spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
71051790eeSThomas Gleixner 
72051790eeSThomas Gleixner #endif /* CONFIG_PREEMPT_RT */
73051790eeSThomas Gleixner 
74ef12f109SThomas Gleixner #include <linux/rwlock_types.h>
75fb1c8f93SIngo Molnar 
76fb1c8f93SIngo Molnar #endif /* __LINUX_SPINLOCK_TYPES_H */
77