blob: bc2994ed66e1c0e915ab7b9dbae61a470b282e50 [file] [log] [blame]
Thomas Gleixneref12f102009-11-07 23:04:15 +01001#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) \
21do { \
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
Luca Barbierib97c4bc2010-03-11 14:08:45 -080032 extern void do_raw_read_lock(rwlock_t *lock) __acquires(lock);
Thomas Gleixner9828ea92009-12-03 20:55:53 +010033#define do_raw_read_lock_flags(lock, flags) do_raw_read_lock(lock)
34 extern int do_raw_read_trylock(rwlock_t *lock);
Luca Barbierib97c4bc2010-03-11 14:08:45 -080035 extern void do_raw_read_unlock(rwlock_t *lock) __releases(lock);
36 extern void do_raw_write_lock(rwlock_t *lock) __acquires(lock);
Thomas Gleixner9828ea92009-12-03 20:55:53 +010037#define do_raw_write_lock_flags(lock, flags) do_raw_write_lock(lock)
38 extern int do_raw_write_trylock(rwlock_t *lock);
Luca Barbierib97c4bc2010-03-11 14:08:45 -080039 extern void do_raw_write_unlock(rwlock_t *lock) __releases(lock);
Thomas Gleixneref12f102009-11-07 23:04:15 +010040#else
Luca Barbierib97c4bc2010-03-11 14:08:45 -080041# define do_raw_read_lock(rwlock) do {__acquire(lock); arch_read_lock(&(rwlock)->raw_lock); } while (0)
Thomas Gleixner9828ea92009-12-03 20:55:53 +010042# define do_raw_read_lock_flags(lock, flags) \
Luca Barbierib97c4bc2010-03-11 14:08:45 -080043 do {__acquire(lock); arch_read_lock_flags(&(lock)->raw_lock, *(flags)); } while (0)
Thomas Gleixner9828ea92009-12-03 20:55:53 +010044# define do_raw_read_trylock(rwlock) arch_read_trylock(&(rwlock)->raw_lock)
Luca Barbierib97c4bc2010-03-11 14:08:45 -080045# define do_raw_read_unlock(rwlock) do {arch_read_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
46# define do_raw_write_lock(rwlock) do {__acquire(lock); arch_write_lock(&(rwlock)->raw_lock); } while (0)
Thomas Gleixner9828ea92009-12-03 20:55:53 +010047# define do_raw_write_lock_flags(lock, flags) \
Luca Barbierib97c4bc2010-03-11 14:08:45 -080048 do {__acquire(lock); arch_write_lock_flags(&(lock)->raw_lock, *(flags)); } while (0)
Thomas Gleixner9828ea92009-12-03 20:55:53 +010049# define do_raw_write_trylock(rwlock) arch_write_trylock(&(rwlock)->raw_lock)
Luca Barbierib97c4bc2010-03-11 14:08:45 -080050# define do_raw_write_unlock(rwlock) do {arch_write_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
Thomas Gleixneref12f102009-11-07 23:04:15 +010051#endif
52
Thomas Gleixnere5931942009-12-03 20:08:46 +010053#define read_can_lock(rwlock) arch_read_can_lock(&(rwlock)->raw_lock)
54#define write_can_lock(rwlock) arch_write_can_lock(&(rwlock)->raw_lock)
Thomas Gleixneref12f102009-11-07 23:04:15 +010055
56/*
57 * Define the various rw_lock methods. Note we define these
58 * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
59 * methods are defined as nops in the case they are not required.
60 */
Thomas Gleixner9c1721a2009-12-03 21:52:18 +010061#define read_trylock(lock) __cond_lock(lock, _raw_read_trylock(lock))
62#define write_trylock(lock) __cond_lock(lock, _raw_write_trylock(lock))
Thomas Gleixneref12f102009-11-07 23:04:15 +010063
Thomas Gleixner9c1721a2009-12-03 21:52:18 +010064#define write_lock(lock) _raw_write_lock(lock)
65#define read_lock(lock) _raw_read_lock(lock)
Thomas Gleixneref12f102009-11-07 23:04:15 +010066
67#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
68
69#define read_lock_irqsave(lock, flags) \
70 do { \
71 typecheck(unsigned long, flags); \
Thomas Gleixner9c1721a2009-12-03 21:52:18 +010072 flags = _raw_read_lock_irqsave(lock); \
Thomas Gleixneref12f102009-11-07 23:04:15 +010073 } while (0)
74#define write_lock_irqsave(lock, flags) \
75 do { \
76 typecheck(unsigned long, flags); \
Thomas Gleixner9c1721a2009-12-03 21:52:18 +010077 flags = _raw_write_lock_irqsave(lock); \
Thomas Gleixneref12f102009-11-07 23:04:15 +010078 } while (0)
79
80#else
81
82#define read_lock_irqsave(lock, flags) \
83 do { \
84 typecheck(unsigned long, flags); \
Thomas Gleixner9c1721a2009-12-03 21:52:18 +010085 _raw_read_lock_irqsave(lock, flags); \
Thomas Gleixneref12f102009-11-07 23:04:15 +010086 } while (0)
87#define write_lock_irqsave(lock, flags) \
88 do { \
89 typecheck(unsigned long, flags); \
Thomas Gleixner9c1721a2009-12-03 21:52:18 +010090 _raw_write_lock_irqsave(lock, flags); \
Thomas Gleixneref12f102009-11-07 23:04:15 +010091 } while (0)
92
93#endif
94
Thomas Gleixner9c1721a2009-12-03 21:52:18 +010095#define read_lock_irq(lock) _raw_read_lock_irq(lock)
96#define read_lock_bh(lock) _raw_read_lock_bh(lock)
97#define write_lock_irq(lock) _raw_write_lock_irq(lock)
98#define write_lock_bh(lock) _raw_write_lock_bh(lock)
99#define read_unlock(lock) _raw_read_unlock(lock)
100#define write_unlock(lock) _raw_write_unlock(lock)
101#define read_unlock_irq(lock) _raw_read_unlock_irq(lock)
102#define write_unlock_irq(lock) _raw_write_unlock_irq(lock)
Thomas Gleixneref12f102009-11-07 23:04:15 +0100103
Thomas Gleixner9c1721a2009-12-03 21:52:18 +0100104#define read_unlock_irqrestore(lock, flags) \
105 do { \
106 typecheck(unsigned long, flags); \
107 _raw_read_unlock_irqrestore(lock, flags); \
Thomas Gleixneref12f102009-11-07 23:04:15 +0100108 } while (0)
Thomas Gleixner9c1721a2009-12-03 21:52:18 +0100109#define read_unlock_bh(lock) _raw_read_unlock_bh(lock)
Thomas Gleixneref12f102009-11-07 23:04:15 +0100110
111#define write_unlock_irqrestore(lock, flags) \
112 do { \
113 typecheck(unsigned long, flags); \
Thomas Gleixner9c1721a2009-12-03 21:52:18 +0100114 _raw_write_unlock_irqrestore(lock, flags); \
Thomas Gleixneref12f102009-11-07 23:04:15 +0100115 } while (0)
Thomas Gleixner9c1721a2009-12-03 21:52:18 +0100116#define write_unlock_bh(lock) _raw_write_unlock_bh(lock)
Thomas Gleixneref12f102009-11-07 23:04:15 +0100117
118#define write_trylock_irqsave(lock, flags) \
119({ \
120 local_irq_save(flags); \
121 write_trylock(lock) ? \
122 1 : ({ local_irq_restore(flags); 0; }); \
123})
124
125#endif /* __LINUX_RWLOCK_H */