apr_global_mutex.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef APR_GLOBAL_MUTEX_H
  17. #define APR_GLOBAL_MUTEX_H
  18. /**
  19. * @file apr_global_mutex.h
  20. * @brief APR Global Locking Routines
  21. */
  22. #include "apr.h"
  23. #include "apr_proc_mutex.h" /* only for apr_lockmech_e */
  24. #include "apr_pools.h"
  25. #include "apr_errno.h"
  26. #if APR_PROC_MUTEX_IS_GLOBAL
  27. #include "apr_proc_mutex.h"
  28. #endif
  29. #include "apr_time.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif /* __cplusplus */
  33. /**
  34. * @defgroup APR_GlobalMutex Global Locking Routines
  35. * @ingroup APR
  36. * @{
  37. */
  38. #if !APR_PROC_MUTEX_IS_GLOBAL || defined(DOXYGEN)
  39. /** Opaque global mutex structure. */
  40. typedef struct apr_global_mutex_t apr_global_mutex_t;
  41. /* Function definitions */
  42. /**
  43. * Create and initialize a mutex that can be used to synchronize both
  44. * processes and threads. Note: There is considerable overhead in using
  45. * this API if only cross-process or cross-thread mutual exclusion is
  46. * required. See apr_proc_mutex.h and apr_thread_mutex.h for more
  47. * specialized lock routines.
  48. * @param mutex the memory address where the newly created mutex will be
  49. * stored.
  50. * @param fname A file name to use if the lock mechanism requires one. This
  51. * argument should always be provided. The lock code itself will
  52. * determine if it should be used.
  53. * @param mech The mechanism to use for the interprocess lock, if any; one of
  54. * <PRE>
  55. * APR_LOCK_FCNTL
  56. * APR_LOCK_FLOCK
  57. * APR_LOCK_SYSVSEM
  58. * APR_LOCK_POSIXSEM
  59. * APR_LOCK_PROC_PTHREAD
  60. * APR_LOCK_DEFAULT pick the default mechanism for the platform
  61. * APR_LOCK_DEFAULT_TIMED pick the default timed mechanism
  62. * </PRE>
  63. * @param pool the pool from which to allocate the mutex.
  64. * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
  65. * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable.
  66. */
  67. APR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex,
  68. const char *fname,
  69. apr_lockmech_e mech,
  70. apr_pool_t *pool);
  71. /**
  72. * Re-open a mutex in a child process.
  73. * @param mutex The newly re-opened mutex structure.
  74. * @param fname A file name to use if the mutex mechanism requires one. This
  75. * argument should always be provided. The mutex code itself will
  76. * determine if it should be used. This filename should be the
  77. * same one that was passed to apr_global_mutex_create().
  78. * @param pool The pool to operate on.
  79. * @remark This function must be called to maintain portability, even
  80. * if the underlying lock mechanism does not require it.
  81. */
  82. APR_DECLARE(apr_status_t) apr_global_mutex_child_init(
  83. apr_global_mutex_t **mutex,
  84. const char *fname,
  85. apr_pool_t *pool);
  86. /**
  87. * Acquire the lock for the given mutex. If the mutex is already locked,
  88. * the current thread will be put to sleep until the lock becomes available.
  89. * @param mutex the mutex on which to acquire the lock.
  90. */
  91. APR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex);
  92. /**
  93. * Attempt to acquire the lock for the given mutex. If the mutex has already
  94. * been acquired, the call returns immediately with APR_EBUSY. Note: it
  95. * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
  96. * if the return value was APR_EBUSY, for portability reasons.
  97. * @param mutex the mutex on which to attempt the lock acquiring.
  98. */
  99. APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex);
  100. /**
  101. * Attempt to acquire the lock for the given mutex until timeout expires.
  102. * If the acquisition time outs, the call returns with APR_TIMEUP.
  103. * @param mutex the mutex on which to attempt the lock acquiring.
  104. * @param timeout the relative timeout (microseconds).
  105. * @note A negative or nul timeout means immediate attempt, returning
  106. * APR_TIMEUP without blocking if it the lock is already acquired.
  107. */
  108. APR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex,
  109. apr_interval_time_t timeout);
  110. /**
  111. * Release the lock for the given mutex.
  112. * @param mutex the mutex from which to release the lock.
  113. */
  114. APR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex);
  115. /**
  116. * Destroy the mutex and free the memory associated with the lock.
  117. * @param mutex the mutex to destroy.
  118. */
  119. APR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex);
  120. /**
  121. * Return the name of the lockfile for the mutex, or NULL
  122. * if the mutex doesn't use a lock file
  123. */
  124. APR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex);
  125. /**
  126. * Get the mechanism of the mutex, as it relates to the actual method
  127. * used for the underlying apr_proc_mutex_t.
  128. * @param mutex the mutex to get the mechanism from.
  129. */
  130. APR_DECLARE(apr_lockmech_e) apr_global_mutex_mech(apr_global_mutex_t *mutex);
  131. /**
  132. * Get the mechanism's name of the mutex, as it relates to the actual method
  133. * used for the underlying apr_proc_mutex_t.
  134. * @param mutex the mutex to get the mechanism's name from.
  135. */
  136. APR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex);
  137. /**
  138. * Set mutex permissions.
  139. */
  140. APR_PERMS_SET_IMPLEMENT(global_mutex);
  141. /**
  142. * Get the pool used by this global_mutex.
  143. * @return apr_pool_t the pool
  144. */
  145. APR_POOL_DECLARE_ACCESSOR(global_mutex);
  146. #else /* APR_PROC_MUTEX_IS_GLOBAL */
  147. /* Some platforms [e.g. Win32] have cross process locks that are truly
  148. * global locks, since there isn't the concept of cross-process locks.
  149. * Define these platforms in terms of an apr_proc_mutex_t.
  150. */
  151. #define apr_global_mutex_t apr_proc_mutex_t
  152. #define apr_global_mutex_create apr_proc_mutex_create
  153. #define apr_global_mutex_child_init apr_proc_mutex_child_init
  154. #define apr_global_mutex_lock apr_proc_mutex_lock
  155. #define apr_global_mutex_trylock apr_proc_mutex_trylock
  156. #define apr_global_mutex_unlock apr_proc_mutex_unlock
  157. #define apr_global_mutex_destroy apr_proc_mutex_destroy
  158. #define apr_global_mutex_lockfile apr_proc_mutex_lockfile
  159. #define apr_global_mutex_mech apr_proc_mutex_mech
  160. #define apr_global_mutex_name apr_proc_mutex_name
  161. #define apr_global_mutex_perms_set apr_proc_mutex_perms_set
  162. #define apr_global_mutex_pool_get apr_proc_mutex_pool_get
  163. #endif
  164. /** @} */
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif /* ndef APR_GLOBAL_MUTEX_H */