mrlock.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2000-2005 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. ( (mrp)->mr_writer = 0, init_rwsem(&(mrp)->mr_lock) )
  28. #define mrlock_init(mrp, t,n,s) mrinit(mrp, n)
  29. #define mrfree(mrp) do { } while (0)
  30. #define mraccess(mrp) mraccessf(mrp, 0)
  31. #define mrupdate(mrp) mrupdatef(mrp, 0)
  32. static inline void mraccessf(mrlock_t *mrp, int flags)
  33. {
  34. down_read(&mrp->mr_lock);
  35. }
  36. static inline void mrupdatef(mrlock_t *mrp, int flags)
  37. {
  38. down_write(&mrp->mr_lock);
  39. mrp->mr_writer = 1;
  40. }
  41. static inline int mrtryaccess(mrlock_t *mrp)
  42. {
  43. return down_read_trylock(&mrp->mr_lock);
  44. }
  45. static inline int mrtryupdate(mrlock_t *mrp)
  46. {
  47. if (!down_write_trylock(&mrp->mr_lock))
  48. return 0;
  49. mrp->mr_writer = 1;
  50. return 1;
  51. }
  52. static inline void mrunlock(mrlock_t *mrp)
  53. {
  54. if (mrp->mr_writer) {
  55. mrp->mr_writer = 0;
  56. up_write(&mrp->mr_lock);
  57. } else {
  58. up_read(&mrp->mr_lock);
  59. }
  60. }
  61. static inline void mrdemote(mrlock_t *mrp)
  62. {
  63. mrp->mr_writer = 0;
  64. downgrade_write(&mrp->mr_lock);
  65. }
  66. #ifdef DEBUG
  67. /*
  68. * Debug-only routine, without some platform-specific asm code, we can
  69. * now only answer requests regarding whether we hold the lock for write
  70. * (reader state is outside our visibility, we only track writer state).
  71. * Note: means !ismrlocked would give false positives, so don't do that.
  72. */
  73. static inline int ismrlocked(mrlock_t *mrp, int type)
  74. {
  75. if (mrp && type == MR_UPDATE)
  76. return mrp->mr_writer;
  77. return 1;
  78. }
  79. #endif
  80. #endif /* __XFS_SUPPORT_MRLOCK_H__ */