prmon.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 prmon_h___
  38. #define prmon_h___
  39. #include "prtypes.h"
  40. #include "prinrval.h"
  41. PR_BEGIN_EXTERN_C
  42. typedef struct PRMonitor PRMonitor;
  43. /*
  44. ** Create a new monitor. Monitors are re-entrant locks with a single built-in
  45. ** condition variable.
  46. **
  47. ** This may fail if memory is tight or if some operating system resource
  48. ** is low.
  49. */
  50. NSPR_API(PRMonitor*) PR_NewMonitor(void);
  51. /*
  52. ** Destroy a monitor. The caller is responsible for guaranteeing that the
  53. ** monitor is no longer in use. There must be no thread waiting on the monitor's
  54. ** condition variable and that the lock is not held.
  55. **
  56. */
  57. NSPR_API(void) PR_DestroyMonitor(PRMonitor *mon);
  58. /*
  59. ** Enter the lock associated with the monitor. If the calling thread currently
  60. ** is in the monitor, the call to enter will silently succeed. In either case,
  61. ** it will increment the entry count by one.
  62. */
  63. NSPR_API(void) PR_EnterMonitor(PRMonitor *mon);
  64. /*
  65. ** Decrement the entry count associated with the monitor. If the decremented
  66. ** entry count is zero, the monitor is exited. Returns PR_FAILURE if the
  67. ** calling thread has not entered the monitor.
  68. */
  69. NSPR_API(PRStatus) PR_ExitMonitor(PRMonitor *mon);
  70. /*
  71. ** Wait for a notify on the monitor's condition variable. Sleep for "ticks"
  72. ** amount of time (if "ticks" is PR_INTERVAL_NO_TIMEOUT then the sleep is
  73. ** indefinite).
  74. **
  75. ** While the thread is waiting it exits the monitor (as if it called
  76. ** PR_ExitMonitor as many times as it had called PR_EnterMonitor). When
  77. ** the wait has finished the thread regains control of the monitors lock
  78. ** with the same entry count as before the wait began.
  79. **
  80. ** The thread waiting on the monitor will be resumed when the monitor is
  81. ** notified (assuming the thread is the next in line to receive the
  82. ** notify) or when the "ticks" timeout elapses.
  83. **
  84. ** Returns PR_FAILURE if the caller has not entered the monitor.
  85. */
  86. NSPR_API(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks);
  87. /*
  88. ** Notify a thread waiting on the monitor's condition variable. If a thread
  89. ** is waiting on the condition variable (using PR_Wait) then it is awakened
  90. ** and attempts to reenter the monitor.
  91. */
  92. NSPR_API(PRStatus) PR_Notify(PRMonitor *mon);
  93. /*
  94. ** Notify all of the threads waiting on the monitor's condition variable.
  95. ** All of threads waiting on the condition are scheduled to reenter the
  96. ** monitor.
  97. */
  98. NSPR_API(PRStatus) PR_NotifyAll(PRMonitor *mon);
  99. PR_END_EXTERN_C
  100. #endif /* prmon_h___ */