mrlock.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. enum { MR_NONE, MR_ACCESS, MR_UPDATE };
  22. typedef struct {
  23. struct rw_semaphore mr_lock;
  24. int mr_writer;
  25. } mrlock_t;
  26. #define mrinit(mrp, name) \
  27. do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0)
  28. #define mrlock_init(mrp, t,n,s) mrinit(mrp, n)
  29. #define mrfree(mrp) do { } while (0)
  30. static inline void mraccess(mrlock_t *mrp)
  31. {
  32. down_read(&mrp->mr_lock);
  33. }
  34. static inline void mrupdate(mrlock_t *mrp)
  35. {
  36. down_write(&mrp->mr_lock);
  37. mrp->mr_writer = 1;
  38. }
  39. static inline void mraccess_nested(mrlock_t *mrp, int subclass)
  40. {
  41. down_read_nested(&mrp->mr_lock, subclass);
  42. }
  43. static inline void mrupdate_nested(mrlock_t *mrp, int subclass)
  44. {
  45. down_write_nested(&mrp->mr_lock, subclass);
  46. mrp->mr_writer = 1;
  47. }
  48. static inline int mrtryaccess(mrlock_t *mrp)
  49. {
  50. return down_read_trylock(&mrp->mr_lock);
  51. }
  52. static inline int mrtryupdate(mrlock_t *mrp)
  53. {
  54. if (!down_write_trylock(&mrp->mr_lock))
  55. return 0;
  56. mrp->mr_writer = 1;
  57. return 1;
  58. }
  59. static 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. static inline void mrdemote(mrlock_t *mrp)
  69. {
  70. mrp->mr_writer = 0;
  71. downgrade_write(&mrp->mr_lock);
  72. }
  73. #ifdef DEBUG
  74. /*
  75. * Debug-only routine, without some platform-specific asm code, we can
  76. * now only answer requests regarding whether we hold the lock for write
  77. * (reader state is outside our visibility, we only track writer state).
  78. * Note: means !ismrlocked would give false positives, so don't do that.
  79. */
  80. static inline int ismrlocked(mrlock_t *mrp, int type)
  81. {
  82. if (mrp && type == MR_UPDATE)
  83. return mrp->mr_writer;
  84. return 1;
  85. }
  86. #endif
  87. #endif /* __XFS_SUPPORT_MRLOCK_H__ */