apr_global_mutex.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifdef __cplusplus
  30. extern "C" {
  31. #endif /* __cplusplus */
  32. /**
  33. * @defgroup APR_GlobalMutex Global Locking Routines
  34. * @ingroup APR
  35. * @{
  36. */
  37. #if !APR_PROC_MUTEX_IS_GLOBAL || defined(DOXYGEN)
  38. /** Opaque global mutex structure. */
  39. typedef struct apr_global_mutex_t apr_global_mutex_t;
  40. /* Function definitions */
  41. /**
  42. * Create and initialize a mutex that can be used to synchronize both
  43. * processes and threads. Note: There is considerable overhead in using
  44. * this API if only cross-process or cross-thread mutual exclusion is
  45. * required. See apr_proc_mutex.h and apr_thread_mutex.h for more
  46. * specialized lock routines.
  47. * @param mutex the memory address where the newly created mutex will be
  48. * stored.
  49. * @param fname A file name to use if the lock mechanism requires one. This
  50. * argument should always be provided. The lock code itself will
  51. * determine if it should be used.
  52. * @param mech The mechanism to use for the interprocess lock, if any; one of
  53. * <PRE>
  54. * APR_LOCK_FCNTL
  55. * APR_LOCK_FLOCK
  56. * APR_LOCK_SYSVSEM
  57. * APR_LOCK_POSIXSEM
  58. * APR_LOCK_PROC_PTHREAD
  59. * APR_LOCK_DEFAULT pick the default mechanism for the platform
  60. * </PRE>
  61. * @param pool the pool from which to allocate the mutex.
  62. * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
  63. * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable.
  64. */
  65. APR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex,
  66. const char *fname,
  67. apr_lockmech_e mech,
  68. apr_pool_t *pool);
  69. /**
  70. * Re-open a mutex in a child process.
  71. * @param mutex The newly re-opened mutex structure.
  72. * @param fname A file name to use if the mutex mechanism requires one. This
  73. * argument should always be provided. The mutex code itself will
  74. * determine if it should be used. This filename should be the
  75. * same one that was passed to apr_global_mutex_create().
  76. * @param pool The pool to operate on.
  77. * @remark This function must be called to maintain portability, even
  78. * if the underlying lock mechanism does not require it.
  79. */
  80. APR_DECLARE(apr_status_t) apr_global_mutex_child_init(
  81. apr_global_mutex_t **mutex,
  82. const char *fname,
  83. apr_pool_t *pool);
  84. /**
  85. * Acquire the lock for the given mutex. If the mutex is already locked,
  86. * the current thread will be put to sleep until the lock becomes available.
  87. * @param mutex the mutex on which to acquire the lock.
  88. */
  89. APR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex);
  90. /**
  91. * Attempt to acquire the lock for the given mutex. If the mutex has already
  92. * been acquired, the call returns immediately with APR_EBUSY. Note: it
  93. * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
  94. * if the return value was APR_EBUSY, for portability reasons.
  95. * @param mutex the mutex on which to attempt the lock acquiring.
  96. */
  97. APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex);
  98. /**
  99. * Release the lock for the given mutex.
  100. * @param mutex the mutex from which to release the lock.
  101. */
  102. APR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex);
  103. /**
  104. * Destroy the mutex and free the memory associated with the lock.
  105. * @param mutex the mutex to destroy.
  106. */
  107. APR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex);
  108. /**
  109. * Return the name of the lockfile for the mutex, or NULL
  110. * if the mutex doesn't use a lock file
  111. */
  112. APR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex);
  113. /**
  114. * Display the name of the mutex, as it relates to the actual method used
  115. * for the underlying apr_proc_mutex_t, if any. NULL is returned if
  116. * there is no underlying apr_proc_mutex_t.
  117. * @param mutex the name of the mutex
  118. */
  119. APR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex);
  120. /**
  121. * Get the pool used by this global_mutex.
  122. * @return apr_pool_t the pool
  123. */
  124. APR_POOL_DECLARE_ACCESSOR(global_mutex);
  125. #else /* APR_PROC_MUTEX_IS_GLOBAL */
  126. /* Some platforms [e.g. Win32] have cross process locks that are truly
  127. * global locks, since there isn't the concept of cross-process locks.
  128. * Define these platforms in terms of an apr_proc_mutex_t.
  129. */
  130. #define apr_global_mutex_t apr_proc_mutex_t
  131. #define apr_global_mutex_create apr_proc_mutex_create
  132. #define apr_global_mutex_child_init apr_proc_mutex_child_init
  133. #define apr_global_mutex_lock apr_proc_mutex_lock
  134. #define apr_global_mutex_trylock apr_proc_mutex_trylock
  135. #define apr_global_mutex_unlock apr_proc_mutex_unlock
  136. #define apr_global_mutex_destroy apr_proc_mutex_destroy
  137. #define apr_global_mutex_lockfile apr_proc_mutex_lockfile
  138. #define apr_global_mutex_name apr_proc_mutex_name
  139. #define apr_global_mutex_pool_get apr_proc_mutex_pool_get
  140. #endif
  141. /** @} */
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif /* ndef APR_GLOBAL_MUTEX_H */