mrlock.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include <linux/rwsem.h>
  35. enum { MR_NONE, MR_ACCESS, MR_UPDATE };
  36. typedef struct {
  37. struct rw_semaphore mr_lock;
  38. int mr_writer;
  39. } mrlock_t;
  40. #define mrinit(mrp, name) \
  41. ( (mrp)->mr_writer = 0, init_rwsem(&(mrp)->mr_lock) )
  42. #define mrlock_init(mrp, t,n,s) mrinit(mrp, n)
  43. #define mrfree(mrp) do { } while (0)
  44. #define mraccess(mrp) mraccessf(mrp, 0)
  45. #define mrupdate(mrp) mrupdatef(mrp, 0)
  46. static inline void mraccessf(mrlock_t *mrp, int flags)
  47. {
  48. down_read(&mrp->mr_lock);
  49. }
  50. static inline void mrupdatef(mrlock_t *mrp, int flags)
  51. {
  52. down_write(&mrp->mr_lock);
  53. mrp->mr_writer = 1;
  54. }
  55. static inline int mrtryaccess(mrlock_t *mrp)
  56. {
  57. return down_read_trylock(&mrp->mr_lock);
  58. }
  59. static inline int mrtryupdate(mrlock_t *mrp)
  60. {
  61. if (!down_write_trylock(&mrp->mr_lock))
  62. return 0;
  63. mrp->mr_writer = 1;
  64. return 1;
  65. }
  66. static inline void mrunlock(mrlock_t *mrp)
  67. {
  68. if (mrp->mr_writer) {
  69. mrp->mr_writer = 0;
  70. up_write(&mrp->mr_lock);
  71. } else {
  72. up_read(&mrp->mr_lock);
  73. }
  74. }
  75. static inline void mrdemote(mrlock_t *mrp)
  76. {
  77. mrp->mr_writer = 0;
  78. downgrade_write(&mrp->mr_lock);
  79. }
  80. #ifdef DEBUG
  81. /*
  82. * Debug-only routine, without some platform-specific asm code, we can
  83. * now only answer requests regarding whether we hold the lock for write
  84. * (reader state is outside our visibility, we only track writer state).
  85. * Note: means !ismrlocked would give false positivies, so don't do that.
  86. */
  87. static inline int ismrlocked(mrlock_t *mrp, int type)
  88. {
  89. if (mrp && type == MR_UPDATE)
  90. return mrp->mr_writer;
  91. return 1;
  92. }
  93. #endif
  94. #endif /* __XFS_SUPPORT_MRLOCK_H__ */