blob: e8be18edb66413e897e1aff361ba04ec92e07e98 [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>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/system.h>
18#include <asm/atomic.h>
19
20struct rw_semaphore;
21
22#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
23#include <linux/rwsem-spinlock.h> /* use a generic implementation */
24#else
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000025/* All arch specific implementations share the same struct */
26struct rw_semaphore {
27 long count;
28 spinlock_t wait_lock;
29 struct list_head wait_list;
30#ifdef CONFIG_DEBUG_LOCK_ALLOC
31 struct lockdep_map dep_map;
32#endif
33};
34
35/* Include the arch specific part */
36#include <asm/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#endif
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * lock for reading
41 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070042extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * trylock for reading -- returns 1 if successful, 0 if contention
46 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070047extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * lock for writing
51 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070052extern void down_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * trylock for writing -- returns 1 if successful, 0 if contention
56 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070057extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
60 * release a read lock
61 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070062extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * release a write lock
66 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070067extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 * downgrade write lock to read lock
71 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070072extern void downgrade_write(struct rw_semaphore *sem);
73
74#ifdef CONFIG_DEBUG_LOCK_ALLOC
75/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -070076 * nested locking. NOTE: rwsems are not allowed to recurse
77 * (which occurs if the same task tries to acquire the same
78 * lock instance multiple times), but multiple locks of the
79 * same lock class might be taken, if the order of the locks
80 * is always the same. This ordering rule can be expressed
81 * to lockdep via the _nested() APIs, but enumerating the
82 * subclasses that are used. (If the nesting relationship is
83 * static then another method for expressing nested locking is
84 * the explicit definition of lock class keys and the use of
85 * lockdep_set_class() at lock initialization time.
86 * See Documentation/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -070087 */
88extern void down_read_nested(struct rw_semaphore *sem, int subclass);
89extern void down_write_nested(struct rw_semaphore *sem, int subclass);
90/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -070091 * Take/release a lock when not the owner will release it.
92 *
93 * [ This API should be avoided as much as possible - the
94 * proper abstraction for this case is completions. ]
Ingo Molnar4ea21762006-07-03 00:24:53 -070095 */
96extern void down_read_non_owner(struct rw_semaphore *sem);
97extern void up_read_non_owner(struct rw_semaphore *sem);
98#else
99# define down_read_nested(sem, subclass) down_read(sem)
100# define down_write_nested(sem, subclass) down_write(sem)
101# define down_read_non_owner(sem) down_read(sem)
102# define up_read_non_owner(sem) up_read(sem)
103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif /* _LINUX_RWSEM_H */