blob: d37fbb34d06fed4494b3152300d71aba23663f63 [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>
Michal Hockod4799602016-04-07 17:12:26 +020017#include <linux/err.h>
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070018#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Low90631822014-07-14 10:27:49 -070019#include <linux/osq_lock.h>
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070020#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22struct rw_semaphore;
23
24#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
25#include <linux/rwsem-spinlock.h> /* use a generic implementation */
26#else
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000027/* All arch specific implementations share the same struct */
28struct rw_semaphore {
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070029 long count;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070030 struct list_head wait_list;
Jason Lowce069fc2014-07-14 10:27:52 -070031 raw_spinlock_t wait_lock;
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070032#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Lowce069fc2014-07-14 10:27:52 -070033 struct optimistic_spin_queue osq; /* spinner MCS lock */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070034 /*
35 * Write owner. Used as a speculative check to see
36 * if the owner is running on the cpu.
37 */
38 struct task_struct *owner;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070039#endif
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000040#ifdef CONFIG_DEBUG_LOCK_ALLOC
41 struct lockdep_map dep_map;
42#endif
43};
44
Thomas Gleixnerd1233752011-01-26 21:32:01 +010045extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
46extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
Michal Hockod4799602016-04-07 17:12:26 +020047extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
Thomas Gleixnerd1233752011-01-26 21:32:01 +010048extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
49extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
Thomas Gleixneraac72272011-01-26 20:06:06 +000050
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000051/* Include the arch specific part */
52#include <asm/rwsem.h>
Thomas Gleixner41e58872011-01-26 20:06:03 +000053
54/* In all implementations count != 0 means locked */
55static inline int rwsem_is_locked(struct rw_semaphore *sem)
56{
57 return sem->count != 0;
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
Thomas Gleixner12249b32011-01-26 20:06:00 +000062/* Common initializer macros and functions */
63
64#ifdef CONFIG_DEBUG_LOCK_ALLOC
65# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
66#else
67# define __RWSEM_DEP_MAP_INIT(lockname)
68#endif
69
Davidlohr Bueso5db6c6f2014-07-11 14:00:06 -070070#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
Jason Lowce069fc2014-07-14 10:27:52 -070071#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070072#else
Jason Lowce069fc2014-07-14 10:27:52 -070073#define __RWSEM_OPT_INIT(lockname)
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070074#endif
Thomas Gleixner12249b32011-01-26 20:06:00 +000075
Jason Lowce069fc2014-07-14 10:27:52 -070076#define __RWSEM_INITIALIZER(name) \
77 { .count = RWSEM_UNLOCKED_VALUE, \
78 .wait_list = LIST_HEAD_INIT((name).wait_list), \
79 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
80 __RWSEM_OPT_INIT(name) \
81 __RWSEM_DEP_MAP_INIT(name) }
82
Thomas Gleixner12249b32011-01-26 20:06:00 +000083#define DECLARE_RWSEM(name) \
84 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
85
86extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
87 struct lock_class_key *key);
88
89#define init_rwsem(sem) \
90do { \
91 static struct lock_class_key __key; \
92 \
93 __init_rwsem((sem), #sem, &__key); \
94} while (0)
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
Josef Bacik4a444b12013-08-30 10:05:22 -040097 * This is the same regardless of which rwsem implementation that is being used.
98 * It is just a heuristic meant to be called by somebody alreadying holding the
99 * rwsem to see if somebody from an incompatible type is wanting access to the
100 * lock.
101 */
102static inline int rwsem_is_contended(struct rw_semaphore *sem)
103{
104 return !list_empty(&sem->wait_list);
105}
106
107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * lock for reading
109 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700110extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/*
113 * trylock for reading -- returns 1 if successful, 0 if contention
114 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700115extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/*
118 * lock for writing
119 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700120extern void down_write(struct rw_semaphore *sem);
Michal Hocko916633a2016-04-07 17:12:31 +0200121extern int __must_check down_write_killable(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
124 * trylock for writing -- returns 1 if successful, 0 if contention
125 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700126extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
129 * release a read lock
130 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700131extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/*
134 * release a write lock
135 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700136extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/*
139 * downgrade write lock to read lock
140 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700141extern void downgrade_write(struct rw_semaphore *sem);
142
143#ifdef CONFIG_DEBUG_LOCK_ALLOC
144/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700145 * nested locking. NOTE: rwsems are not allowed to recurse
146 * (which occurs if the same task tries to acquire the same
147 * lock instance multiple times), but multiple locks of the
148 * same lock class might be taken, if the order of the locks
149 * is always the same. This ordering rule can be expressed
150 * to lockdep via the _nested() APIs, but enumerating the
151 * subclasses that are used. (If the nesting relationship is
152 * static then another method for expressing nested locking is
153 * the explicit definition of lock class keys and the use of
154 * lockdep_set_class() at lock initialization time.
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -0700155 * See Documentation/locking/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700156 */
157extern void down_read_nested(struct rw_semaphore *sem, int subclass);
158extern void down_write_nested(struct rw_semaphore *sem, int subclass);
Al Viro887bddf2016-05-26 00:04:58 -0400159extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800160extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
161
162# define down_write_nest_lock(sem, nest_lock) \
163do { \
164 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
165 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
166} while (0);
167
Kent Overstreet84759c62011-09-21 21:43:05 -0700168/*
169 * Take/release a lock when not the owner will release it.
170 *
171 * [ This API should be avoided as much as possible - the
172 * proper abstraction for this case is completions. ]
173 */
174extern void down_read_non_owner(struct rw_semaphore *sem);
175extern void up_read_non_owner(struct rw_semaphore *sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700176#else
177# define down_read_nested(sem, subclass) down_read(sem)
Jiri Kosinae65b9ad2013-01-15 20:12:37 +0100178# define down_write_nest_lock(sem, nest_lock) down_write(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700179# define down_write_nested(sem, subclass) down_write(sem)
Al Viro887bddf2016-05-26 00:04:58 -0400180# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
Kent Overstreet84759c62011-09-21 21:43:05 -0700181# define down_read_non_owner(sem) down_read(sem)
182# define up_read_non_owner(sem) up_read(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700183#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#endif /* _LINUX_RWSEM_H */