1 #ifndef __LINUX_RWLOCK_H
2 #define __LINUX_RWLOCK_H
3 
4 #ifndef __LINUX_SPINLOCK_H
5 # error "please don't include this file directly"
6 #endif
7 
8 /*
9  * rwlock related methods
10  *
11  * split out from spinlock.h
12  *
13  * portions Copyright 2005, Red Hat, Inc., Ingo Molnar
14  * Released under the General Public License (GPL).
15  */
16 
17 #ifdef CONFIG_DEBUG_SPINLOCK
18   extern void __rwlock_init(rwlock_t *lock, const char *name,
19 			    struct lock_class_key *key);
20 # define rwlock_init(lock)					\
21 do {								\
22 	static struct lock_class_key __key;			\
23 								\
24 	__rwlock_init((lock), #lock, &__key);			\
25 } while (0)
26 #else
27 # define rwlock_init(lock)					\
28 	do { *(lock) = __RW_LOCK_UNLOCKED(lock); } while (0)
29 #endif
30 
31 #ifdef CONFIG_DEBUG_SPINLOCK
32  extern void do_raw_read_lock(rwlock_t *lock) __acquires(lock);
33  extern int do_raw_read_trylock(rwlock_t *lock);
34  extern void do_raw_read_unlock(rwlock_t *lock) __releases(lock);
35  extern void do_raw_write_lock(rwlock_t *lock) __acquires(lock);
36  extern int do_raw_write_trylock(rwlock_t *lock);
37  extern void do_raw_write_unlock(rwlock_t *lock) __releases(lock);
38 #else
39 # define do_raw_read_lock(rwlock)	do {__acquire(lock); arch_read_lock(&(rwlock)->raw_lock); } while (0)
40 # define do_raw_read_trylock(rwlock)	arch_read_trylock(&(rwlock)->raw_lock)
41 # define do_raw_read_unlock(rwlock)	do {arch_read_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
42 # define do_raw_write_lock(rwlock)	do {__acquire(lock); arch_write_lock(&(rwlock)->raw_lock); } while (0)
43 # define do_raw_write_trylock(rwlock)	arch_write_trylock(&(rwlock)->raw_lock)
44 # define do_raw_write_unlock(rwlock)	do {arch_write_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
45 #endif
46 
47 /*
48  * Define the various rw_lock methods.  Note we define these
49  * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
50  * methods are defined as nops in the case they are not required.
51  */
52 #define read_trylock(lock)	__cond_lock(lock, _raw_read_trylock(lock))
53 #define write_trylock(lock)	__cond_lock(lock, _raw_write_trylock(lock))
54 
55 #define write_lock(lock)	_raw_write_lock(lock)
56 #define read_lock(lock)		_raw_read_lock(lock)
57 
58 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
59 
60 #define read_lock_irqsave(lock, flags)			\
61 	do {						\
62 		typecheck(unsigned long, flags);	\
63 		flags = _raw_read_lock_irqsave(lock);	\
64 	} while (0)
65 #define write_lock_irqsave(lock, flags)			\
66 	do {						\
67 		typecheck(unsigned long, flags);	\
68 		flags = _raw_write_lock_irqsave(lock);	\
69 	} while (0)
70 
71 #else
72 
73 #define read_lock_irqsave(lock, flags)			\
74 	do {						\
75 		typecheck(unsigned long, flags);	\
76 		_raw_read_lock_irqsave(lock, flags);	\
77 	} while (0)
78 #define write_lock_irqsave(lock, flags)			\
79 	do {						\
80 		typecheck(unsigned long, flags);	\
81 		_raw_write_lock_irqsave(lock, flags);	\
82 	} while (0)
83 
84 #endif
85 
86 #define read_lock_irq(lock)		_raw_read_lock_irq(lock)
87 #define read_lock_bh(lock)		_raw_read_lock_bh(lock)
88 #define write_lock_irq(lock)		_raw_write_lock_irq(lock)
89 #define write_lock_bh(lock)		_raw_write_lock_bh(lock)
90 #define read_unlock(lock)		_raw_read_unlock(lock)
91 #define write_unlock(lock)		_raw_write_unlock(lock)
92 #define read_unlock_irq(lock)		_raw_read_unlock_irq(lock)
93 #define write_unlock_irq(lock)		_raw_write_unlock_irq(lock)
94 
95 #define read_unlock_irqrestore(lock, flags)			\
96 	do {							\
97 		typecheck(unsigned long, flags);		\
98 		_raw_read_unlock_irqrestore(lock, flags);	\
99 	} while (0)
100 #define read_unlock_bh(lock)		_raw_read_unlock_bh(lock)
101 
102 #define write_unlock_irqrestore(lock, flags)		\
103 	do {						\
104 		typecheck(unsigned long, flags);	\
105 		_raw_write_unlock_irqrestore(lock, flags);	\
106 	} while (0)
107 #define write_unlock_bh(lock)		_raw_write_unlock_bh(lock)
108 
109 #define write_trylock_irqsave(lock, flags) \
110 ({ \
111 	local_irq_save(flags); \
112 	write_trylock(lock) ? \
113 	1 : ({ local_irq_restore(flags); 0; }); \
114 })
115 
116 #ifdef arch_rwlock_is_contended
117 #define rwlock_is_contended(lock) \
118 	 arch_rwlock_is_contended(&(lock)->raw_lock)
119 #else
120 #define rwlock_is_contended(lock)	((void)(lock), 0)
121 #endif /* arch_rwlock_is_contended */
122 
123 #endif /* __LINUX_RWLOCK_H */
124