prmon.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 prmon_h___
  6. #define prmon_h___
  7. #include "prtypes.h"
  8. #include "prinrval.h"
  9. PR_BEGIN_EXTERN_C
  10. typedef struct PRMonitor PRMonitor;
  11. /*
  12. ** Create a new monitor. Monitors are re-entrant locks with a single built-in
  13. ** condition variable.
  14. **
  15. ** This may fail if memory is tight or if some operating system resource
  16. ** is low.
  17. */
  18. NSPR_API(PRMonitor*) PR_NewMonitor(void);
  19. /*
  20. ** Destroy a monitor. The caller is responsible for guaranteeing that the
  21. ** monitor is no longer in use. There must be no thread waiting on the monitor's
  22. ** condition variable and that the lock is not held.
  23. **
  24. */
  25. NSPR_API(void) PR_DestroyMonitor(PRMonitor *mon);
  26. /*
  27. ** Enter the lock associated with the monitor. If the calling thread currently
  28. ** is in the monitor, the call to enter will silently succeed. In either case,
  29. ** it will increment the entry count by one.
  30. */
  31. NSPR_API(void) PR_EnterMonitor(PRMonitor *mon);
  32. /*
  33. ** Decrement the entry count associated with the monitor. If the decremented
  34. ** entry count is zero, the monitor is exited. Returns PR_FAILURE if the
  35. ** calling thread has not entered the monitor.
  36. */
  37. NSPR_API(PRStatus) PR_ExitMonitor(PRMonitor *mon);
  38. /*
  39. ** Wait for a notify on the monitor's condition variable. Sleep for "ticks"
  40. ** amount of time (if "ticks" is PR_INTERVAL_NO_TIMEOUT then the sleep is
  41. ** indefinite).
  42. **
  43. ** While the thread is waiting it exits the monitor (as if it called
  44. ** PR_ExitMonitor as many times as it had called PR_EnterMonitor). When
  45. ** the wait has finished the thread regains control of the monitors lock
  46. ** with the same entry count as before the wait began.
  47. **
  48. ** The thread waiting on the monitor will be resumed when the monitor is
  49. ** notified (assuming the thread is the next in line to receive the
  50. ** notify) or when the "ticks" timeout elapses.
  51. **
  52. ** Returns PR_FAILURE if the caller has not entered the monitor.
  53. */
  54. NSPR_API(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks);
  55. /*
  56. ** Notify a thread waiting on the monitor's condition variable. If a thread
  57. ** is waiting on the condition variable (using PR_Wait) then it is awakened
  58. ** and attempts to reenter the monitor.
  59. */
  60. NSPR_API(PRStatus) PR_Notify(PRMonitor *mon);
  61. /*
  62. ** Notify all of the threads waiting on the monitor's condition variable.
  63. ** All of threads waiting on the condition are scheduled to reenter the
  64. ** monitor.
  65. */
  66. NSPR_API(PRStatus) PR_NotifyAll(PRMonitor *mon);
  67. /*
  68. ** PR_ASSERT_CURRENT_THREAD_IN_MONITOR
  69. ** If the current thread is in |mon|, this assertion is guaranteed to
  70. ** succeed. Otherwise, the behavior of this function is undefined.
  71. */
  72. #if defined(DEBUG) || defined(FORCE_PR_ASSERT)
  73. #define PR_ASSERT_CURRENT_THREAD_IN_MONITOR(/* PRMonitor* */ mon) \
  74. PR_AssertCurrentThreadInMonitor(mon)
  75. #else
  76. #define PR_ASSERT_CURRENT_THREAD_IN_MONITOR(/* PRMonitor* */ mon)
  77. #endif
  78. /* Don't call this function directly. */
  79. NSPR_API(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon);
  80. PR_END_EXTERN_C
  81. #endif /* prmon_h___ */