blob: 9fdcdd03507d183e2e92184a9a2b0632e282a1ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* rwsem.h: R/W semaphores, public interface
2 *
3 * Written by David Howells (dhowells@redhat.com).
4 * Derived from asm-i386/semaphore.h
5 */
6
7#ifndef _LINUX_RWSEM_H
8#define _LINUX_RWSEM_H
9
10#include <linux/linkage.h>
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/kernel.h>
Thomas Gleixnerc16a87c2011-01-26 20:05:50 +000014#include <linux/list.h>
15#include <linux/spinlock.h>
Arun Sharma600634972011-07-26 16:09:06 -070016#include <linux/atomic.h>
Jason Low90631822014-07-14 10:27:49 -070017#include <linux/osq_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19struct rw_semaphore;
20
21#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
22#include <linux/rwsem-spinlock.h> /* use a generic implementation */
23#else
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000024/* All arch specific implementations share the same struct */
25struct rw_semaphore {
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070026 long count;
27 raw_spinlock_t wait_lock;
28 struct list_head wait_list;
29#ifdef CONFIG_SMP
30 /*
31 * Write owner. Used as a speculative check to see
32 * if the owner is running on the cpu.
33 */
34 struct task_struct *owner;
Jason Low90631822014-07-14 10:27:49 -070035 struct optimistic_spin_queue osq; /* spinner MCS lock */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070036#endif
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000037#ifdef CONFIG_DEBUG_LOCK_ALLOC
38 struct lockdep_map dep_map;
39#endif
40};
41
Thomas Gleixnerd1233752011-01-26 21:32:01 +010042extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
43extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
44extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
45extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
Thomas Gleixneraac72272011-01-26 20:06:06 +000046
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000047/* Include the arch specific part */
48#include <asm/rwsem.h>
Thomas Gleixner41e58872011-01-26 20:06:03 +000049
50/* In all implementations count != 0 means locked */
51static inline int rwsem_is_locked(struct rw_semaphore *sem)
52{
53 return sem->count != 0;
54}
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Thomas Gleixner12249b32011-01-26 20:06:00 +000058/* Common initializer macros and functions */
59
60#ifdef CONFIG_DEBUG_LOCK_ALLOC
61# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
62#else
63# define __RWSEM_DEP_MAP_INIT(lockname)
64#endif
65
Davidlohr Buesodbb5eaf2014-05-19 17:27:57 -070066#if defined(CONFIG_SMP) && !defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070067#define __RWSEM_INITIALIZER(name) \
68 { RWSEM_UNLOCKED_VALUE, \
69 __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
70 LIST_HEAD_INIT((name).wait_list), \
71 NULL, /* owner */ \
Jason Low90631822014-07-14 10:27:49 -070072 { ATOMIC_INIT(OSQ_UNLOCKED_VAL) } /* osq */ \
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070073 __RWSEM_DEP_MAP_INIT(name) }
74#else
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010075#define __RWSEM_INITIALIZER(name) \
76 { RWSEM_UNLOCKED_VALUE, \
77 __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
78 LIST_HEAD_INIT((name).wait_list) \
79 __RWSEM_DEP_MAP_INIT(name) }
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070080#endif
Thomas Gleixner12249b32011-01-26 20:06:00 +000081
82#define DECLARE_RWSEM(name) \
83 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
84
85extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
86 struct lock_class_key *key);
87
88#define init_rwsem(sem) \
89do { \
90 static struct lock_class_key __key; \
91 \
92 __init_rwsem((sem), #sem, &__key); \
93} while (0)
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
Josef Bacik4a444b12013-08-30 10:05:22 -040096 * This is the same regardless of which rwsem implementation that is being used.
97 * It is just a heuristic meant to be called by somebody alreadying holding the
98 * rwsem to see if somebody from an incompatible type is wanting access to the
99 * lock.
100 */
101static inline int rwsem_is_contended(struct rw_semaphore *sem)
102{
103 return !list_empty(&sem->wait_list);
104}
105
106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * lock for reading
108 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700109extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
112 * trylock for reading -- returns 1 if successful, 0 if contention
113 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700114extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*
117 * lock for writing
118 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700119extern void down_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/*
122 * trylock for writing -- returns 1 if successful, 0 if contention
123 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700124extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/*
127 * release a read lock
128 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700129extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
132 * release a write lock
133 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700134extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/*
137 * downgrade write lock to read lock
138 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700139extern void downgrade_write(struct rw_semaphore *sem);
140
141#ifdef CONFIG_DEBUG_LOCK_ALLOC
142/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700143 * nested locking. NOTE: rwsems are not allowed to recurse
144 * (which occurs if the same task tries to acquire the same
145 * lock instance multiple times), but multiple locks of the
146 * same lock class might be taken, if the order of the locks
147 * is always the same. This ordering rule can be expressed
148 * to lockdep via the _nested() APIs, but enumerating the
149 * subclasses that are used. (If the nesting relationship is
150 * static then another method for expressing nested locking is
151 * the explicit definition of lock class keys and the use of
152 * lockdep_set_class() at lock initialization time.
153 * See Documentation/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700154 */
155extern void down_read_nested(struct rw_semaphore *sem, int subclass);
156extern void down_write_nested(struct rw_semaphore *sem, int subclass);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800157extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
158
159# define down_write_nest_lock(sem, nest_lock) \
160do { \
161 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
162 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
163} while (0);
164
Kent Overstreet84759c62011-09-21 21:43:05 -0700165/*
166 * Take/release a lock when not the owner will release it.
167 *
168 * [ This API should be avoided as much as possible - the
169 * proper abstraction for this case is completions. ]
170 */
171extern void down_read_non_owner(struct rw_semaphore *sem);
172extern void up_read_non_owner(struct rw_semaphore *sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700173#else
174# define down_read_nested(sem, subclass) down_read(sem)
Jiri Kosinae65b9ad2013-01-15 20:12:37 +0100175# define down_write_nest_lock(sem, nest_lock) down_write(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700176# define down_write_nested(sem, subclass) down_write(sem)
Kent Overstreet84759c62011-09-21 21:43:05 -0700177# define down_read_non_owner(sem) down_read(sem)
178# define up_read_non_owner(sem) up_read(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700179#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#endif /* _LINUX_RWSEM_H */