2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include <linux/time.h>
34 #include <linux/sched.h>
35 #include <asm/system.h>
36 #include <linux/interrupt.h>
37 #include <asm/current.h>
42 #if USE_RW_WAIT_QUEUE_SPINLOCK
43 # define wq_write_lock write_lock
45 # define wq_write_lock spin_lock
49 * We don't seem to need lock_type (only one supported), name, or
50 * sequence. But, XFS will pass it so let's leave them here for now.
54 mrlock_init(mrlock_t *mrp, int lock_type, char *name, long sequence)
57 mrp->mr_reads_waiting = 0;
58 mrp->mr_writes_waiting = 0;
59 init_waitqueue_head(&mrp->mr_readerq);
60 init_waitqueue_head(&mrp->mr_writerq);
61 mrp->mr_lock = SPIN_LOCK_UNLOCKED;
65 * Macros to lock/unlock the mrlock_t.
68 #define MRLOCK(m) spin_lock(&(m)->mr_lock);
69 #define MRUNLOCK(m) spin_unlock(&(m)->mr_lock);
73 * lock_wait should never be called in an interrupt thread.
75 * mrlocks can sleep (i.e. call schedule) and so they can't ever
76 * be called from an interrupt thread.
78 * threads that wake-up should also never be invoked from interrupt threads.
80 * But, waitqueue_lock is locked from interrupt threads - and we are
81 * called with interrupts disabled, so it is all OK.
86 lock_wait(wait_queue_head_t *q, spinlock_t *lock, int rw)
88 DECLARE_WAITQUEUE( wait, current );
90 __set_current_state(TASK_UNINTERRUPTIBLE);
92 wq_write_lock(&q->lock);
94 __add_wait_queue_tail(q, &wait);
96 __add_wait_queue(q, &wait);
99 wq_write_unlock(&q->lock);
104 wq_write_lock(&q->lock);
105 __remove_wait_queue(q, &wait);
106 wq_write_unlock(&q->lock);
110 /* return with lock held */
115 mrfree(mrlock_t *mrp)
121 mrlock(mrlock_t *mrp, int type, int flags)
123 if (type == MR_ACCESS)
131 mraccessf(mrlock_t *mrp, int flags)
134 if(mrp->mr_writes_waiting > 0) {
135 mrp->mr_reads_waiting++;
136 lock_wait(&mrp->mr_readerq, &mrp->mr_lock, 0);
137 mrp->mr_reads_waiting--;
139 while (mrp->mr_count < 0) {
140 mrp->mr_reads_waiting++;
141 lock_wait(&mrp->mr_readerq, &mrp->mr_lock, 0);
142 mrp->mr_reads_waiting--;
150 mrupdatef(mrlock_t *mrp, int flags)
153 while(mrp->mr_count) {
154 mrp->mr_writes_waiting++;
155 lock_wait(&mrp->mr_writerq, &mrp->mr_lock, 1);
156 mrp->mr_writes_waiting--;
159 mrp->mr_count = -1; /* writer on it */
164 mrtryaccess(mrlock_t *mrp)
168 * If anyone is waiting for update access or the lock is held for update
171 if(mrp->mr_writes_waiting > 0 || mrp->mr_count < 0) {
181 mrtrypromote(mrlock_t *mrp)
185 if(mrp->mr_count == 1) { /* We are the only thread with the lock */
186 mrp->mr_count = -1; /* writer on it */
196 mrtryupdate(mrlock_t *mrp)
205 mrp->mr_count = -1; /* writer on it */
210 static __inline__ void mrwake(mrlock_t *mrp)
213 * First, if the count is now 0, we need to wake-up anyone waiting.
215 if (!mrp->mr_count) {
216 if (mrp->mr_writes_waiting) { /* Wake-up first writer waiting */
217 wake_up(&mrp->mr_writerq);
218 } else if (mrp->mr_reads_waiting) { /* Wakeup any readers waiting */
219 wake_up(&mrp->mr_readerq);
225 mraccunlock(mrlock_t *mrp)
234 mrunlock(mrlock_t *mrp)
237 if (mrp->mr_count < 0) {
247 ismrlocked(mrlock_t *mrp, int type) /* No need to lock since info can change */
249 if (type == MR_ACCESS)
250 return (mrp->mr_count > 0); /* Read lock */
251 else if (type == MR_UPDATE)
252 return (mrp->mr_count < 0); /* Write lock */
253 else if (type == (MR_UPDATE | MR_ACCESS))
254 return (mrp->mr_count); /* Any type of lock held */
255 else /* Any waiters */
256 return (mrp->mr_reads_waiting | mrp->mr_writes_waiting);
260 * Demote from update to access. We better be the only thread with the
261 * lock in update mode so it should be easy to set to 1.
262 * Wake-up any readers waiting.
266 mrdemote(mrlock_t *mrp)
270 if (mrp->mr_reads_waiting) { /* Wakeup all readers waiting */
271 wake_up(&mrp->mr_readerq);