mrlock.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  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
  7. * published by the Free Software Foundation.
  8. *
  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.
  13. *
  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
  17. */
  18. #ifndef __XFS_SUPPORT_MRLOCK_H__
  19. #define __XFS_SUPPORT_MRLOCK_H__
  20. #include <linux/rwsem.h>
  21. typedef struct {
  22. struct rw_semaphore mr_lock;
  23. #if defined(DEBUG) || defined(XFS_WARN)
  24. int mr_writer;
  25. #endif
  26. } mrlock_t;
  27. #if defined(DEBUG) || defined(XFS_WARN)
  28. #define mrinit(mrp, name) \
  29. do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0)
  30. #else
  31. #define mrinit(mrp, name) \
  32. do { init_rwsem(&(mrp)->mr_lock); } while (0)
  33. #endif
  34. #define mrlock_init(mrp, t,n,s) mrinit(mrp, n)
  35. #define mrfree(mrp) do { } while (0)
  36. static inline void mraccess_nested(mrlock_t *mrp, int subclass)
  37. {
  38. down_read_nested(&mrp->mr_lock, subclass);
  39. }
  40. static inline void mrupdate_nested(mrlock_t *mrp, int subclass)
  41. {
  42. down_write_nested(&mrp->mr_lock, subclass);
  43. #if defined(DEBUG) || defined(XFS_WARN)
  44. mrp->mr_writer = 1;
  45. #endif
  46. }
  47. static inline int mrtryaccess(mrlock_t *mrp)
  48. {
  49. return down_read_trylock(&mrp->mr_lock);
  50. }
  51. static inline int mrtryupdate(mrlock_t *mrp)
  52. {
  53. if (!down_write_trylock(&mrp->mr_lock))
  54. return 0;
  55. #if defined(DEBUG) || defined(XFS_WARN)
  56. mrp->mr_writer = 1;
  57. #endif
  58. return 1;
  59. }
  60. static inline void mrunlock_excl(mrlock_t *mrp)
  61. {
  62. #if defined(DEBUG) || defined(XFS_WARN)
  63. mrp->mr_writer = 0;
  64. #endif
  65. up_write(&mrp->mr_lock);
  66. }
  67. static inline void mrunlock_shared(mrlock_t *mrp)
  68. {
  69. up_read(&mrp->mr_lock);
  70. }
  71. static inline void mrdemote(mrlock_t *mrp)
  72. {
  73. #if defined(DEBUG) || defined(XFS_WARN)
  74. mrp->mr_writer = 0;
  75. #endif
  76. downgrade_write(&mrp->mr_lock);
  77. }
  78. #endif /* __XFS_SUPPORT_MRLOCK_H__ */