slapi2nspr.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /*
  42. * slapi2nspr.c - expose a subset of the NSPR20/21 API to SLAPI plugin writers
  43. *
  44. */
  45. #include "slap.h"
  46. #include <nspr.h>
  47. /*
  48. * Note that Slapi_Mutex and Slapi_CondVar are defined like this in
  49. * slapi-plugin.h:
  50. *
  51. * typedef struct slapi_mutex Slapi_Mutex;
  52. * typedef struct slapi_condvar Slapi_CondVar;
  53. *
  54. * but there is no definition for struct slapi_mutex or struct slapi_condvar.
  55. * This seems to work okay since we always use them in pointer form and cast
  56. * directly to their NSPR equivalents. Clever, huh?
  57. */
  58. /*
  59. * ---------------- SLAPI API Functions --------------------------------------
  60. */
  61. /*
  62. * Function: slapi_new_mutex
  63. * Description: behaves just like PR_NewLock().
  64. * Returns: a pointer to the new mutex (NULL if a mutex can't be created).
  65. */
  66. Slapi_Mutex *
  67. slapi_new_mutex( void )
  68. {
  69. return( (Slapi_Mutex *)PR_NewLock());
  70. }
  71. /*
  72. * Function: slapi_destroy_mutex
  73. * Description: behaves just like PR_DestroyLock().
  74. */
  75. void
  76. slapi_destroy_mutex( Slapi_Mutex *mutex )
  77. {
  78. if ( mutex != NULL ) {
  79. PR_DestroyLock( (PRLock *)mutex );
  80. }
  81. }
  82. /*
  83. * Function: slapi_lock_mutex
  84. * Description: behaves just like PR_Lock().
  85. */
  86. void
  87. slapi_lock_mutex( Slapi_Mutex *mutex )
  88. {
  89. if ( mutex != NULL ) {
  90. PR_Lock( (PRLock *)mutex );
  91. }
  92. }
  93. /*
  94. * Function: slapi_unlock_mutex
  95. * Description: behaves just like PR_Unlock().
  96. * Returns:
  97. * non-zero if mutex was successfully unlocked.
  98. * 0 if mutex is NULL or is not locked by the calling thread.
  99. */
  100. int
  101. slapi_unlock_mutex( Slapi_Mutex *mutex )
  102. {
  103. if ( mutex == NULL || PR_Unlock( (PRLock *)mutex ) == PR_FAILURE ) {
  104. return( 0 );
  105. } else {
  106. return( 1 );
  107. }
  108. }
  109. /*
  110. * Function: slapi_new_condvar
  111. * Description: behaves just like PR_NewCondVar().
  112. * Returns: pointer to a new condition variable (NULL if one can't be created).
  113. */
  114. Slapi_CondVar *
  115. slapi_new_condvar( Slapi_Mutex *mutex )
  116. {
  117. if ( mutex == NULL ) {
  118. return( NULL );
  119. }
  120. return( (Slapi_CondVar *)PR_NewCondVar( (PRLock *)mutex ));
  121. }
  122. /*
  123. * Function: slapi_destroy_condvar
  124. * Description: behaves just like PR_DestroyCondVar().
  125. */
  126. void
  127. slapi_destroy_condvar( Slapi_CondVar *cvar )
  128. {
  129. if ( cvar != NULL ) {
  130. PR_DestroyCondVar( (PRCondVar *)cvar );
  131. }
  132. }
  133. /*
  134. * Function: slapi_wait_condvar
  135. * Description: behaves just like PR_WaitCondVar() except timeout is
  136. * in seconds and microseconds instead of PRIntervalTime units.
  137. * If timeout is NULL, this call blocks indefinitely.
  138. * Returns:
  139. * non-zero is all goes well.
  140. * 0 if cvar is NULL, the caller has not locked the mutex associated
  141. * with cvar, or the waiting thread was interrupted.
  142. */
  143. int
  144. slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
  145. {
  146. PRIntervalTime prit;
  147. if ( cvar == NULL ) {
  148. return( 0 );
  149. }
  150. if ( timeout == NULL ) {
  151. prit = PR_INTERVAL_NO_TIMEOUT;
  152. } else {
  153. prit = PR_SecondsToInterval( timeout->tv_sec )
  154. + PR_MicrosecondsToInterval( timeout->tv_usec );
  155. }
  156. if ( PR_WaitCondVar( (PRCondVar *)cvar, prit ) != PR_SUCCESS ) {
  157. return( 0 );
  158. }
  159. return( 1 );
  160. }
  161. /*
  162. * Function: slapi_notify_condvar
  163. * Description: if notify_all is zero, behaves just like PR_NotifyCondVar().
  164. * if notify_all is non-zero, behaves just like PR_NotifyAllCondVar().
  165. * Returns:
  166. * non-zero if all goes well.
  167. * 0 if cvar is NULL or the caller has not locked the mutex associated
  168. * with cvar.
  169. */
  170. int
  171. slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
  172. {
  173. PRStatus prrc;
  174. if ( cvar == NULL ) {
  175. return( 0 );
  176. }
  177. if ( notify_all ) {
  178. prrc = PR_NotifyAllCondVar( (PRCondVar *)cvar );
  179. } else {
  180. prrc = PR_NotifyCondVar( (PRCondVar *)cvar );
  181. }
  182. return( prrc == PR_SUCCESS ? 1 : 0 );
  183. }