Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 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 |
Luca Barbieri | b97c4bc | 2010-03-11 14:08:45 -0800 | [diff] [blame] | 32 | extern void do_raw_read_lock(rwlock_t *lock) __acquires(lock); |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 33 | #define do_raw_read_lock_flags(lock, flags) do_raw_read_lock(lock) |
| 34 | extern int do_raw_read_trylock(rwlock_t *lock); |
Luca Barbieri | b97c4bc | 2010-03-11 14:08:45 -0800 | [diff] [blame] | 35 | extern void do_raw_read_unlock(rwlock_t *lock) __releases(lock); |
| 36 | extern void do_raw_write_lock(rwlock_t *lock) __acquires(lock); |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 37 | #define do_raw_write_lock_flags(lock, flags) do_raw_write_lock(lock) |
| 38 | extern int do_raw_write_trylock(rwlock_t *lock); |
Luca Barbieri | b97c4bc | 2010-03-11 14:08:45 -0800 | [diff] [blame] | 39 | extern void do_raw_write_unlock(rwlock_t *lock) __releases(lock); |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 40 | #else |
Will Deacon | a4c1887 | 2017-10-03 19:25:29 +0100 | [diff] [blame] | 41 | |
| 42 | #ifndef arch_read_lock_flags |
| 43 | # define arch_read_lock_flags(lock, flags) arch_read_lock(lock) |
| 44 | #endif |
| 45 | |
| 46 | #ifndef arch_write_lock_flags |
| 47 | # define arch_write_lock_flags(lock, flags) arch_write_lock(lock) |
| 48 | #endif |
| 49 | |
Luca Barbieri | b97c4bc | 2010-03-11 14:08:45 -0800 | [diff] [blame] | 50 | # define do_raw_read_lock(rwlock) do {__acquire(lock); arch_read_lock(&(rwlock)->raw_lock); } while (0) |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 51 | # define do_raw_read_lock_flags(lock, flags) \ |
Luca Barbieri | b97c4bc | 2010-03-11 14:08:45 -0800 | [diff] [blame] | 52 | do {__acquire(lock); arch_read_lock_flags(&(lock)->raw_lock, *(flags)); } while (0) |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 53 | # define do_raw_read_trylock(rwlock) arch_read_trylock(&(rwlock)->raw_lock) |
Luca Barbieri | b97c4bc | 2010-03-11 14:08:45 -0800 | [diff] [blame] | 54 | # define do_raw_read_unlock(rwlock) do {arch_read_unlock(&(rwlock)->raw_lock); __release(lock); } while (0) |
| 55 | # define do_raw_write_lock(rwlock) do {__acquire(lock); arch_write_lock(&(rwlock)->raw_lock); } while (0) |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 56 | # define do_raw_write_lock_flags(lock, flags) \ |
Luca Barbieri | b97c4bc | 2010-03-11 14:08:45 -0800 | [diff] [blame] | 57 | do {__acquire(lock); arch_write_lock_flags(&(lock)->raw_lock, *(flags)); } while (0) |
Thomas Gleixner | 9828ea9 | 2009-12-03 20:55:53 +0100 | [diff] [blame] | 58 | # define do_raw_write_trylock(rwlock) arch_write_trylock(&(rwlock)->raw_lock) |
Luca Barbieri | b97c4bc | 2010-03-11 14:08:45 -0800 | [diff] [blame] | 59 | # define do_raw_write_unlock(rwlock) do {arch_write_unlock(&(rwlock)->raw_lock); __release(lock); } while (0) |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 60 | #endif |
| 61 | |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 62 | /* |
| 63 | * Define the various rw_lock methods. Note we define these |
| 64 | * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various |
| 65 | * methods are defined as nops in the case they are not required. |
| 66 | */ |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 67 | #define read_trylock(lock) __cond_lock(lock, _raw_read_trylock(lock)) |
| 68 | #define write_trylock(lock) __cond_lock(lock, _raw_write_trylock(lock)) |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 69 | |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 70 | #define write_lock(lock) _raw_write_lock(lock) |
| 71 | #define read_lock(lock) _raw_read_lock(lock) |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 72 | |
| 73 | #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) |
| 74 | |
| 75 | #define read_lock_irqsave(lock, flags) \ |
| 76 | do { \ |
| 77 | typecheck(unsigned long, flags); \ |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 78 | flags = _raw_read_lock_irqsave(lock); \ |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 79 | } while (0) |
| 80 | #define write_lock_irqsave(lock, flags) \ |
| 81 | do { \ |
| 82 | typecheck(unsigned long, flags); \ |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 83 | flags = _raw_write_lock_irqsave(lock); \ |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 84 | } while (0) |
| 85 | |
| 86 | #else |
| 87 | |
| 88 | #define read_lock_irqsave(lock, flags) \ |
| 89 | do { \ |
| 90 | typecheck(unsigned long, flags); \ |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 91 | _raw_read_lock_irqsave(lock, flags); \ |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 92 | } while (0) |
| 93 | #define write_lock_irqsave(lock, flags) \ |
| 94 | do { \ |
| 95 | typecheck(unsigned long, flags); \ |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 96 | _raw_write_lock_irqsave(lock, flags); \ |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 97 | } while (0) |
| 98 | |
| 99 | #endif |
| 100 | |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 101 | #define read_lock_irq(lock) _raw_read_lock_irq(lock) |
| 102 | #define read_lock_bh(lock) _raw_read_lock_bh(lock) |
| 103 | #define write_lock_irq(lock) _raw_write_lock_irq(lock) |
| 104 | #define write_lock_bh(lock) _raw_write_lock_bh(lock) |
| 105 | #define read_unlock(lock) _raw_read_unlock(lock) |
| 106 | #define write_unlock(lock) _raw_write_unlock(lock) |
| 107 | #define read_unlock_irq(lock) _raw_read_unlock_irq(lock) |
| 108 | #define write_unlock_irq(lock) _raw_write_unlock_irq(lock) |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 109 | |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 110 | #define read_unlock_irqrestore(lock, flags) \ |
| 111 | do { \ |
| 112 | typecheck(unsigned long, flags); \ |
| 113 | _raw_read_unlock_irqrestore(lock, flags); \ |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 114 | } while (0) |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 115 | #define read_unlock_bh(lock) _raw_read_unlock_bh(lock) |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 116 | |
| 117 | #define write_unlock_irqrestore(lock, flags) \ |
| 118 | do { \ |
| 119 | typecheck(unsigned long, flags); \ |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 120 | _raw_write_unlock_irqrestore(lock, flags); \ |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 121 | } while (0) |
Thomas Gleixner | 9c1721a | 2009-12-03 21:52:18 +0100 | [diff] [blame] | 122 | #define write_unlock_bh(lock) _raw_write_unlock_bh(lock) |
Thomas Gleixner | ef12f10 | 2009-11-07 23:04:15 +0100 | [diff] [blame] | 123 | |
| 124 | #define write_trylock_irqsave(lock, flags) \ |
| 125 | ({ \ |
| 126 | local_irq_save(flags); \ |
| 127 | write_trylock(lock) ? \ |
| 128 | 1 : ({ local_irq_restore(flags); 0; }); \ |
| 129 | }) |
| 130 | |
| 131 | #endif /* __LINUX_RWLOCK_H */ |