| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Ingo Molnar | 72c93bc | 2006-06-09 14:57:01 +1000 | [diff] [blame] | 2 |  * Copyright (c) 2000-2006 Silicon Graphics, Inc. | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 |  * All Rights Reserved. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  * published by the Free Software Foundation. | 
 | 8 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 |  */ | 
 | 18 | #ifndef __XFS_SUPPORT_MRLOCK_H__ | 
 | 19 | #define __XFS_SUPPORT_MRLOCK_H__ | 
 | 20 |  | 
 | 21 | #include <linux/rwsem.h> | 
 | 22 |  | 
 | 23 | enum { MR_NONE, MR_ACCESS, MR_UPDATE }; | 
 | 24 |  | 
 | 25 | typedef struct { | 
 | 26 | 	struct rw_semaphore	mr_lock; | 
 | 27 | 	int			mr_writer; | 
 | 28 | } mrlock_t; | 
 | 29 |  | 
 | 30 | #define mrinit(mrp, name)	\ | 
| Ingo Molnar | 72c93bc | 2006-06-09 14:57:01 +1000 | [diff] [blame] | 31 | 	do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #define mrlock_init(mrp, t,n,s)	mrinit(mrp, n) | 
 | 33 | #define mrfree(mrp)		do { } while (0) | 
 | 34 | #define mraccess(mrp)		mraccessf(mrp, 0) | 
 | 35 | #define mrupdate(mrp)		mrupdatef(mrp, 0) | 
 | 36 |  | 
 | 37 | static inline void mraccessf(mrlock_t *mrp, int flags) | 
 | 38 | { | 
 | 39 | 	down_read(&mrp->mr_lock); | 
 | 40 | } | 
 | 41 |  | 
 | 42 | static inline void mrupdatef(mrlock_t *mrp, int flags) | 
 | 43 | { | 
 | 44 | 	down_write(&mrp->mr_lock); | 
 | 45 | 	mrp->mr_writer = 1; | 
 | 46 | } | 
 | 47 |  | 
 | 48 | static inline int mrtryaccess(mrlock_t *mrp) | 
 | 49 | { | 
 | 50 | 	return down_read_trylock(&mrp->mr_lock); | 
 | 51 | } | 
 | 52 |  | 
 | 53 | static inline int mrtryupdate(mrlock_t *mrp) | 
 | 54 | { | 
 | 55 | 	if (!down_write_trylock(&mrp->mr_lock)) | 
 | 56 | 		return 0; | 
 | 57 | 	mrp->mr_writer = 1; | 
 | 58 | 	return 1; | 
 | 59 | } | 
 | 60 |  | 
 | 61 | static inline void mrunlock(mrlock_t *mrp) | 
 | 62 | { | 
 | 63 | 	if (mrp->mr_writer) { | 
 | 64 | 		mrp->mr_writer = 0; | 
 | 65 | 		up_write(&mrp->mr_lock); | 
 | 66 | 	} else { | 
 | 67 | 		up_read(&mrp->mr_lock); | 
 | 68 | 	} | 
 | 69 | } | 
 | 70 |  | 
 | 71 | static inline void mrdemote(mrlock_t *mrp) | 
 | 72 | { | 
 | 73 | 	mrp->mr_writer = 0; | 
 | 74 | 	downgrade_write(&mrp->mr_lock); | 
 | 75 | } | 
 | 76 |  | 
 | 77 | #ifdef DEBUG | 
 | 78 | /* | 
 | 79 |  * Debug-only routine, without some platform-specific asm code, we can | 
 | 80 |  * now only answer requests regarding whether we hold the lock for write | 
 | 81 |  * (reader state is outside our visibility, we only track writer state). | 
| Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 82 |  * Note: means !ismrlocked would give false positives, so don't do that. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 |  */ | 
 | 84 | static inline int ismrlocked(mrlock_t *mrp, int type) | 
 | 85 | { | 
 | 86 | 	if (mrp && type == MR_UPDATE) | 
 | 87 | 		return mrp->mr_writer; | 
 | 88 | 	return 1; | 
 | 89 | } | 
 | 90 | #endif | 
 | 91 |  | 
 | 92 | #endif /* __XFS_SUPPORT_MRLOCK_H__ */ |