blob: 54bd7cd7ecbd11449b9e15b49f9ab7e208e96413 [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
Arun Sharma600634972011-07-26 16:09:06 -070017#include <linux/atomic.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 {
26 long count;
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010027 raw_spinlock_t wait_lock;
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000028 struct list_head wait_list;
29#ifdef CONFIG_DEBUG_LOCK_ALLOC
30 struct lockdep_map dep_map;
31#endif
32};
33
Thomas Gleixnerd1233752011-01-26 21:32:01 +010034extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
35extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
36extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
37extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
Thomas Gleixneraac72272011-01-26 20:06:06 +000038
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000039/* Include the arch specific part */
40#include <asm/rwsem.h>
Thomas Gleixner41e58872011-01-26 20:06:03 +000041
42/* In all implementations count != 0 means locked */
43static inline int rwsem_is_locked(struct rw_semaphore *sem)
44{
45 return sem->count != 0;
46}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#endif
49
Thomas Gleixner12249b32011-01-26 20:06:00 +000050/* Common initializer macros and functions */
51
52#ifdef CONFIG_DEBUG_LOCK_ALLOC
53# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
54#else
55# define __RWSEM_DEP_MAP_INIT(lockname)
56#endif
57
Thomas Gleixnerddb6c9b2010-02-24 09:54:54 +010058#define __RWSEM_INITIALIZER(name) \
59 { RWSEM_UNLOCKED_VALUE, \
60 __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
61 LIST_HEAD_INIT((name).wait_list) \
62 __RWSEM_DEP_MAP_INIT(name) }
Thomas Gleixner12249b32011-01-26 20:06:00 +000063
64#define DECLARE_RWSEM(name) \
65 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
66
67extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
68 struct lock_class_key *key);
69
70#define init_rwsem(sem) \
71do { \
72 static struct lock_class_key __key; \
73 \
74 __init_rwsem((sem), #sem, &__key); \
75} while (0)
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * lock for reading
79 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070080extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * trylock for reading -- returns 1 if successful, 0 if contention
84 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070085extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * lock for writing
89 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070090extern void down_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
93 * trylock for writing -- returns 1 if successful, 0 if contention
94 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070095extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/*
98 * release a read lock
99 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700100extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
103 * release a write lock
104 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700105extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * downgrade write lock to read lock
109 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700110extern void downgrade_write(struct rw_semaphore *sem);
111
112#ifdef CONFIG_DEBUG_LOCK_ALLOC
113/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700114 * nested locking. NOTE: rwsems are not allowed to recurse
115 * (which occurs if the same task tries to acquire the same
116 * lock instance multiple times), but multiple locks of the
117 * same lock class might be taken, if the order of the locks
118 * is always the same. This ordering rule can be expressed
119 * to lockdep via the _nested() APIs, but enumerating the
120 * subclasses that are used. (If the nesting relationship is
121 * static then another method for expressing nested locking is
122 * the explicit definition of lock class keys and the use of
123 * lockdep_set_class() at lock initialization time.
124 * See Documentation/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700125 */
126extern void down_read_nested(struct rw_semaphore *sem, int subclass);
127extern void down_write_nested(struct rw_semaphore *sem, int subclass);
Ingo Molnar4ea21762006-07-03 00:24:53 -0700128#else
129# define down_read_nested(sem, subclass) down_read(sem)
130# define down_write_nested(sem, subclass) down_write(sem)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#endif /* _LINUX_RWSEM_H */