blob: af168a1a98c1bf5ad6a56e1f2f513bca584f632d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ingo Molnar72c93bc2006-06-09 14:57:01 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_SUPPORT_MRLOCK_H__
19#define __XFS_SUPPORT_MRLOCK_H__
20
21#include <linux/rwsem.h>
22
23enum { MR_NONE, MR_ACCESS, MR_UPDATE };
24
25typedef struct {
26 struct rw_semaphore mr_lock;
27 int mr_writer;
28} mrlock_t;
29
30#define mrinit(mrp, name) \
Ingo Molnar72c93bc2006-06-09 14:57:01 +100031 do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define mrlock_init(mrp, t,n,s) mrinit(mrp, n)
33#define mrfree(mrp) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Lachlan McIlroya3227fb2007-02-10 18:35:40 +110035static inline void mraccess(mrlock_t *mrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 down_read(&mrp->mr_lock);
38}
39
Lachlan McIlroya3227fb2007-02-10 18:35:40 +110040static inline void mrupdate(mrlock_t *mrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 down_write(&mrp->mr_lock);
43 mrp->mr_writer = 1;
44}
45
46static inline int mrtryaccess(mrlock_t *mrp)
47{
48 return down_read_trylock(&mrp->mr_lock);
49}
50
51static inline int mrtryupdate(mrlock_t *mrp)
52{
53 if (!down_write_trylock(&mrp->mr_lock))
54 return 0;
55 mrp->mr_writer = 1;
56 return 1;
57}
58
59static inline void mrunlock(mrlock_t *mrp)
60{
61 if (mrp->mr_writer) {
62 mrp->mr_writer = 0;
63 up_write(&mrp->mr_lock);
64 } else {
65 up_read(&mrp->mr_lock);
66 }
67}
68
69static inline void mrdemote(mrlock_t *mrp)
70{
71 mrp->mr_writer = 0;
72 downgrade_write(&mrp->mr_lock);
73}
74
75#ifdef DEBUG
76/*
77 * Debug-only routine, without some platform-specific asm code, we can
78 * now only answer requests regarding whether we hold the lock for write
79 * (reader state is outside our visibility, we only track writer state).
Nathan Scottc41564b2006-03-29 08:55:14 +100080 * Note: means !ismrlocked would give false positives, so don't do that.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82static inline int ismrlocked(mrlock_t *mrp, int type)
83{
84 if (mrp && type == MR_UPDATE)
85 return mrp->mr_writer;
86 return 1;
87}
88#endif
89
90#endif /* __XFS_SUPPORT_MRLOCK_H__ */