crit.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #ifndef BASE_CRIT_H
  13. #define BASE_CRIT_H
  14. #ifndef NOINTNSAPI
  15. #define INTNSAPI
  16. #endif /* !NOINTNSAPI */
  17. /*
  18. * crit.h: Critical section abstraction. Used in threaded servers to protect
  19. * areas where two threads can interfere with each other.
  20. *
  21. * Condvars are condition variables that are used for thread-thread
  22. * synchronization.
  23. *
  24. * Rob McCool
  25. */
  26. #ifndef NETSITE_H
  27. #include "netsite.h"
  28. #endif /* !NETSITE_H */
  29. /* Define C++ interface */
  30. #ifdef __cplusplus
  31. #include "prlog.h" /* NSPR */
  32. #ifndef prmon_h___
  33. #include "prmon.h"
  34. #endif /* !prmon_h___ */
  35. class NSAPI_PUBLIC CriticalSection
  36. {
  37. public:
  38. CriticalSection();
  39. ~CriticalSection();
  40. void Acquire() { PR_EnterMonitor(_crtsec); }
  41. void Release() { PR_ExitMonitor(_crtsec); }
  42. private:
  43. PRMonitor *_crtsec;
  44. };
  45. inline CriticalSection::CriticalSection() : _crtsec(0)
  46. {
  47. _crtsec = PR_NewMonitor();
  48. PR_ASSERT(_crtsec);
  49. }
  50. inline CriticalSection::~CriticalSection()
  51. {
  52. if (_crtsec)
  53. PR_DestroyMonitor(_crtsec);
  54. }
  55. class SafeLock
  56. {
  57. public:
  58. SafeLock(CriticalSection &); // acquire lock
  59. ~SafeLock(); // release lock
  60. private:
  61. CriticalSection &lock;
  62. };
  63. inline SafeLock::SafeLock(CriticalSection &_lock) : lock(_lock)
  64. {
  65. lock.Acquire();
  66. }
  67. inline SafeLock::~SafeLock()
  68. {
  69. lock.Release();
  70. }
  71. #endif /* __cplusplus */
  72. /* --- Begin function prototypes --- */
  73. #ifdef INTNSAPI
  74. NSPR_BEGIN_EXTERN_C
  75. /* ASSERT function only */
  76. NSAPI_PUBLIC int crit_owner_is_me(CRITICAL id);
  77. /*
  78. * INTcrit_init creates and returns a new critical section variable. At the
  79. * time of creation no one has entered it.
  80. */
  81. NSAPI_PUBLIC CRITICAL INTcrit_init(void);
  82. /*
  83. * INTcrit_enter enters a critical section. If someone is already in the
  84. * section, the calling thread is blocked until that thread exits.
  85. */
  86. NSAPI_PUBLIC void INTcrit_enter(CRITICAL id);
  87. /*
  88. * INTcrit_exit exits a critical section. If another thread is blocked waiting
  89. * to enter, it will be unblocked and given ownership of the section.
  90. */
  91. NSAPI_PUBLIC void INTcrit_exit(CRITICAL id);
  92. /*
  93. * INTcrit_terminate removes a previously allocated critical section variable.
  94. */
  95. NSAPI_PUBLIC void INTcrit_terminate(CRITICAL id);
  96. /*
  97. * INTcondvar_init initializes and returns a new condition variable. You
  98. * must provide a critical section to be associated with this condition
  99. * variable.
  100. */
  101. NSAPI_PUBLIC CONDVAR INTcondvar_init(CRITICAL id);
  102. /*
  103. * INTcondvar_wait blocks on the given condition variable. The calling thread
  104. * will be blocked until another thread calls INTcondvar_notify on this variable.
  105. * The caller must have entered the critical section associated with this
  106. * condition variable prior to waiting for it.
  107. */
  108. NSAPI_PUBLIC void INTcondvar_wait(CONDVAR cv);
  109. NSAPI_PUBLIC void condvar_timed_wait(CONDVAR _cv, long secs);
  110. /*
  111. * INTcondvar_notify awakens any threads blocked on the given condition
  112. * variable. The caller must have entered the critical section associated
  113. * with this variable first.
  114. */
  115. NSAPI_PUBLIC void INTcondvar_notify(CONDVAR cv);
  116. /*
  117. * INTcondvar_notifyAll awakens all threads blocked on the given condition
  118. * variable. The caller must have entered the critical section associated
  119. * with this variable first.
  120. */
  121. NSAPI_PUBLIC void INTcondvar_notifyAll(CONDVAR cv);
  122. /*
  123. * INTcondvar_terminate frees the given previously allocated condition variable
  124. */
  125. NSAPI_PUBLIC void INTcondvar_terminate(CONDVAR cv);
  126. /*
  127. * Create a counting semaphore.
  128. * Return non-zero on success, 0 on failure.
  129. */
  130. NSAPI_PUBLIC COUNTING_SEMAPHORE INTcs_init(int initial_count);
  131. /*
  132. * Destroy a counting semaphore
  133. */
  134. NSAPI_PUBLIC void INTcs_terminate(COUNTING_SEMAPHORE csp);
  135. /*
  136. * Wait to "enter" the semaphore.
  137. * Return 0 on success, -1 on failure.
  138. */
  139. NSAPI_PUBLIC int INTcs_wait(COUNTING_SEMAPHORE csp);
  140. /*
  141. * Enter the semaphore if the count is > 0. Otherwise return -1.
  142. *
  143. */
  144. NSAPI_PUBLIC int INTcs_trywait(COUNTING_SEMAPHORE csp);
  145. /*
  146. * Release the semaphore- allowing a thread to enter.
  147. * Return 0 on success, -1 on failure.
  148. */
  149. NSAPI_PUBLIC int INTcs_release(COUNTING_SEMAPHORE csp);
  150. NSPR_END_EXTERN_C
  151. /* --- End function prototypes --- */
  152. #define crit_init INTcrit_init
  153. #define crit_enter INTcrit_enter
  154. #define crit_exit INTcrit_exit
  155. #define crit_terminate INTcrit_terminate
  156. #define condvar_init INTcondvar_init
  157. #define condvar_wait INTcondvar_wait
  158. #define condvar_notify INTcondvar_notify
  159. #define condvar_notifyAll INTcondvar_notifyAll
  160. #define condvar_terminate INTcondvar_terminate
  161. #define cs_init INTcs_init
  162. #define cs_terminate INTcs_terminate
  163. #define cs_wait INTcs_wait
  164. #define cs_trywait INTcs_trywait
  165. #define cs_release INTcs_release
  166. #endif /* INTNSAPI */
  167. #endif /* !BASE_CRIT_H */