Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / linux-2.6 / mrlock.h
blobd2c11a098ff26cf2364bac11126d32b76084e0bd
1 /*
2 * Copyright (c) 2000-2004 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:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32 #ifndef __XFS_SUPPORT_MRLOCK_H__
33 #define __XFS_SUPPORT_MRLOCK_H__
35 #include <linux/rwsem.h>
37 enum { MR_NONE, MR_ACCESS, MR_UPDATE };
39 typedef struct {
40 struct rw_semaphore mr_lock;
41 int mr_writer;
42 } mrlock_t;
44 #define mrinit(mrp, name) \
45 ( (mrp)->mr_writer = 0, init_rwsem(&(mrp)->mr_lock) )
46 #define mrlock_init(mrp, t,n,s) mrinit(mrp, n)
47 #define mrfree(mrp) do { } while (0)
48 #define mraccess(mrp) mraccessf(mrp, 0)
49 #define mrupdate(mrp) mrupdatef(mrp, 0)
51 static inline void mraccessf(mrlock_t *mrp, int flags)
53 down_read(&mrp->mr_lock);
56 static inline void mrupdatef(mrlock_t *mrp, int flags)
58 down_write(&mrp->mr_lock);
59 mrp->mr_writer = 1;
62 static inline int mrtryaccess(mrlock_t *mrp)
64 return down_read_trylock(&mrp->mr_lock);
67 static inline int mrtryupdate(mrlock_t *mrp)
69 if (!down_write_trylock(&mrp->mr_lock))
70 return 0;
71 mrp->mr_writer = 1;
72 return 1;
75 static inline void mrunlock(mrlock_t *mrp)
77 if (mrp->mr_writer) {
78 mrp->mr_writer = 0;
79 up_write(&mrp->mr_lock);
80 } else {
81 up_read(&mrp->mr_lock);
85 static inline void mrdemote(mrlock_t *mrp)
87 mrp->mr_writer = 0;
88 downgrade_write(&mrp->mr_lock);
91 #ifdef DEBUG
93 * Debug-only routine, without some platform-specific asm code, we can
94 * now only answer requests regarding whether we hold the lock for write
95 * (reader state is outside our visibility, we only track writer state).
96 * Note: means !ismrlocked would give false positivies, so don't do that.
98 static inline int ismrlocked(mrlock_t *mrp, int type)
100 if (mrp && type == MR_UPDATE)
101 return mrp->mr_writer;
102 return 1;
104 #endif
106 #endif /* __XFS_SUPPORT_MRLOCK_H__ */