prolock.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef prolock_h___
  6. #define prolock_h___
  7. #include "prtypes.h"
  8. PR_BEGIN_EXTERN_C
  9. /*
  10. ** A locking mechanism, built on the existing PRLock definition,
  11. ** is provided that will permit applications to define a Lock
  12. ** Hierarchy (or Lock Ordering) schema. An application designed
  13. ** using the Ordered Lock functions will terminate with a
  14. ** diagnostic message when a lock inversion condition is
  15. ** detected.
  16. **
  17. ** The lock ordering detection is compile-time enabled only. In
  18. ** optimized builds of NSPR, the Ordered Lock functions map
  19. ** directly to PRLock functions, providing no lock order
  20. ** detection.
  21. **
  22. ** The Ordered Lock Facility is compiled in when DEBUG is defined at
  23. ** compile-time. Ordered Lock can be forced on in optimized builds by
  24. ** defining FORCE_NSPR_ORDERED_LOCK at compile-time. Both the
  25. ** application using Ordered Lock and NSPR must be compiled with the
  26. ** facility enabled to achieve the desired results.
  27. **
  28. ** Application designers should use the macro interfaces to the Ordered
  29. ** Lock facility to ensure that it is compiled out in optimized builds.
  30. **
  31. ** Application designers are responsible for defining their own
  32. ** lock hierarchy.
  33. **
  34. ** Ordered Lock is thread-safe and SMP safe.
  35. **
  36. ** See Also: prlock.h
  37. **
  38. ** /lth. 10-Jun-1998.
  39. **
  40. */
  41. /*
  42. ** Opaque type for ordered lock.
  43. ** ... Don't even think of looking in here.
  44. **
  45. */
  46. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  47. typedef void * PROrderedLock;
  48. #else
  49. /*
  50. ** Map PROrderedLock and methods onto PRLock when ordered locking
  51. ** is not compiled in.
  52. **
  53. */
  54. #include "prlock.h"
  55. typedef PRLock PROrderedLock;
  56. #endif
  57. /* -----------------------------------------------------------------------
  58. ** FUNCTION: PR_CreateOrderedLock() -- Create an Ordered Lock
  59. **
  60. ** DESCRIPTION: PR_CreateOrderedLock() creates an ordered lock.
  61. **
  62. ** INPUTS:
  63. ** order: user defined order of this lock.
  64. ** name: name of the lock. For debugging purposes.
  65. **
  66. ** OUTPUTS: returned
  67. **
  68. ** RETURNS: PR_OrderedLock pointer
  69. **
  70. ** RESTRICTIONS:
  71. **
  72. */
  73. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  74. #define PR_CREATE_ORDERED_LOCK(order,name)\
  75. PR_CreateOrderedLock((order),(name))
  76. #else
  77. #define PR_CREATE_ORDERED_LOCK(order) PR_NewLock()
  78. #endif
  79. NSPR_API(PROrderedLock *)
  80. PR_CreateOrderedLock(
  81. PRInt32 order,
  82. const char *name
  83. );
  84. /* -----------------------------------------------------------------------
  85. ** FUNCTION: PR_DestroyOrderedLock() -- Destroy an Ordered Lock
  86. **
  87. ** DESCRIPTION: PR_DestroyOrderedLock() destroys the ordered lock
  88. ** referenced by lock.
  89. **
  90. ** INPUTS: lock: pointer to a PROrderedLock
  91. **
  92. ** OUTPUTS: the lock is destroyed
  93. **
  94. ** RETURNS: void
  95. **
  96. ** RESTRICTIONS:
  97. **
  98. */
  99. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  100. #define PR_DESTROY_ORDERED_LOCK(lock) PR_DestroyOrderedLock((lock))
  101. #else
  102. #define PR_DESTROY_ORDERED_LOCK(lock) PR_DestroyLock((lock))
  103. #endif
  104. NSPR_API(void)
  105. PR_DestroyOrderedLock(
  106. PROrderedLock *lock
  107. );
  108. /* -----------------------------------------------------------------------
  109. ** FUNCTION: PR_LockOrderedLock() -- Lock an ordered lock
  110. **
  111. ** DESCRIPTION: PR_LockOrderedLock() locks the ordered lock
  112. ** referenced by lock. If the order of lock is less than or equal
  113. ** to the order of the highest lock held by the locking thread,
  114. ** the function asserts.
  115. **
  116. ** INPUTS: lock: a pointer to a PROrderedLock
  117. **
  118. ** OUTPUTS: The lock is held or the function asserts.
  119. **
  120. ** RETURNS: void
  121. **
  122. ** RESTRICTIONS:
  123. **
  124. */
  125. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  126. #define PR_LOCK_ORDERED_LOCK(lock) PR_LockOrderedLock((lock))
  127. #else
  128. #define PR_LOCK_ORDERED_LOCK(lock) PR_Lock((lock))
  129. #endif
  130. NSPR_API(void)
  131. PR_LockOrderedLock(
  132. PROrderedLock *lock
  133. );
  134. /* -----------------------------------------------------------------------
  135. ** FUNCTION: PR_UnlockOrderedLock() -- unlock and Ordered Lock
  136. **
  137. ** DESCRIPTION: PR_UnlockOrderedLock() unlocks the lock referenced
  138. ** by lock.
  139. **
  140. ** INPUTS: lock: a pointer to a PROrderedLock
  141. **
  142. ** OUTPUTS: the lock is unlocked
  143. **
  144. ** RETURNS:
  145. ** PR_SUCCESS
  146. ** PR_FAILURE
  147. **
  148. ** RESTRICTIONS:
  149. **
  150. */
  151. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  152. #define PR_UNLOCK_ORDERED_LOCK(lock) PR_UnlockOrderedLock((lock))
  153. #else
  154. #define PR_UNLOCK_ORDERED_LOCK(lock) PR_Unlock((lock))
  155. #endif
  156. NSPR_API(PRStatus)
  157. PR_UnlockOrderedLock(
  158. PROrderedLock *lock
  159. );
  160. PR_END_EXTERN_C
  161. #endif /* prolock_h___ */