util_mutex.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /**
  17. * @file util_mutex.h
  18. * @brief Apache Mutex support library
  19. *
  20. * @defgroup APACHE_CORE_MUTEX Mutex Library
  21. * @ingroup APACHE_CORE
  22. * @{
  23. */
  24. #ifndef UTIL_MUTEX_H
  25. #define UTIL_MUTEX_H
  26. #include "httpd.h"
  27. #include "http_config.h"
  28. #include "apr_global_mutex.h"
  29. #if APR_HAS_FLOCK_SERIALIZE
  30. # define AP_LIST_FLOCK_SERIALIZE ", 'flock:/path/to/file'"
  31. #else
  32. # define AP_LIST_FLOCK_SERIALIZE
  33. #endif
  34. #if APR_HAS_FCNTL_SERIALIZE
  35. # define AP_LIST_FCNTL_SERIALIZE ", 'fcntl:/path/to/file'"
  36. #else
  37. # define AP_LIST_FCNTL_SERIALIZE
  38. #endif
  39. #if APR_HAS_SYSVSEM_SERIALIZE
  40. # define AP_LIST_SYSVSEM_SERIALIZE ", 'sysvsem'"
  41. #else
  42. # define AP_LIST_SYSVSEM_SERIALIZE
  43. #endif
  44. #if APR_HAS_POSIXSEM_SERIALIZE
  45. # define AP_LIST_POSIXSEM_SERIALIZE ", 'posixsem'"
  46. #else
  47. # define AP_LIST_POSIXSEM_SERIALIZE
  48. #endif
  49. #if APR_HAS_PROC_PTHREAD_SERIALIZE
  50. # define AP_LIST_PTHREAD_SERIALIZE ", 'pthread'"
  51. #else
  52. # define AP_LIST_PTHREAD_SERIALIZE
  53. #endif
  54. #if APR_HAS_FLOCK_SERIALIZE || APR_HAS_FCNTL_SERIALIZE
  55. # define AP_LIST_FILE_SERIALIZE ", 'file:/path/to/file'"
  56. #else
  57. # define AP_LIST_FILE_SERIALIZE
  58. #endif
  59. #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_POSIXSEM_SERIALIZE
  60. # define AP_LIST_SEM_SERIALIZE ", 'sem'"
  61. #else
  62. # define AP_LIST_SEM_SERIALIZE
  63. #endif
  64. #define AP_ALL_AVAILABLE_MUTEXES_STRING \
  65. "Mutex mechanisms are: 'none', 'default'" \
  66. AP_LIST_FLOCK_SERIALIZE AP_LIST_FCNTL_SERIALIZE \
  67. AP_LIST_FILE_SERIALIZE AP_LIST_PTHREAD_SERIALIZE \
  68. AP_LIST_SYSVSEM_SERIALIZE AP_LIST_POSIXSEM_SERIALIZE \
  69. AP_LIST_SEM_SERIALIZE
  70. #define AP_AVAILABLE_MUTEXES_STRING \
  71. "Mutex mechanisms are: 'default'" \
  72. AP_LIST_FLOCK_SERIALIZE AP_LIST_FCNTL_SERIALIZE \
  73. AP_LIST_FILE_SERIALIZE AP_LIST_PTHREAD_SERIALIZE \
  74. AP_LIST_SYSVSEM_SERIALIZE AP_LIST_POSIXSEM_SERIALIZE \
  75. AP_LIST_SEM_SERIALIZE
  76. #ifdef __cplusplus
  77. extern "C" {
  78. #endif
  79. /**
  80. * Get Mutex config data and parse it
  81. * @param arg The mutex config string
  82. * @param pool The allocation pool
  83. * @param mutexmech The APR mutex locking mechanism
  84. * @param mutexfile The lockfile to use as required
  85. * @return APR status code
  86. * @fn apr_status_t ap_parse_mutex(const char *arg, apr_pool_t *pool,
  87. apr_lockmech_e *mutexmech,
  88. const char **mutexfile)
  89. */
  90. AP_DECLARE(apr_status_t) ap_parse_mutex(const char *arg, apr_pool_t *pool,
  91. apr_lockmech_e *mutexmech,
  92. const char **mutexfile);
  93. /* private function to process the Mutex directive */
  94. AP_DECLARE_NONSTD(const char *) ap_set_mutex(cmd_parms *cmd, void *dummy,
  95. const char *arg);
  96. /* private function to initialize Mutex infrastructure */
  97. AP_DECLARE_NONSTD(void) ap_mutex_init(apr_pool_t *p);
  98. /**
  99. * option flags for ap_mutex_register(), ap_global_mutex_create(), and
  100. * ap_proc_mutex_create()
  101. */
  102. #define AP_MUTEX_ALLOW_NONE 1 /* allow "none" as mutex implementation;
  103. * respected only on ap_mutex_register()
  104. */
  105. #define AP_MUTEX_DEFAULT_NONE 2 /* default to "none" for this mutex;
  106. * respected only on ap_mutex_register()
  107. */
  108. /**
  109. * Register a module's mutex type with core to allow configuration
  110. * with the Mutex directive. This must be called in the pre_config
  111. * hook; otherwise, configuration directives referencing this mutex
  112. * type will be rejected.
  113. *
  114. * The default_dir and default_mech parameters allow a module to set
  115. * defaults for the lock file directory and mechanism. These could
  116. * be based on compile-time settings. These aren't required except
  117. * in special circumstances.
  118. *
  119. * The order of precedence for the choice of mechanism and lock file
  120. * directory is:
  121. *
  122. * 1. Mutex directive specifically for this mutex
  123. * e.g., Mutex mpm-default flock:/tmp/mpmlocks
  124. * 2. Mutex directive for global default
  125. * e.g., Mutex default flock:/tmp/httpdlocks
  126. * 3. Defaults for this mutex provided on the ap_mutex_register()
  127. * 4. Built-in defaults for all mutexes, which are
  128. * APR_LOCK_DEFAULT and DEFAULT_REL_RUNTIMEDIR.
  129. *
  130. * @param pconf The pconf pool
  131. * @param type The type name of the mutex, used as the basename of the
  132. * file associated with the mutex, if any. This must be unique among
  133. * all mutex types (mutex creation accommodates multi-instance mutex
  134. * types); mod_foo might have mutex types "foo-pipe" and "foo-shm"
  135. * @param default_dir Default dir for any lock file required for this
  136. * lock, to override built-in defaults; should be NULL for most
  137. * modules, to respect built-in defaults
  138. * @param default_mech Default mechanism for this lock, to override
  139. * built-in defaults; should be APR_LOCK_DEFAULT for most modules, to
  140. * respect built-in defaults
  141. * or NULL if there are no defaults for this mutex.
  142. * @param options combination of AP_MUTEX_* constants, or 0 for defaults
  143. */
  144. AP_DECLARE(apr_status_t) ap_mutex_register(apr_pool_t *pconf,
  145. const char *type,
  146. const char *default_dir,
  147. apr_lockmech_e default_mech,
  148. apr_int32_t options);
  149. /**
  150. * Create an APR global mutex that has been registered previously with
  151. * ap_mutex_register(). Mutex files, permissions, and error logging will
  152. * be handled internally.
  153. * @param mutex The memory address where the newly created mutex will be
  154. * stored. If this mutex is disabled, mutex will be set to NULL on
  155. * output. (That is allowed only if the AP_MUTEX_ALLOW_NONE flag is
  156. * passed to ap_mutex_register().)
  157. * @param name The generated filename of the created mutex, or NULL if
  158. * no file was created. Pass NULL if this result is not needed.
  159. * @param type The type name of the mutex, matching the type name passed
  160. * to ap_mutex_register().
  161. * @param instance_id A unique string to be used in the lock filename IFF
  162. * this mutex type is multi-instance, NULL otherwise.
  163. * @param server server_rec of main server
  164. * @param pool pool lifetime of the mutex
  165. * @param options combination of AP_MUTEX_* constants, or 0 for defaults
  166. * (currently none are defined for this function)
  167. */
  168. AP_DECLARE(apr_status_t) ap_global_mutex_create(apr_global_mutex_t **mutex,
  169. const char **name,
  170. const char *type,
  171. const char *instance_id,
  172. server_rec *server,
  173. apr_pool_t *pool,
  174. apr_int32_t options);
  175. /**
  176. * Create an APR proc mutex that has been registered previously with
  177. * ap_mutex_register(). Mutex files, permissions, and error logging will
  178. * be handled internally.
  179. * @param mutex The memory address where the newly created mutex will be
  180. * stored. If this mutex is disabled, mutex will be set to NULL on
  181. * output. (That is allowed only if the AP_MUTEX_ALLOW_NONE flag is
  182. * passed to ap_mutex_register().)
  183. * @param name The generated filename of the created mutex, or NULL if
  184. * no file was created. Pass NULL if this result is not needed.
  185. * @param type The type name of the mutex, matching the type name passed
  186. * to ap_mutex_register().
  187. * @param instance_id A unique string to be used in the lock filename IFF
  188. * this mutex type is multi-instance, NULL otherwise.
  189. * @param server server_rec of main server
  190. * @param pool pool lifetime of the mutex
  191. * @param options combination of AP_MUTEX_* constants, or 0 for defaults
  192. * (currently none are defined for this function)
  193. */
  194. AP_DECLARE(apr_status_t) ap_proc_mutex_create(apr_proc_mutex_t **mutex,
  195. const char **name,
  196. const char *type,
  197. const char *instance_id,
  198. server_rec *server,
  199. apr_pool_t *pool,
  200. apr_int32_t options);
  201. AP_CORE_DECLARE(void) ap_dump_mutexes(apr_pool_t *p, server_rec *s, apr_file_t *out);
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif /* UTIL_MUTEX_H */
  206. /** @} */