prolock.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the Netscape Portable Runtime (NSPR).
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998-2000
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef prolock_h___
  38. #define prolock_h___
  39. #include "prtypes.h"
  40. PR_BEGIN_EXTERN_C
  41. /*
  42. ** A locking mechanism, built on the existing PRLock definiion,
  43. ** is provided that will permit applications to define a Lock
  44. ** Hierarchy (or Lock Ordering) schema. An application designed
  45. ** using the Ordered Lock functions will terminate with a
  46. ** diagnostic message when a lock inversion condition is
  47. ** detected.
  48. **
  49. ** The lock ordering detection is complile-time enabled only. in
  50. ** optimized builds of NSPR, the Ordered Lock functions map
  51. ** directly to PRLock functions, providing no lock order
  52. ** detection.
  53. **
  54. ** The Ordered Lock Facility is compiled in when DEBUG is defined at
  55. ** compile time. Ordered Lock can be forced on in optimized builds by
  56. ** defining FORCE_NSPR_ORDERED_LOCK at compile time. Both the
  57. ** application using Ordered Lock and NSPR must be compiled with the
  58. ** facility enabled to achieve the desired results.
  59. **
  60. ** Application designers should use the macro interfaces to the Ordered
  61. ** Lock facility to ensure that it is compiled out in optimized builds.
  62. **
  63. ** Application designers are responsible for defining their own
  64. ** lock hierarchy.
  65. **
  66. ** Ordered Lock is thread-safe and SMP safe.
  67. **
  68. ** See Also: prlock.h
  69. **
  70. ** /lth. 10-Jun-1998.
  71. **
  72. */
  73. /*
  74. ** Opaque type for ordered lock.
  75. ** ... Don't even think of looking in here.
  76. **
  77. */
  78. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  79. typedef void * PROrderedLock;
  80. #else
  81. /*
  82. ** Map PROrderedLock and methods onto PRLock when ordered locking
  83. ** is not compiled in.
  84. **
  85. */
  86. #include "prlock.h"
  87. typedef PRLock PROrderedLock;
  88. #endif
  89. /* -----------------------------------------------------------------------
  90. ** FUNCTION: PR_CreateOrderedLock() -- Create an Ordered Lock
  91. **
  92. ** DESCRIPTION: PR_CreateOrderedLock() creates an ordered lock.
  93. **
  94. ** INPUTS:
  95. ** order: user defined order of this lock.
  96. ** name: name of the lock. For debugging purposes.
  97. **
  98. ** OUTPUTS: returned
  99. **
  100. ** RETURNS: PR_OrderedLock pointer
  101. **
  102. ** RESTRICTIONS:
  103. **
  104. */
  105. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  106. #define PR_CREATE_ORDERED_LOCK(order,name)\
  107. PR_CreateOrderedLock((order),(name))
  108. #else
  109. #define PR_CREATE_ORDERED_LOCK(order) PR_NewLock()
  110. #endif
  111. NSPR_API(PROrderedLock *)
  112. PR_CreateOrderedLock(
  113. PRInt32 order,
  114. const char *name
  115. );
  116. /* -----------------------------------------------------------------------
  117. ** FUNCTION: PR_DestroyOrderedLock() -- Destroy an Ordered Lock
  118. **
  119. ** DESCRIPTION: PR_DestroyOrderedLock() destroys the ordered lock
  120. ** referenced by lock.
  121. **
  122. ** INPUTS: lock: pointer to a PROrderedLock
  123. **
  124. ** OUTPUTS: the lock is destroyed
  125. **
  126. ** RETURNS: void
  127. **
  128. ** RESTRICTIONS:
  129. **
  130. */
  131. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  132. #define PR_DESTROY_ORDERED_LOCK(lock) PR_DestroyOrderedLock((lock))
  133. #else
  134. #define PR_DESTROY_ORDERED_LOCK(lock) PR_DestroyLock((lock))
  135. #endif
  136. NSPR_API(void)
  137. PR_DestroyOrderedLock(
  138. PROrderedLock *lock
  139. );
  140. /* -----------------------------------------------------------------------
  141. ** FUNCTION: PR_LockOrderedLock() -- Lock an ordered lock
  142. **
  143. ** DESCRIPTION: PR_LockOrderedLock() locks the ordered lock
  144. ** referenced by lock. If the order of lock is less than or equal
  145. ** to the order of the highest lock held by the locking thread,
  146. ** the function asserts.
  147. **
  148. ** INPUTS: lock: a pointer to a PROrderedLock
  149. **
  150. ** OUTPUTS: The lock is held or the function asserts.
  151. **
  152. ** RETURNS: void
  153. **
  154. ** RESTRICTIONS:
  155. **
  156. */
  157. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  158. #define PR_LOCK_ORDERED_LOCK(lock) PR_LockOrderedLock((lock))
  159. #else
  160. #define PR_LOCK_ORDERED_LOCK(lock) PR_Lock((lock))
  161. #endif
  162. NSPR_API(void)
  163. PR_LockOrderedLock(
  164. PROrderedLock *lock
  165. );
  166. /* -----------------------------------------------------------------------
  167. ** FUNCTION: PR_UnlockOrderedLock() -- unlock and Ordered Lock
  168. **
  169. ** DESCRIPTION: PR_UnlockOrderedLock() unlocks the lock referenced
  170. ** by lock.
  171. **
  172. ** INPUTS: lock: a pointer to a PROrderedLock
  173. **
  174. ** OUTPUTS: the lock is unlocked
  175. **
  176. ** RETURNS:
  177. ** PR_SUCCESS
  178. ** PR_FAILURE
  179. **
  180. ** RESTRICTIONS:
  181. **
  182. */
  183. #if defined(DEBUG) || defined(FORCE_NSPR_ORDERED_LOCKS)
  184. #define PR_UNLOCK_ORDERED_LOCK(lock) PR_UnlockOrderedLock((lock))
  185. #else
  186. #define PR_UNLOCK_ORDERED_LOCK(lock) PR_Unlock((lock))
  187. #endif
  188. NSPR_API(PRStatus)
  189. PR_UnlockOrderedLock(
  190. PROrderedLock *lock
  191. );
  192. PR_END_EXTERN_C
  193. #endif /* prolock_h___ */