blob: 716807f0eb2d62a94015dd8c421b09744371769b [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;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070027 struct list_head wait_list;
Jason Lowce069fc2014-07-14 10:27:52 -070028 raw_spinlock_t wait_lock;
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070029#ifdef CONFIG_SMP
Jason Lowce069fc2014-07-14 10:27:52 -070030 struct optimistic_spin_queue osq; /* spinner MCS lock */
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070031 /*
32 * Write owner. Used as a speculative check to see
33 * if the owner is running on the cpu.
34 */
35 struct task_struct *owner;
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)
Jason Lowce069fc2014-07-14 10:27:52 -070067#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070068#else
Jason Lowce069fc2014-07-14 10:27:52 -070069#define __RWSEM_OPT_INIT(lockname)
Davidlohr Bueso4fc828e2014-05-02 11:24:15 -070070#endif
Thomas Gleixner12249b32011-01-26 20:06:00 +000071
Jason Lowce069fc2014-07-14 10:27:52 -070072#define __RWSEM_INITIALIZER(name) \
73 { .count = RWSEM_UNLOCKED_VALUE, \
74 .wait_list = LIST_HEAD_INIT((name).wait_list), \
75 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
76 __RWSEM_OPT_INIT(name) \
77 __RWSEM_DEP_MAP_INIT(name) }
78
Thomas Gleixner12249b32011-01-26 20:06:00 +000079#define DECLARE_RWSEM(name) \
80 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
81
82extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
83 struct lock_class_key *key);
84
85#define init_rwsem(sem) \
86do { \
87 static struct lock_class_key __key; \
88 \
89 __init_rwsem((sem), #sem, &__key); \
90} while (0)
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
Josef Bacik4a444b12013-08-30 10:05:22 -040093 * This is the same regardless of which rwsem implementation that is being used.
94 * It is just a heuristic meant to be called by somebody alreadying holding the
95 * rwsem to see if somebody from an incompatible type is wanting access to the
96 * lock.
97 */
98static inline int rwsem_is_contended(struct rw_semaphore *sem)
99{
100 return !list_empty(&sem->wait_list);
101}
102
103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * lock for reading
105 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700106extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * trylock for reading -- returns 1 if successful, 0 if contention
110 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700111extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
114 * lock for writing
115 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700116extern void down_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/*
119 * trylock for writing -- returns 1 if successful, 0 if contention
120 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700121extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
124 * release a read lock
125 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700126extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
129 * release a write lock
130 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700131extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/*
134 * downgrade write lock to read lock
135 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700136extern void downgrade_write(struct rw_semaphore *sem);
137
138#ifdef CONFIG_DEBUG_LOCK_ALLOC
139/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700140 * nested locking. NOTE: rwsems are not allowed to recurse
141 * (which occurs if the same task tries to acquire the same
142 * lock instance multiple times), but multiple locks of the
143 * same lock class might be taken, if the order of the locks
144 * is always the same. This ordering rule can be expressed
145 * to lockdep via the _nested() APIs, but enumerating the
146 * subclasses that are used. (If the nesting relationship is
147 * static then another method for expressing nested locking is
148 * the explicit definition of lock class keys and the use of
149 * lockdep_set_class() at lock initialization time.
150 * See Documentation/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700151 */
152extern void down_read_nested(struct rw_semaphore *sem, int subclass);
153extern void down_write_nested(struct rw_semaphore *sem, int subclass);
Jiri Kosina1b963c82013-01-11 14:31:56 -0800154extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
155
156# define down_write_nest_lock(sem, nest_lock) \
157do { \
158 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
159 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
160} while (0);
161
Kent Overstreet84759c62011-09-21 21:43:05 -0700162/*
163 * Take/release a lock when not the owner will release it.
164 *
165 * [ This API should be avoided as much as possible - the
166 * proper abstraction for this case is completions. ]
167 */
168extern void down_read_non_owner(struct rw_semaphore *sem);
169extern void up_read_non_owner(struct rw_semaphore *sem);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700170#else
171# define down_read_nested(sem, subclass) down_read(sem)
Jiri Kosinae65b9ad2013-01-15 20:12:37 +0100172# define down_write_nest_lock(sem, nest_lock) down_write(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700173# define down_write_nested(sem, subclass) down_write(sem)
Kent Overstreet84759c62011-09-21 21:43:05 -0700174# define down_read_non_owner(sem) down_read(sem)
175# define up_read_non_owner(sem) up_read(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif /* _LINUX_RWSEM_H */