blob: 496296d12d62a183304a443caa921cb0fd821538 [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
25#include <asm/rwsem.h> /* use an arch-specific implementation */
26#endif
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
29 * lock for reading
30 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070031extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
34 * trylock for reading -- returns 1 if successful, 0 if contention
35 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070036extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * lock for writing
40 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070041extern void down_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * trylock for writing -- returns 1 if successful, 0 if contention
45 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070046extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * release a read lock
50 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070051extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * release a write lock
55 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070056extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
59 * downgrade write lock to read lock
60 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070061extern void downgrade_write(struct rw_semaphore *sem);
62
63#ifdef CONFIG_DEBUG_LOCK_ALLOC
64/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -070065 * nested locking. NOTE: rwsems are not allowed to recurse
66 * (which occurs if the same task tries to acquire the same
67 * lock instance multiple times), but multiple locks of the
68 * same lock class might be taken, if the order of the locks
69 * is always the same. This ordering rule can be expressed
70 * to lockdep via the _nested() APIs, but enumerating the
71 * subclasses that are used. (If the nesting relationship is
72 * static then another method for expressing nested locking is
73 * the explicit definition of lock class keys and the use of
74 * lockdep_set_class() at lock initialization time.
75 * See Documentation/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -070076 */
77extern void down_read_nested(struct rw_semaphore *sem, int subclass);
78extern void down_write_nested(struct rw_semaphore *sem, int subclass);
79/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -070080 * Take/release a lock when not the owner will release it.
81 *
82 * [ This API should be avoided as much as possible - the
83 * proper abstraction for this case is completions. ]
Ingo Molnar4ea21762006-07-03 00:24:53 -070084 */
85extern void down_read_non_owner(struct rw_semaphore *sem);
86extern void up_read_non_owner(struct rw_semaphore *sem);
87#else
88# define down_read_nested(sem, subclass) down_read(sem)
89# define down_write_nested(sem, subclass) down_write(sem)
90# define down_read_non_owner(sem) down_read(sem)
91# define up_read_non_owner(sem) up_read(sem)
92#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#endif /* _LINUX_RWSEM_H */