rwsem.c 3.7 KB
Newer Older
I
Ingo Molnar 已提交
1 2 3 4 5 6 7 8
/* kernel/rwsem.c: R/W semaphores, public implementation
 *
 * Written by David Howells (dhowells@redhat.com).
 * Derived from asm-i386/semaphore.h
 */

#include <linux/types.h>
#include <linux/kernel.h>
9
#include <linux/sched.h>
10
#include <linux/export.h>
I
Ingo Molnar 已提交
11
#include <linux/rwsem.h>
A
Arun Sharma 已提交
12
#include <linux/atomic.h>
I
Ingo Molnar 已提交
13

14
#include "rwsem.h"
15

I
Ingo Molnar 已提交
16 17 18
/*
 * lock for reading
 */
19
void __sched down_read(struct rw_semaphore *sem)
I
Ingo Molnar 已提交
20 21 22 23
{
	might_sleep();
	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);

24
	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
I
Ingo Molnar 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
}

EXPORT_SYMBOL(down_read);

/*
 * trylock for reading -- returns 1 if successful, 0 if contention
 */
int down_read_trylock(struct rw_semaphore *sem)
{
	int ret = __down_read_trylock(sem);

	if (ret == 1)
		rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
	return ret;
}

EXPORT_SYMBOL(down_read_trylock);

/*
 * lock for writing
 */
46
void __sched down_write(struct rw_semaphore *sem)
I
Ingo Molnar 已提交
47 48 49 50
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);

51
	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
52
	rwsem_set_owner(sem);
I
Ingo Molnar 已提交
53 54 55 56
}

EXPORT_SYMBOL(down_write);

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*
 * lock for writing
 */
int __sched down_write_killable(struct rw_semaphore *sem)
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);

	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
		rwsem_release(&sem->dep_map, 1, _RET_IP_);
		return -EINTR;
	}

	rwsem_set_owner(sem);
	return 0;
}

EXPORT_SYMBOL(down_write_killable);

I
Ingo Molnar 已提交
76 77 78 79 80 81 82
/*
 * trylock for writing -- returns 1 if successful, 0 if contention
 */
int down_write_trylock(struct rw_semaphore *sem)
{
	int ret = __down_write_trylock(sem);

83
	if (ret == 1) {
84
		rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
85 86 87
		rwsem_set_owner(sem);
	}

I
Ingo Molnar 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	return ret;
}

EXPORT_SYMBOL(down_write_trylock);

/*
 * release a read lock
 */
void up_read(struct rw_semaphore *sem)
{
	rwsem_release(&sem->dep_map, 1, _RET_IP_);

	__up_read(sem);
}

EXPORT_SYMBOL(up_read);

/*
 * release a write lock
 */
void up_write(struct rw_semaphore *sem)
{
	rwsem_release(&sem->dep_map, 1, _RET_IP_);

112
	rwsem_clear_owner(sem);
I
Ingo Molnar 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126
	__up_write(sem);
}

EXPORT_SYMBOL(up_write);

/*
 * downgrade write lock to read lock
 */
void downgrade_write(struct rw_semaphore *sem)
{
	/*
	 * lockdep: a downgraded write will live on as a write
	 * dependency.
	 */
127
	rwsem_clear_owner(sem);
I
Ingo Molnar 已提交
128 129 130 131
	__downgrade_write(sem);
}

EXPORT_SYMBOL(downgrade_write);
132 133 134 135 136 137 138 139

#ifdef CONFIG_DEBUG_LOCK_ALLOC

void down_read_nested(struct rw_semaphore *sem, int subclass)
{
	might_sleep();
	rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);

140
	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
141 142 143 144
}

EXPORT_SYMBOL(down_read_nested);

145 146 147 148 149 150
void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
{
	might_sleep();
	rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);

	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
151
	rwsem_set_owner(sem);
152 153 154 155
}

EXPORT_SYMBOL(_down_write_nest_lock);

156 157 158 159 160 161 162 163 164
void down_read_non_owner(struct rw_semaphore *sem)
{
	might_sleep();

	__down_read(sem);
}

EXPORT_SYMBOL(down_read_non_owner);

165 166 167 168 169
void down_write_nested(struct rw_semaphore *sem, int subclass)
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);

170
	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
171
	rwsem_set_owner(sem);
172 173 174 175
}

EXPORT_SYMBOL(down_write_nested);

A
Al Viro 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);

	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
		rwsem_release(&sem->dep_map, 1, _RET_IP_);
		return -EINTR;
	}

	rwsem_set_owner(sem);
	return 0;
}

EXPORT_SYMBOL(down_write_killable_nested);

192 193 194 195 196 197 198
void up_read_non_owner(struct rw_semaphore *sem)
{
	__up_read(sem);
}

EXPORT_SYMBOL(up_read_non_owner);

199 200 201
#endif