Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 11 | * |
| 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. |
| 18 | * |
| 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. |
| 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ |
| 31 | */ |
| 32 | #ifndef __XFS_SUPPORT_MRLOCK_H__ |
| 33 | #define __XFS_SUPPORT_MRLOCK_H__ |
| 34 | |
| 35 | #include <linux/rwsem.h> |
| 36 | |
| 37 | enum { MR_NONE, MR_ACCESS, MR_UPDATE }; |
| 38 | |
| 39 | typedef struct { |
| 40 | struct rw_semaphore mr_lock; |
| 41 | int mr_writer; |
| 42 | } mrlock_t; |
| 43 | |
| 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) |
| 50 | |
| 51 | static inline void mraccessf(mrlock_t *mrp, int flags) |
| 52 | { |
| 53 | down_read(&mrp->mr_lock); |
| 54 | } |
| 55 | |
| 56 | static inline void mrupdatef(mrlock_t *mrp, int flags) |
| 57 | { |
| 58 | down_write(&mrp->mr_lock); |
| 59 | mrp->mr_writer = 1; |
| 60 | } |
| 61 | |
| 62 | static inline int mrtryaccess(mrlock_t *mrp) |
| 63 | { |
| 64 | return down_read_trylock(&mrp->mr_lock); |
| 65 | } |
| 66 | |
| 67 | static inline int mrtryupdate(mrlock_t *mrp) |
| 68 | { |
| 69 | if (!down_write_trylock(&mrp->mr_lock)) |
| 70 | return 0; |
| 71 | mrp->mr_writer = 1; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static inline void mrunlock(mrlock_t *mrp) |
| 76 | { |
| 77 | if (mrp->mr_writer) { |
| 78 | mrp->mr_writer = 0; |
| 79 | up_write(&mrp->mr_lock); |
| 80 | } else { |
| 81 | up_read(&mrp->mr_lock); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static inline void mrdemote(mrlock_t *mrp) |
| 86 | { |
| 87 | mrp->mr_writer = 0; |
| 88 | downgrade_write(&mrp->mr_lock); |
| 89 | } |
| 90 | |
| 91 | #ifdef DEBUG |
| 92 | /* |
| 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. |
| 97 | */ |
| 98 | static inline int ismrlocked(mrlock_t *mrp, int type) |
| 99 | { |
| 100 | if (mrp && type == MR_UPDATE) |
| 101 | return mrp->mr_writer; |
| 102 | return 1; |
| 103 | } |
| 104 | #endif |
| 105 | |
| 106 | #endif /* __XFS_SUPPORT_MRLOCK_H__ */ |