Blame view

include/linux/rwsem.h 5.93 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
  /* rwsem.h: R/W semaphores, public interface
   *
   * Written by David Howells (dhowells@redhat.com).
   * Derived from asm-i386/semaphore.h
   */
  
  #ifndef _LINUX_RWSEM_H
  #define _LINUX_RWSEM_H
  
  #include <linux/linkage.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
  #include <linux/types.h>
  #include <linux/kernel.h>
c16a87ce0   Thomas Gleixner   rwsem: Cleanup in...
14
15
  #include <linux/list.h>
  #include <linux/spinlock.h>
60063497a   Arun Sharma   atomic: use <linu...
16
  #include <linux/atomic.h>
d47996082   Michal Hocko   locking/rwsem: In...
17
  #include <linux/err.h>
5db6c6fef   Davidlohr Bueso   locking/rwsem: Ad...
18
  #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
90631822c   Jason Low   locking/spinlocks...
19
  #include <linux/osq_lock.h>
5db6c6fef   Davidlohr Bueso   locking/rwsem: Ad...
20
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
24
25
  
  struct rw_semaphore;
  
  #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  #include <linux/rwsem-spinlock.h> /* use a generic implementation */
8ee62b187   Jason Low   locking/rwsem: Co...
26
  #define __RWSEM_INIT_COUNT(name)	.count = RWSEM_UNLOCKED_VALUE
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
  #else
1c8ed640d   Thomas Gleixner   rwsem: Move dupli...
28
29
  /* All arch specific implementations share the same struct */
  struct rw_semaphore {
8ee62b187   Jason Low   locking/rwsem: Co...
30
  	atomic_long_t count;
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
31
  	struct list_head wait_list;
ce069fc92   Jason Low   locking/rwsem: Re...
32
  	raw_spinlock_t wait_lock;
5db6c6fef   Davidlohr Bueso   locking/rwsem: Ad...
33
  #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
ce069fc92   Jason Low   locking/rwsem: Re...
34
  	struct optimistic_spin_queue osq; /* spinner MCS lock */
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
35
36
37
38
39
  	/*
  	 * Write owner. Used as a speculative check to see
  	 * if the owner is running on the cpu.
  	 */
  	struct task_struct *owner;
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
40
  #endif
1c8ed640d   Thomas Gleixner   rwsem: Move dupli...
41
42
43
44
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  	struct lockdep_map	dep_map;
  #endif
  };
77a60e752   Waiman Long   locking/percpu-rw...
45
46
47
48
49
  /*
   * Setting bit 0 of the owner field with other non-zero bits will indicate
   * that the rwsem is writer-owned with an unknown owner.
   */
  #define RWSEM_OWNER_UNKNOWN	((struct task_struct *)-1L)
d12337542   Thomas Gleixner   rwsem: Remove red...
50
  extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
83ced169d   Kirill Tkhai   locking/rwsem-xad...
51
  extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem);
d12337542   Thomas Gleixner   rwsem: Remove red...
52
  extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
d47996082   Michal Hocko   locking/rwsem: In...
53
  extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
d12337542   Thomas Gleixner   rwsem: Remove red...
54
55
  extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
  extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
aac72277f   Thomas Gleixner   rwsem: Move dupli...
56

1c8ed640d   Thomas Gleixner   rwsem: Move dupli...
57
58
  /* Include the arch specific part */
  #include <asm/rwsem.h>
41e5887fa   Thomas Gleixner   rwsem: Unify the ...
59
60
61
62
  
  /* In all implementations count != 0 means locked */
  static inline int rwsem_is_locked(struct rw_semaphore *sem)
  {
8ee62b187   Jason Low   locking/rwsem: Co...
63
  	return atomic_long_read(&sem->count) != 0;
41e5887fa   Thomas Gleixner   rwsem: Unify the ...
64
  }
8ee62b187   Jason Low   locking/rwsem: Co...
65
  #define __RWSEM_INIT_COUNT(name)	.count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  #endif
12249b344   Thomas Gleixner   rwsem: Move dupli...
67
68
69
70
71
72
73
  /* Common initializer macros and functions */
  
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
  #else
  # define __RWSEM_DEP_MAP_INIT(lockname)
  #endif
5db6c6fef   Davidlohr Bueso   locking/rwsem: Ad...
74
  #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
ce069fc92   Jason Low   locking/rwsem: Re...
75
  #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
76
  #else
ce069fc92   Jason Low   locking/rwsem: Re...
77
  #define __RWSEM_OPT_INIT(lockname)
4fc828e24   Davidlohr Bueso   locking/rwsem: Su...
78
  #endif
12249b344   Thomas Gleixner   rwsem: Move dupli...
79

ce069fc92   Jason Low   locking/rwsem: Re...
80
  #define __RWSEM_INITIALIZER(name)				\
8ee62b187   Jason Low   locking/rwsem: Co...
81
  	{ __RWSEM_INIT_COUNT(name),				\
ce069fc92   Jason Low   locking/rwsem: Re...
82
83
84
85
  	  .wait_list = LIST_HEAD_INIT((name).wait_list),	\
  	  .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock)	\
  	  __RWSEM_OPT_INIT(name)				\
  	  __RWSEM_DEP_MAP_INIT(name) }
12249b344   Thomas Gleixner   rwsem: Move dupli...
86
87
88
89
90
91
92
93
94
95
96
97
  #define DECLARE_RWSEM(name) \
  	struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  
  extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  			 struct lock_class_key *key);
  
  #define init_rwsem(sem)						\
  do {								\
  	static struct lock_class_key __key;			\
  								\
  	__init_rwsem((sem), #sem, &__key);			\
  } while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
  /*
4a444b1f0   Josef Bacik   rwsem: add rwsem_...
99
100
101
102
103
104
105
106
107
108
109
   * This is the same regardless of which rwsem implementation that is being used.
   * It is just a heuristic meant to be called by somebody alreadying holding the
   * rwsem to see if somebody from an incompatible type is wanting access to the
   * lock.
   */
  static inline int rwsem_is_contended(struct rw_semaphore *sem)
  {
  	return !list_empty(&sem->wait_list);
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
   * lock for reading
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
112
  extern void down_read(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
115
116
  
  /*
   * trylock for reading -- returns 1 if successful, 0 if contention
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
117
  extern int down_read_trylock(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
120
121
  
  /*
   * lock for writing
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
122
  extern void down_write(struct rw_semaphore *sem);
916633a40   Michal Hocko   locking/rwsem: Pr...
123
  extern int __must_check down_write_killable(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
125
126
127
  
  /*
   * trylock for writing -- returns 1 if successful, 0 if contention
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
128
  extern int down_write_trylock(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
  
  /*
   * release a read lock
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
133
  extern void up_read(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
136
137
  
  /*
   * release a write lock
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
138
  extern void up_write(struct rw_semaphore *sem);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
  
  /*
   * downgrade write lock to read lock
   */
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
143
144
145
146
  extern void downgrade_write(struct rw_semaphore *sem);
  
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  /*
5fca80e8b   Ingo Molnar   [PATCH] lockdep: ...
147
148
149
150
151
152
153
154
155
156
   * nested locking. NOTE: rwsems are not allowed to recurse
   * (which occurs if the same task tries to acquire the same
   * lock instance multiple times), but multiple locks of the
   * same lock class might be taken, if the order of the locks
   * is always the same. This ordering rule can be expressed
   * to lockdep via the _nested() APIs, but enumerating the
   * subclasses that are used. (If the nesting relationship is
   * static then another method for expressing nested locking is
   * the explicit definition of lock class keys and the use of
   * lockdep_set_class() at lock initialization time.
214e0aed6   Davidlohr Bueso   locking/Documenta...
157
   * See Documentation/locking/lockdep-design.txt for more details.)
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
158
159
160
   */
  extern void down_read_nested(struct rw_semaphore *sem, int subclass);
  extern void down_write_nested(struct rw_semaphore *sem, int subclass);
887bddfa9   Al Viro   add down_write_ki...
161
  extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
1b963c81b   Jiri Kosina   lockdep, rwsem: p...
162
163
164
165
166
167
168
  extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
  
  # define down_write_nest_lock(sem, nest_lock)			\
  do {								\
  	typecheck(struct lockdep_map *, &(nest_lock)->dep_map);	\
  	_down_write_nest_lock(sem, &(nest_lock)->dep_map);	\
  } while (0);
84759c6d1   Kent Overstreet   Revert "rw_semaph...
169
170
171
172
173
174
175
176
  /*
   * Take/release a lock when not the owner will release it.
   *
   * [ This API should be avoided as much as possible - the
   *   proper abstraction for this case is completions. ]
   */
  extern void down_read_non_owner(struct rw_semaphore *sem);
  extern void up_read_non_owner(struct rw_semaphore *sem);
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
177
178
  #else
  # define down_read_nested(sem, subclass)		down_read(sem)
e65b9ad22   Jiri Kosina   lockdep, rwsem: f...
179
  # define down_write_nest_lock(sem, nest_lock)	down_write(sem)
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
180
  # define down_write_nested(sem, subclass)	down_write(sem)
887bddfa9   Al Viro   add down_write_ki...
181
  # define down_write_killable_nested(sem, subclass)	down_write_killable(sem)
84759c6d1   Kent Overstreet   Revert "rw_semaph...
182
183
  # define down_read_non_owner(sem)		down_read(sem)
  # define up_read_non_owner(sem)			up_read(sem)
4ea2176df   Ingo Molnar   [PATCH] lockdep: ...
184
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
  #endif /* _LINUX_RWSEM_H */