rwsem.c 5.35 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9
/* rwsem.c: R/W semaphores: contention handling functions
 *
 * Written by David Howells (dhowells@redhat.com).
 * Derived from arch/i386/kernel/semaphore.c
 */
#include <linux/rwsem.h>
#include <linux/sched.h>
#include <linux/module.h>

Linus Torvalds's avatar
Linus Torvalds committed
10
struct rwsem_waiter {
Linus Torvalds's avatar
Linus Torvalds committed
11
	struct list_head	list;
Linus Torvalds's avatar
Linus Torvalds committed
12 13 14 15 16 17 18
	struct task_struct	*task;
	unsigned int		flags;
#define RWSEM_WAITING_FOR_READ	0x00000001
#define RWSEM_WAITING_FOR_WRITE	0x00000002
};

#if RWSEM_DEBUG
Linus Torvalds's avatar
Linus Torvalds committed
19
#undef rwsemtrace
Linus Torvalds's avatar
Linus Torvalds committed
20 21
void rwsemtrace(struct rw_semaphore *sem, const char *str)
{
Linus Torvalds's avatar
Linus Torvalds committed
22 23
	printk("sem=%p\n",sem);
	printk("(sem)=%08lx\n",sem->count);
Linus Torvalds's avatar
Linus Torvalds committed
24
	if (sem->debug)
Linus Torvalds's avatar
Linus Torvalds committed
25
		printk("[%d] %s({%08lx})\n",current->pid,str,sem->count);
Linus Torvalds's avatar
Linus Torvalds committed
26 27 28 29 30 31
}
#endif

/*
 * handle the lock being released whilst there are processes blocked on it that can now run
 * - if we come here, then:
Linus Torvalds's avatar
Linus Torvalds committed
32
 *   - the 'active part' of the count (&0x0000ffff) reached zero but has been re-incremented
Linus Torvalds's avatar
Linus Torvalds committed
33
 *   - the 'waiting part' of the count (&0xffff0000) is negative (and will still be so)
Linus Torvalds's avatar
Linus Torvalds committed
34 35 36
 *   - there must be someone on the queue
 * - the spinlock must be held by the caller
 * - woken process blocks are discarded from the list after having flags zeroised
Linus Torvalds's avatar
Linus Torvalds committed
37
 */
Linus Torvalds's avatar
Linus Torvalds committed
38
static inline struct rw_semaphore *__rwsem_do_wake(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
39
{
Linus Torvalds's avatar
Linus Torvalds committed
40 41 42
	struct rwsem_waiter *waiter;
	struct list_head *next;
	signed long oldcount;
Linus Torvalds's avatar
Linus Torvalds committed
43 44 45 46
	int woken, loop;

	rwsemtrace(sem,"Entering __rwsem_do_wake");

Linus Torvalds's avatar
Linus Torvalds committed
47 48 49 50 51
	/* only wake someone up if we can transition the active part of the count from 0 -> 1 */
 try_again:
	oldcount = rwsem_atomic_update(RWSEM_ACTIVE_BIAS,sem) - RWSEM_ACTIVE_BIAS;
	if (oldcount & RWSEM_ACTIVE_MASK)
		goto undo;
Linus Torvalds's avatar
Linus Torvalds committed
52

Linus Torvalds's avatar
Linus Torvalds committed
53
	waiter = list_entry(sem->wait_list.next,struct rwsem_waiter,list);
Linus Torvalds's avatar
Linus Torvalds committed
54 55 56 57 58

	/* try to grant a single write lock if there's a writer at the front of the queue
	 * - note we leave the 'active part' of the count incremented by 1 and the waiting part
	 *   incremented by 0x00010000
	 */
Linus Torvalds's avatar
Linus Torvalds committed
59 60 61 62 63 64 65
	if (!(waiter->flags & RWSEM_WAITING_FOR_WRITE))
		goto readers_only;

	list_del(&waiter->list);
	waiter->flags = 0;
	wake_up_process(waiter->task);
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
66 67 68 69 70

	/* grant an infinite number of read locks to the readers at the front of the queue
	 * - note we increment the 'active part' of the count by the number of readers (less one
	 *   for the activity decrement we've already done) before waking any processes up
	 */
Linus Torvalds's avatar
Linus Torvalds committed
71
 readers_only:
Linus Torvalds's avatar
Linus Torvalds committed
72 73 74
	woken = 0;
	do {
		woken++;
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78 79 80 81

		if (waiter->list.next==&sem->wait_list)
			break;

		waiter = list_entry(waiter->list.next,struct rwsem_waiter,list);

	} while (waiter->flags & RWSEM_WAITING_FOR_READ);
Linus Torvalds's avatar
Linus Torvalds committed
82 83 84 85

	loop = woken;
	woken *= RWSEM_ACTIVE_BIAS-RWSEM_WAITING_BIAS;
	woken -= RWSEM_ACTIVE_BIAS;
Linus Torvalds's avatar
Linus Torvalds committed
86
	rwsem_atomic_add(woken,sem);
Linus Torvalds's avatar
Linus Torvalds committed
87

Linus Torvalds's avatar
Linus Torvalds committed
88
	next = sem->wait_list.next;
Linus Torvalds's avatar
Linus Torvalds committed
89
	for (; loop>0; loop--) {
Linus Torvalds's avatar
Linus Torvalds committed
90 91
		waiter = list_entry(next,struct rwsem_waiter,list);
		next = waiter->list.next;
Linus Torvalds's avatar
Linus Torvalds committed
92 93 94 95
		waiter->flags = 0;
		wake_up_process(waiter->task);
	}

Linus Torvalds's avatar
Linus Torvalds committed
96 97
	sem->wait_list.next = next;
	next->prev = &sem->wait_list;
Linus Torvalds's avatar
Linus Torvalds committed
98 99 100 101

 out:
	rwsemtrace(sem,"Leaving __rwsem_do_wake");
	return sem;
Linus Torvalds's avatar
Linus Torvalds committed
102 103 104 105 106 107

	/* undo the change to count, but check for a transition 1->0 */
 undo:
	if (rwsem_atomic_update(-RWSEM_ACTIVE_BIAS,sem)!=0)
		goto out;
	goto try_again;
Linus Torvalds's avatar
Linus Torvalds committed
108 109
}

Linus Torvalds's avatar
Linus Torvalds committed
110
/*
Linus Torvalds's avatar
Linus Torvalds committed
111
 * wait for a lock to be granted
Linus Torvalds's avatar
Linus Torvalds committed
112
 */
Linus Torvalds's avatar
Linus Torvalds committed
113 114 115
static inline struct rw_semaphore *rwsem_down_failed_common(struct rw_semaphore *sem,
								 struct rwsem_waiter *waiter,
								 __s32 adjustment)
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119
{
	struct task_struct *tsk = current;
	signed long count;

Linus Torvalds's avatar
Linus Torvalds committed
120
	set_task_state(tsk,TASK_UNINTERRUPTIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
121

Linus Torvalds's avatar
Linus Torvalds committed
122 123
	/* set up my own style of waitqueue */
	spin_lock(&sem->wait_lock);
Linus Torvalds's avatar
Linus Torvalds committed
124
	waiter->task = tsk;
Linus Torvalds's avatar
Linus Torvalds committed
125

Linus Torvalds's avatar
Linus Torvalds committed
126
	list_add_tail(&waiter->list,&sem->wait_list);
Linus Torvalds's avatar
Linus Torvalds committed
127 128

	/* note that we're now waiting on the lock, but no longer actively read-locking */
Linus Torvalds's avatar
Linus Torvalds committed
129
	count = rwsem_atomic_update(adjustment,sem);
Linus Torvalds's avatar
Linus Torvalds committed
130 131 132 133 134

	/* if there are no longer active locks, wake the front queued process(es) up
	 * - it might even be this process, since the waker takes a more active part
	 */
	if (!(count & RWSEM_ACTIVE_MASK))
Linus Torvalds's avatar
Linus Torvalds committed
135
		sem = __rwsem_do_wake(sem);
Linus Torvalds's avatar
Linus Torvalds committed
136 137

	spin_unlock(&sem->wait_lock);
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140

	/* wait to be given the lock */
	for (;;) {
Linus Torvalds's avatar
Linus Torvalds committed
141
		if (!waiter->flags)
Linus Torvalds's avatar
Linus Torvalds committed
142 143 144 145 146 147 148
			break;
		schedule();
		set_task_state(tsk, TASK_UNINTERRUPTIBLE);
	}

	tsk->state = TASK_RUNNING;

Linus Torvalds's avatar
Linus Torvalds committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	return sem;
}

/*
 * wait for the read lock to be granted
 */
struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem)
{
	struct rwsem_waiter waiter;

	rwsemtrace(sem,"Entering rwsem_down_read_failed");

	waiter.flags = RWSEM_WAITING_FOR_READ;
	rwsem_down_failed_common(sem,&waiter,RWSEM_WAITING_BIAS-RWSEM_ACTIVE_BIAS);

Linus Torvalds's avatar
Linus Torvalds committed
164
	rwsemtrace(sem,"Leaving rwsem_down_read_failed");
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168 169 170 171 172
	return sem;
}

/*
 * wait for the write lock to be granted
 */
struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem)
{
Linus Torvalds's avatar
Linus Torvalds committed
173
	struct rwsem_waiter waiter;
Linus Torvalds's avatar
Linus Torvalds committed
174

Linus Torvalds's avatar
Linus Torvalds committed
175
	rwsemtrace(sem,"Entering rwsem_down_write_failed");
Linus Torvalds's avatar
Linus Torvalds committed
176

Linus Torvalds's avatar
Linus Torvalds committed
177
	waiter.flags = RWSEM_WAITING_FOR_WRITE;
Linus Torvalds's avatar
Linus Torvalds committed
178
	rwsem_down_failed_common(sem,&waiter,-RWSEM_ACTIVE_BIAS);
Linus Torvalds's avatar
Linus Torvalds committed
179

Linus Torvalds's avatar
Linus Torvalds committed
180
	rwsemtrace(sem,"Leaving rwsem_down_write_failed");
Linus Torvalds's avatar
Linus Torvalds committed
181 182 183 184
	return sem;
}

/*
Linus Torvalds's avatar
Linus Torvalds committed
185 186
 * handle waking up a waiter on the semaphore
 * - up_read has decremented the active part of the count if we come here
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189
 */
struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)
{
Linus Torvalds's avatar
Linus Torvalds committed
190
	rwsemtrace(sem,"Entering rwsem_wake");
Linus Torvalds's avatar
Linus Torvalds committed
191

Linus Torvalds's avatar
Linus Torvalds committed
192
	spin_lock(&sem->wait_lock);
Linus Torvalds's avatar
Linus Torvalds committed
193

Linus Torvalds's avatar
Linus Torvalds committed
194 195 196
	/* do nothing if list empty */
	if (!list_empty(&sem->wait_list))
		sem = __rwsem_do_wake(sem);
Linus Torvalds's avatar
Linus Torvalds committed
197

Linus Torvalds's avatar
Linus Torvalds committed
198
	spin_unlock(&sem->wait_lock);
Linus Torvalds's avatar
Linus Torvalds committed
199

Linus Torvalds's avatar
Linus Torvalds committed
200
	rwsemtrace(sem,"Leaving rwsem_wake");
Linus Torvalds's avatar
Linus Torvalds committed
201

Linus Torvalds's avatar
Linus Torvalds committed
202 203 204 205 206 207
	return sem;
}

EXPORT_SYMBOL(rwsem_down_read_failed);
EXPORT_SYMBOL(rwsem_down_write_failed);
EXPORT_SYMBOL(rwsem_wake);
Linus Torvalds's avatar
Linus Torvalds committed
208 209 210
#if RWSEM_DEBUG
EXPORT_SYMBOL(rwsemtrace);
#endif