blob: efd348fe8ca75f6d9effcf8f05e87bd276e46d97 [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>
14#include <asm/system.h>
15#include <asm/atomic.h>
16
17struct rw_semaphore;
18
19#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
20#include <linux/rwsem-spinlock.h> /* use a generic implementation */
21#else
22#include <asm/rwsem.h> /* use an arch-specific implementation */
23#endif
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/*
26 * lock for reading
27 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070028extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * trylock for reading -- returns 1 if successful, 0 if contention
32 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070033extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
36 * lock for writing
37 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070038extern void down_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * trylock for writing -- returns 1 if successful, 0 if contention
42 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070043extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * release a read lock
47 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070048extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * release a write lock
52 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070053extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * downgrade write lock to read lock
57 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070058extern void downgrade_write(struct rw_semaphore *sem);
59
60#ifdef CONFIG_DEBUG_LOCK_ALLOC
61/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -070062 * nested locking. NOTE: rwsems are not allowed to recurse
63 * (which occurs if the same task tries to acquire the same
64 * lock instance multiple times), but multiple locks of the
65 * same lock class might be taken, if the order of the locks
66 * is always the same. This ordering rule can be expressed
67 * to lockdep via the _nested() APIs, but enumerating the
68 * subclasses that are used. (If the nesting relationship is
69 * static then another method for expressing nested locking is
70 * the explicit definition of lock class keys and the use of
71 * lockdep_set_class() at lock initialization time.
72 * See Documentation/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -070073 */
74extern void down_read_nested(struct rw_semaphore *sem, int subclass);
75extern void down_write_nested(struct rw_semaphore *sem, int subclass);
76/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -070077 * Take/release a lock when not the owner will release it.
78 *
79 * [ This API should be avoided as much as possible - the
80 * proper abstraction for this case is completions. ]
Ingo Molnar4ea21762006-07-03 00:24:53 -070081 */
82extern void down_read_non_owner(struct rw_semaphore *sem);
83extern void up_read_non_owner(struct rw_semaphore *sem);
84#else
85# define down_read_nested(sem, subclass) down_read(sem)
86# define down_write_nested(sem, subclass) down_write(sem)
87# define down_read_non_owner(sem) down_read(sem)
88# define up_read_non_owner(sem) up_read(sem)
89#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#endif /* _LINUX_RWSEM_H */