crit.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #ifndef BASE_CRIT_H
  42. #define BASE_CRIT_H
  43. #ifndef NOINTNSAPI
  44. #define INTNSAPI
  45. #endif /* !NOINTNSAPI */
  46. /*
  47. * crit.h: Critical section abstraction. Used in threaded servers to protect
  48. * areas where two threads can interfere with each other.
  49. *
  50. * Condvars are condition variables that are used for thread-thread
  51. * synchronization.
  52. *
  53. * Rob McCool
  54. */
  55. #ifndef NETSITE_H
  56. #include "netsite.h"
  57. #endif /* !NETSITE_H */
  58. /* Define C++ interface */
  59. #ifdef __cplusplus
  60. #include "prlog.h" /* NSPR */
  61. #ifndef prmon_h___
  62. #include "prmon.h"
  63. #endif /* !prmon_h___ */
  64. class NSAPI_PUBLIC CriticalSection
  65. {
  66. public:
  67. CriticalSection();
  68. ~CriticalSection();
  69. void Acquire(){PR_EnterMonitor(_crtsec);}
  70. void Release(){PR_ExitMonitor(_crtsec);}
  71. private:
  72. PRMonitor *_crtsec;
  73. };
  74. inline CriticalSection::CriticalSection():_crtsec(0)
  75. {
  76. _crtsec = PR_NewMonitor();
  77. PR_ASSERT(_crtsec);
  78. }
  79. inline CriticalSection::~CriticalSection()
  80. {
  81. if (_crtsec)
  82. PR_DestroyMonitor(_crtsec);
  83. }
  84. class SafeLock {
  85. public:
  86. SafeLock (CriticalSection&); // acquire lock
  87. ~SafeLock (); // release lock
  88. private:
  89. CriticalSection& lock;
  90. };
  91. inline SafeLock::SafeLock (CriticalSection& _lock) : lock(_lock)
  92. {
  93. lock.Acquire();
  94. }
  95. inline SafeLock::~SafeLock ()
  96. {
  97. lock.Release();
  98. }
  99. #endif /* __cplusplus */
  100. /* --- Begin function prototypes --- */
  101. #ifdef INTNSAPI
  102. NSPR_BEGIN_EXTERN_C
  103. /* ASSERT function only */
  104. NSAPI_PUBLIC int crit_owner_is_me(CRITICAL id);
  105. /*
  106. * INTcrit_init creates and returns a new critical section variable. At the
  107. * time of creation no one has entered it.
  108. */
  109. NSAPI_PUBLIC CRITICAL INTcrit_init(void);
  110. /*
  111. * INTcrit_enter enters a critical section. If someone is already in the
  112. * section, the calling thread is blocked until that thread exits.
  113. */
  114. NSAPI_PUBLIC void INTcrit_enter(CRITICAL id);
  115. /*
  116. * INTcrit_exit exits a critical section. If another thread is blocked waiting
  117. * to enter, it will be unblocked and given ownership of the section.
  118. */
  119. NSAPI_PUBLIC void INTcrit_exit(CRITICAL id);
  120. /*
  121. * INTcrit_terminate removes a previously allocated critical section variable.
  122. */
  123. NSAPI_PUBLIC void INTcrit_terminate(CRITICAL id);
  124. /*
  125. * INTcondvar_init initializes and returns a new condition variable. You
  126. * must provide a critical section to be associated with this condition
  127. * variable.
  128. */
  129. NSAPI_PUBLIC CONDVAR INTcondvar_init(CRITICAL id);
  130. /*
  131. * INTcondvar_wait blocks on the given condition variable. The calling thread
  132. * will be blocked until another thread calls INTcondvar_notify on this variable.
  133. * The caller must have entered the critical section associated with this
  134. * condition variable prior to waiting for it.
  135. */
  136. NSAPI_PUBLIC void INTcondvar_wait(CONDVAR cv);
  137. NSAPI_PUBLIC void condvar_timed_wait(CONDVAR _cv, long secs);
  138. /*
  139. * INTcondvar_notify awakens any threads blocked on the given condition
  140. * variable. The caller must have entered the critical section associated
  141. * with this variable first.
  142. */
  143. NSAPI_PUBLIC void INTcondvar_notify(CONDVAR cv);
  144. /*
  145. * INTcondvar_notifyAll awakens all threads blocked on the given condition
  146. * variable. The caller must have entered the critical section associated
  147. * with this variable first.
  148. */
  149. NSAPI_PUBLIC void INTcondvar_notifyAll(CONDVAR cv);
  150. /*
  151. * INTcondvar_terminate frees the given previously allocated condition variable
  152. */
  153. NSAPI_PUBLIC void INTcondvar_terminate(CONDVAR cv);
  154. /*
  155. * Create a counting semaphore.
  156. * Return non-zero on success, 0 on failure.
  157. */
  158. NSAPI_PUBLIC COUNTING_SEMAPHORE INTcs_init(int initial_count);
  159. /*
  160. * Destroy a counting semaphore
  161. */
  162. NSAPI_PUBLIC void INTcs_terminate(COUNTING_SEMAPHORE csp);
  163. /*
  164. * Wait to "enter" the semaphore.
  165. * Return 0 on success, -1 on failure.
  166. */
  167. NSAPI_PUBLIC int INTcs_wait(COUNTING_SEMAPHORE csp);
  168. /*
  169. * Enter the semaphore if the count is > 0. Otherwise return -1.
  170. *
  171. */
  172. NSAPI_PUBLIC int INTcs_trywait(COUNTING_SEMAPHORE csp);
  173. /*
  174. * Release the semaphore- allowing a thread to enter.
  175. * Return 0 on success, -1 on failure.
  176. */
  177. NSAPI_PUBLIC int INTcs_release(COUNTING_SEMAPHORE csp);
  178. NSPR_END_EXTERN_C
  179. /* --- End function prototypes --- */
  180. #define crit_init INTcrit_init
  181. #define crit_enter INTcrit_enter
  182. #define crit_exit INTcrit_exit
  183. #define crit_terminate INTcrit_terminate
  184. #define condvar_init INTcondvar_init
  185. #define condvar_wait INTcondvar_wait
  186. #define condvar_notify INTcondvar_notify
  187. #define condvar_notifyAll INTcondvar_notifyAll
  188. #define condvar_terminate INTcondvar_terminate
  189. #define cs_init INTcs_init
  190. #define cs_terminate INTcs_terminate
  191. #define cs_wait INTcs_wait
  192. #define cs_trywait INTcs_trywait
  193. #define cs_release INTcs_release
  194. #endif /* INTNSAPI */
  195. #endif /* !BASE_CRIT_H */