prlock.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /*
  6. ** File: prlock.h
  7. ** Description: API to basic locking functions of NSPR.
  8. **
  9. **
  10. ** NSPR provides basic locking mechanisms for thread synchronization. Locks
  11. ** are lightweight resource contention controls that prevent multiple threads
  12. ** from accessing something (code/data) simultaneously.
  13. **/
  14. #ifndef prlock_h___
  15. #define prlock_h___
  16. #include "prtypes.h"
  17. PR_BEGIN_EXTERN_C
  18. /**********************************************************************/
  19. /************************* TYPES AND CONSTANTS ************************/
  20. /**********************************************************************/
  21. /*
  22. * PRLock --
  23. *
  24. * NSPR represents the lock as an opaque entity to the client of the
  25. * API. All routines operate on a pointer to this opaque entity.
  26. */
  27. typedef struct PRLock PRLock;
  28. /**********************************************************************/
  29. /****************************** FUNCTIONS *****************************/
  30. /**********************************************************************/
  31. /***********************************************************************
  32. ** FUNCTION: PR_NewLock
  33. ** DESCRIPTION:
  34. ** Returns a pointer to a newly created opaque lock object.
  35. ** INPUTS: void
  36. ** OUTPUTS: void
  37. ** RETURN: PRLock*
  38. ** If the lock can not be created because of resource constraints, NULL
  39. ** is returned.
  40. **
  41. ***********************************************************************/
  42. NSPR_API(PRLock*) PR_NewLock(void);
  43. /***********************************************************************
  44. ** FUNCTION: PR_DestroyLock
  45. ** DESCRIPTION:
  46. ** Destroys a given opaque lock object.
  47. ** INPUTS: PRLock *lock
  48. ** Lock to be freed.
  49. ** OUTPUTS: void
  50. ** RETURN: None
  51. ***********************************************************************/
  52. NSPR_API(void) PR_DestroyLock(PRLock *lock);
  53. /***********************************************************************
  54. ** FUNCTION: PR_Lock
  55. ** DESCRIPTION:
  56. ** Lock a lock.
  57. ** INPUTS: PRLock *lock
  58. ** Lock to locked.
  59. ** OUTPUTS: void
  60. ** RETURN: None
  61. ***********************************************************************/
  62. NSPR_API(void) PR_Lock(PRLock *lock);
  63. /***********************************************************************
  64. ** FUNCTION: PR_Unlock
  65. ** DESCRIPTION:
  66. ** Unlock a lock. Unlocking an unlocked lock has undefined results.
  67. ** INPUTS: PRLock *lock
  68. ** Lock to unlocked.
  69. ** OUTPUTS: void
  70. ** RETURN: PR_STATUS
  71. ** Returns PR_FAILURE if the caller does not own the lock.
  72. ***********************************************************************/
  73. NSPR_API(PRStatus) PR_Unlock(PRLock *lock);
  74. /***********************************************************************
  75. ** MACRO: PR_ASSERT_CURRENT_THREAD_OWNS_LOCK
  76. ** DESCRIPTION:
  77. ** If the current thread owns |lock|, this assertion is guaranteed to
  78. ** succeed. Otherwise, the behavior of this function is undefined.
  79. ** INPUTS: PRLock *lock
  80. ** Lock to assert ownership of.
  81. ** OUTPUTS: void
  82. ** RETURN: None
  83. ***********************************************************************/
  84. #if defined(DEBUG) || defined(FORCE_PR_ASSERT)
  85. #define PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(/* PrLock* */ lock) \
  86. PR_AssertCurrentThreadOwnsLock(lock)
  87. #else
  88. #define PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(/* PrLock* */ lock)
  89. #endif
  90. /* Don't call this function directly. */
  91. NSPR_API(void) PR_AssertCurrentThreadOwnsLock(PRLock *lock);
  92. PR_END_EXTERN_C
  93. #endif /* prlock_h___ */