prcvar.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 prcvar_h___
  6. #define prcvar_h___
  7. #include "prlock.h"
  8. #include "prinrval.h"
  9. PR_BEGIN_EXTERN_C
  10. typedef struct PRCondVar PRCondVar;
  11. /*
  12. ** Create a new condition variable.
  13. **
  14. ** "lock" is the lock used to protect the condition variable.
  15. **
  16. ** Condition variables are synchronization objects that threads can use
  17. ** to wait for some condition to occur.
  18. **
  19. ** This may fail if memory is tight or if some operating system resource
  20. ** is low. In such cases, a NULL will be returned.
  21. */
  22. NSPR_API(PRCondVar*) PR_NewCondVar(PRLock *lock);
  23. /*
  24. ** Destroy a condition variable. There must be no thread
  25. ** waiting on the condvar. The caller is responsible for guaranteeing
  26. ** that the condvar is no longer in use.
  27. **
  28. */
  29. NSPR_API(void) PR_DestroyCondVar(PRCondVar *cvar);
  30. /*
  31. ** The thread that waits on a condition is blocked in a "waiting on
  32. ** condition" state until another thread notifies the condition or a
  33. ** caller specified amount of time expires. The lock associated with
  34. ** the condition variable will be released, which must have be held
  35. ** prior to the call to wait.
  36. **
  37. ** Logically a notified thread is moved from the "waiting on condition"
  38. ** state and made "ready." When scheduled, it will attempt to reacquire
  39. ** the lock that it held when wait was called.
  40. **
  41. ** The timeout has two well known values, PR_INTERVAL_NO_TIMEOUT and
  42. ** PR_INTERVAL_NO_WAIT. The former value requires that a condition be
  43. ** notified (or the thread interrupted) before it will resume from the
  44. ** wait. If the timeout has a value of PR_INTERVAL_NO_WAIT, the effect
  45. ** is to release the lock, possibly causing a rescheduling within the
  46. ** runtime, then immediately attempting to reacquire the lock and resume.
  47. **
  48. ** Any other value for timeout will cause the thread to be rescheduled
  49. ** either due to explicit notification or an expired interval. The latter
  50. ** must be determined by treating time as one part of the monitored data
  51. ** being protected by the lock and tested explicitly for an expired
  52. ** interval.
  53. **
  54. ** Returns PR_FAILURE if the caller has not locked the lock associated
  55. ** with the condition variable or the thread was interrupted (PR_Interrupt()).
  56. ** The particular reason can be extracted with PR_GetError().
  57. */
  58. NSPR_API(PRStatus) PR_WaitCondVar(PRCondVar *cvar, PRIntervalTime timeout);
  59. /*
  60. ** Notify ONE thread that is currently waiting on 'cvar'. Which thread is
  61. ** dependent on the implementation of the runtime. Common sense would dictate
  62. ** that all threads waiting on a single condition have identical semantics,
  63. ** therefore which one gets notified is not significant.
  64. **
  65. ** The calling thead must hold the lock that protects the condition, as
  66. ** well as the invariants that are tightly bound to the condition, when
  67. ** notify is called.
  68. **
  69. ** Returns PR_FAILURE if the caller has not locked the lock associated
  70. ** with the condition variable.
  71. */
  72. NSPR_API(PRStatus) PR_NotifyCondVar(PRCondVar *cvar);
  73. /*
  74. ** Notify all of the threads waiting on the condition variable. The order
  75. ** that the threads are notified is indeterminant. The lock that protects
  76. ** the condition must be held.
  77. **
  78. ** Returns PR_FAILURE if the caller has not locked the lock associated
  79. ** with the condition variable.
  80. */
  81. NSPR_API(PRStatus) PR_NotifyAllCondVar(PRCondVar *cvar);
  82. PR_END_EXTERN_C
  83. #endif /* prcvar_h___ */