summaryrefslogtreecommitdiffstats
path: root/include/linux/spinlock_rt.h
blob: 61c49b16f69ab03ba053ace72c421385309ad713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-License-Identifier: GPL-2.0-only
#ifndef __LINUX_SPINLOCK_RT_H
#define __LINUX_SPINLOCK_RT_H

#ifndef __LINUX_INSIDE_SPINLOCK_H
#error Do not include directly. Use spinlock.h
#endif

#ifdef CONFIG_DEBUG_LOCK_ALLOC
extern void __rt_spin_lock_init(spinlock_t *lock, const char *name,
				struct lock_class_key *key, bool percpu);
#else
static inline void __rt_spin_lock_init(spinlock_t *lock, const char *name,
				struct lock_class_key *key, bool percpu)
{
}
#endif

#define spin_lock_init(slock)					\
do {								\
	static struct lock_class_key __key;			\
								\
	rt_mutex_base_init(&(slock)->lock);			\
	__rt_spin_lock_init(slock, #slock, &__key, false);	\
} while (0)

#define local_spin_lock_init(slock)				\
do {								\
	static struct lock_class_key __key;			\
								\
	rt_mutex_base_init(&(slock)->lock);			\
	__rt_spin_lock_init(slock, #slock, &__key, true);	\
} while (0)

extern void rt_spin_lock(spinlock_t *lock);
extern void rt_spin_lock_nested(spinlock_t *lock, int subclass);
extern void rt_spin_lock_nest_lock(spinlock_t *lock, struct lockdep_map *nest_lock);
extern void rt_spin_unlock(spinlock_t *lock);
extern void rt_spin_lock_unlock(spinlock_t *lock);
extern int rt_spin_trylock_bh(spinlock_t *lock);
extern int rt_spin_trylock(spinlock_t *lock);

static __always_inline void spin_lock(spinlock_t *lock)
{
	rt_spin_lock(lock);
}

#ifdef CONFIG_LOCKDEP
# define __spin_lock_nested(lock, subclass)				\
	rt_spin_lock_nested(lock, subclass)

# define __spin_lock_nest_lock(lock, nest_lock)				\
	do {								\
		typecheck(struct lockdep_map *, &(nest_lock)->dep_map);	\
		rt_spin_lock_nest_lock(lock, &(nest_lock)->dep_map);	\
	} while (0)
# define __spin_lock_irqsave_nested(lock, flags, subclass)	\
	do {							\
		typecheck(unsigned long, flags);		\
		flags = 0;					\
		__spin_lock_nested(lock, subclass);		\
	} while (0)

#else
 /*
  * Always evaluate the 'subclass' argument to avoid that the compiler
  * warns about set-but-not-used variables when building with
  * CONFIG_DEBUG_LOCK_ALLOC=n and with W=1.
  */
# define __spin_lock_nested(lock, subclass)	spin_lock(((void)(subclass), (lock)))
# define __spin_lock_nest_lock(lock, subclass)	spin_lock(((void)(subclass), (lock)))
# define __spin_lock_irqsave_nested(lock, flags, subclass)	\
	spin_lock_irqsave(((void)(subclass), (lock)), flags)
#endif

#define spin_lock_nested(lock, subclass)		\
	__spin_lock_nested(lock, subclass)

#define spin_lock_nest_lock(lock, nest_lock)		\
	__spin_lock_nest_lock(lock, nest_lock)

#define spin_lock_irqsave_nested(lock, flags, subclass)	\
	__spin_lock_irqsave_nested(lock, flags, subclass)

static __always_inline void spin_lock_bh(spinlock_t *lock)
{
	/* Investigate: Drop bh when blocking ? */
	local_bh_disable();
	rt_spin_lock(lock);
}

static __always_inline void spin_lock_irq(spinlock_t *lock)
{
	rt_spin_lock(lock);
}

#define spin_lock_irqsave(lock, flags)			 \
	do {						 \
		typecheck(unsigned long, flags);	 \
		flags = 0;				 \
		spin_lock(lock);			 \
	} while (0)

static __always_inline void spin_unlock(spinlock_t *lock)
{
	rt_spin_unlock(lock);
}

static __always_inline void spin_unlock_bh(spinlock_t *lock)
{
	rt_spin_unlock(lock);
	local_bh_enable();
}

static __always_inline void spin_unlock_irq(spinlock_t *lock)
{
	rt_spin_unlock(lock);
}

static __always_inline void spin_unlock_irqrestore(spinlock_t *lock,
						   unsigned long flags)
{
	rt_spin_unlock(lock);
}

#define spin_trylock(lock)				\
	__cond_lock(lock, rt_spin_trylock(lock))

#define spin_trylock_bh(lock)				\
	__cond_lock(lock, rt_spin_trylock_bh(lock))

#define spin_trylock_irq(lock)				\
	__cond_lock(lock, rt_spin_trylock(lock))

#define __spin_trylock_irqsave(lock, flags)		\
({							\
	int __locked;					\
							\
	typecheck(unsigned long, flags);		\
	flags = 0;					\
	__locked = spin_trylock(lock);			\
	__locked;					\
})

#define spin_trylock_irqsave(lock, flags)		\
	__cond_lock(lock, __spin_trylock_irqsave(lock, flags))

#define spin_is_contended(lock)		(((void)(lock), 0))

static inline int spin_is_locked(spinlock_t *lock)
{
	return rt_mutex_base_is_locked(&lock->lock);
}

#define assert_spin_locked(lock) BUG_ON(!spin_is_locked(lock))

#include <linux/rwlock_rt.h>

#endif