blob: 035d3c57fc8a7147207c1d2cf4e532a355c9d8ae [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>
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070017#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Low90631822014-07-14 10:27:49 -070018#include <linux/osq_lock.h>
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070019#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21struct rw_semaphore;
22
23#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
24#include <linux/rwsem-spinlock.h> /* use a generic implementation */
25#else
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000026/* All arch specific implementations share the same struct */
27struct rw_semaphore {
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070028 long count;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070029 struct list_head wait_list;
Jason Lowce069fc2014-07-14 10:27:52 -070030 raw_spinlock_t wait_lock;
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070031#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Lowce069fc2014-07-14 10:27:52 -070032 struct optimistic_spin_queue osq; /* spinner MCS lock */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070033 /*
34 * Write owner. Used as a speculative check to see
35 * if the owner is running on the cpu.
36 */
37 struct task_struct *owner;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070038#endif
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000039#ifdef CONFIG_DEBUG_LOCK_ALLOC
40 struct lockdep_map dep_map;
41#endif
42};
43
Thomas Gleixnerd1233752011-01-26 21:32:01 +010044extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
45extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
46extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
47extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
Thomas Gleixneraac72272011-01-26 20:06:06 +000048
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000049/* Include the arch specific part */
50#include <asm/rwsem.h>
Thomas Gleixner41e58872011-01-26 20:06:03 +000051
52/* In all implementations count != 0 means locked */
53static inline int rwsem_is_locked(struct rw_semaphore *sem)
54{
55 return sem->count != 0;
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Thomas Gleixner12249b32011-01-26 20:06:00 +000060/* Common initializer macros and functions */
61
62#ifdef CONFIG_DEBUG_LOCK_ALLOC
63# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
64#else
65# define __RWSEM_DEP_MAP_INIT(lockname)
66#endif
67
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070068#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Lowce069fc2014-07-14 10:27:52 -070069#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070070#else
Jason Lowce069fc2014-07-14 10:27:52 -070071#define __RWSEM_OPT_INIT(lockname)
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070072#endif
Thomas Gleixner12249b32011-01-26 20:06:00 +000073
Jason Lowce069fc2014-07-14 10:27:52 -070074#define __RWSEM_INITIALIZER(name) \
75 { .count = RWSEM_UNLOCKED_VALUE, \
76 .wait_list = LIST_HEAD_INIT((name).wait_list), \
77 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
78 __RWSEM_OPT_INIT(name) \
79 __RWSEM_DEP_MAP_INIT(name) }
80
Thomas Gleixner12249b32011-01-26 20:06:00 +000081#define DECLARE_RWSEM(name) \
82 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
83
84extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
85 struct lock_class_key *key);
86
87#define init_rwsem(sem) \
88do { \
89 static struct lock_class_key __key; \
90 \
91 __init_rwsem((sem), #sem, &__key); \
92} while (0)
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
Josef Bacik4a444b12013-08-30 10:05:22 -040095 * This is the same regardless of which rwsem implementation that is being used.
96 * It is just a heuristic meant to be called by somebody alreadying holding the
97 * rwsem to see if somebody from an incompatible type is wanting access to the
98 * lock.
99 */
100static inline int rwsem_is_contended(struct rw_semaphore *sem)
101{
102 return !list_empty(&sem->wait_list);
103}
104
105/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * lock for reading
107 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700108extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/*
111 * trylock for reading -- returns 1 if successful, 0 if contention
112 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700113extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
116 * lock for writing
117 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700118extern void down_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
121 * trylock for writing -- returns 1 if successful, 0 if contention
122 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700123extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*
126 * release a read lock
127 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700128extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*
131 * release a write lock
132 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700133extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/*
136 * downgrade write lock to read lock
137 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700138extern void downgrade_write(struct rw_semaphore *sem);
139
140#ifdef CONFIG_DEBUG_LOCK_ALLOC
141/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700142 * nested locking. NOTE: rwsems are not allowed to recurse
143 * (which occurs if the same task tries to acquire the same
144 * lock instance multiple times), but multiple locks of the
145 * same lock class might be taken, if the order of the locks
146 * is always the same. This ordering rule can be expressed
147 * to lockdep via the _nested() APIs, but enumerating the
148 * subclasses that are used. (If the nesting relationship is
149 * static then another method for expressing nested locking is
150 * the explicit definition of lock class keys and the use of
151 * lockdep_set_class() at lock initialization time.
152 * See Documentation/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700153 */
154extern void down_read_nested(struct rw_semaphore *sem, int subclass);
155extern void down_write_nested(struct rw_semaphore *sem, int subclass);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800156extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
157
158# define down_write_nest_lock(sem, nest_lock) \
159do { \
160 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
161 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
162} while (0);
163
Kent Overstreet84759c62011-09-21 21:43:05 -0700164/*
165 * Take/release a lock when not the owner will release it.
166 *
167 * [ This API should be avoided as much as possible - the
168 * proper abstraction for this case is completions. ]
169 */
170extern void down_read_non_owner(struct rw_semaphore *sem);
171extern void up_read_non_owner(struct rw_semaphore *sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700172#else
173# define down_read_nested(sem, subclass) down_read(sem)
Jiri Kosinae65b9ad2013-01-15 20:12:37 +0100174# define down_write_nest_lock(sem, nest_lock) down_write(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700175# define down_write_nested(sem, subclass) down_write(sem)
Kent Overstreet84759c62011-09-21 21:43:05 -0700176# define down_read_non_owner(sem) down_read(sem)
177# define up_read_non_owner(sem) up_read(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700178#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#endif /* _LINUX_RWSEM_H */