apr_proc_mutex.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_PROC_MUTEX_H
  17. #define APR_PROC_MUTEX_H
  18. /**
  19. * @file apr_proc_mutex.h
  20. * @brief APR Process Locking Routines
  21. */
  22. #include "apr.h"
  23. #include "apr_pools.h"
  24. #include "apr_errno.h"
  25. #include "apr_perms_set.h"
  26. #include "apr_time.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif /* __cplusplus */
  30. /**
  31. * @defgroup apr_proc_mutex Process Locking Routines
  32. * @ingroup APR
  33. * @{
  34. */
  35. /**
  36. * Enumerated potential types for APR process locking methods
  37. * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
  38. * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable.
  39. */
  40. typedef enum {
  41. APR_LOCK_FCNTL, /**< fcntl() */
  42. APR_LOCK_FLOCK, /**< flock() */
  43. APR_LOCK_SYSVSEM, /**< System V Semaphores */
  44. APR_LOCK_PROC_PTHREAD, /**< POSIX pthread process-based locking */
  45. APR_LOCK_POSIXSEM, /**< POSIX semaphore process-based locking */
  46. APR_LOCK_DEFAULT, /**< Use the default process lock */
  47. APR_LOCK_DEFAULT_TIMED /**< Use the default process timed lock */
  48. } apr_lockmech_e;
  49. /** Opaque structure representing a process mutex. */
  50. typedef struct apr_proc_mutex_t apr_proc_mutex_t;
  51. /* Function definitions */
  52. /**
  53. * Create and initialize a mutex that can be used to synchronize processes.
  54. * @param mutex the memory address where the newly created mutex will be
  55. * stored.
  56. * @param fname A file name to use if the lock mechanism requires one. This
  57. * argument should always be provided. The lock code itself will
  58. * determine if it should be used.
  59. * @param mech The mechanism to use for the interprocess lock, if any; one of
  60. * <PRE>
  61. * APR_LOCK_FCNTL
  62. * APR_LOCK_FLOCK
  63. * APR_LOCK_SYSVSEM
  64. * APR_LOCK_POSIXSEM
  65. * APR_LOCK_PROC_PTHREAD
  66. * APR_LOCK_DEFAULT pick the default mechanism for the platform
  67. * </PRE>
  68. * @param pool the pool from which to allocate the mutex.
  69. * @see apr_lockmech_e
  70. * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
  71. * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable.
  72. */
  73. APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
  74. const char *fname,
  75. apr_lockmech_e mech,
  76. apr_pool_t *pool);
  77. /**
  78. * Re-open a mutex in a child process.
  79. * @param mutex The newly re-opened mutex structure.
  80. * @param fname A file name to use if the mutex mechanism requires one. This
  81. * argument should always be provided. The mutex code itself will
  82. * determine if it should be used. This filename should be the
  83. * same one that was passed to apr_proc_mutex_create().
  84. * @param pool The pool to operate on.
  85. * @remark This function must be called to maintain portability, even
  86. * if the underlying lock mechanism does not require it.
  87. */
  88. APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
  89. const char *fname,
  90. apr_pool_t *pool);
  91. /**
  92. * Acquire the lock for the given mutex. If the mutex is already locked,
  93. * the current thread will be put to sleep until the lock becomes available.
  94. * @param mutex the mutex on which to acquire the lock.
  95. */
  96. APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex);
  97. /**
  98. * Attempt to acquire the lock for the given mutex. If the mutex has already
  99. * been acquired, the call returns immediately with APR_EBUSY. Note: it
  100. * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
  101. * if the return value was APR_EBUSY, for portability reasons.
  102. * @param mutex the mutex on which to attempt the lock acquiring.
  103. */
  104. APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex);
  105. /**
  106. * Attempt to acquire the lock for the given mutex until timeout expires.
  107. * If the acquisition time outs, the call returns with APR_TIMEUP.
  108. * @param mutex the mutex on which to attempt the lock acquiring.
  109. * @param timeout the relative timeout (microseconds).
  110. * @note A negative or nul timeout means immediate attempt, returning
  111. * APR_TIMEUP without blocking if it the lock is already acquired.
  112. */
  113. APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex,
  114. apr_interval_time_t timeout);
  115. /**
  116. * Release the lock for the given mutex.
  117. * @param mutex the mutex from which to release the lock.
  118. */
  119. APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex);
  120. /**
  121. * Destroy the mutex and free the memory associated with the lock.
  122. * @param mutex the mutex to destroy.
  123. */
  124. APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex);
  125. /**
  126. * Destroy the mutex and free the memory associated with the lock.
  127. * @param mutex the mutex to destroy.
  128. * @note This function is generally used to kill a cleanup on an already
  129. * created mutex
  130. */
  131. APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex);
  132. /**
  133. * Return the name of the lockfile for the mutex, or NULL
  134. * if the mutex doesn't use a lock file
  135. */
  136. APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex);
  137. /**
  138. * Get the mechanism of the mutex, as it relates to the actual method
  139. * used for the underlying apr_proc_mutex_t.
  140. * @param mutex the mutex to get the mechanism from.
  141. */
  142. APR_DECLARE(apr_lockmech_e) apr_proc_mutex_mech(apr_proc_mutex_t *mutex);
  143. /**
  144. * Get the mechanism's name of the mutex, as it relates to the actual method
  145. * used for the underlying apr_proc_mutex_t.
  146. * @param mutex the mutex to get the mechanism's name from.
  147. */
  148. APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex);
  149. /**
  150. * Display the name of the default mutex: APR_LOCK_DEFAULT
  151. */
  152. APR_DECLARE(const char *) apr_proc_mutex_defname(void);
  153. /**
  154. * Set mutex permissions.
  155. */
  156. APR_PERMS_SET_IMPLEMENT(proc_mutex);
  157. /**
  158. * Get the pool used by this proc_mutex.
  159. * @return apr_pool_t the pool
  160. */
  161. APR_POOL_DECLARE_ACCESSOR(proc_mutex);
  162. /** @} */
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166. #endif /* ! APR_PROC_MUTEX_H */