2
0

fspr_proc_mutex.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 fspr_proc_mutex.h
  20. * @brief APR Process Locking Routines
  21. */
  22. #include "fspr.h"
  23. #include "fspr_pools.h"
  24. #include "fspr_errno.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif /* __cplusplus */
  28. /**
  29. * @defgroup fspr_proc_mutex Process Locking Routines
  30. * @ingroup APR
  31. * @{
  32. */
  33. /**
  34. * Enumerated potential types for APR process locking methods
  35. * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
  36. * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable.
  37. */
  38. typedef enum {
  39. APR_LOCK_FCNTL, /**< fcntl() */
  40. APR_LOCK_FLOCK, /**< flock() */
  41. APR_LOCK_SYSVSEM, /**< System V Semaphores */
  42. APR_LOCK_PROC_PTHREAD, /**< POSIX pthread process-based locking */
  43. APR_LOCK_POSIXSEM, /**< POSIX semaphore process-based locking */
  44. APR_LOCK_DEFAULT /**< Use the default process lock */
  45. } fspr_lockmech_e;
  46. /** Opaque structure representing a process mutex. */
  47. typedef struct fspr_proc_mutex_t fspr_proc_mutex_t;
  48. /* Function definitions */
  49. /**
  50. * Create and initialize a mutex that can be used to synchronize processes.
  51. * @param mutex the memory address where the newly created mutex will be
  52. * stored.
  53. * @param fname A file name to use if the lock mechanism requires one. This
  54. * argument should always be provided. The lock code itself will
  55. * determine if it should be used.
  56. * @param mech The mechanism to use for the interprocess lock, if any; one of
  57. * <PRE>
  58. * APR_LOCK_FCNTL
  59. * APR_LOCK_FLOCK
  60. * APR_LOCK_SYSVSEM
  61. * APR_LOCK_POSIXSEM
  62. * APR_LOCK_PROC_PTHREAD
  63. * APR_LOCK_DEFAULT pick the default mechanism for the platform
  64. * </PRE>
  65. * @param pool the pool from which to allocate the mutex.
  66. * @see fspr_lockmech_e
  67. * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
  68. * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable.
  69. */
  70. APR_DECLARE(fspr_status_t) fspr_proc_mutex_create(fspr_proc_mutex_t **mutex,
  71. const char *fname,
  72. fspr_lockmech_e mech,
  73. fspr_pool_t *pool);
  74. /**
  75. * Re-open a mutex in a child process.
  76. * @param mutex The newly re-opened mutex structure.
  77. * @param fname A file name to use if the mutex mechanism requires one. This
  78. * argument should always be provided. The mutex code itself will
  79. * determine if it should be used. This filename should be the
  80. * same one that was passed to fspr_proc_mutex_create().
  81. * @param pool The pool to operate on.
  82. * @remark This function must be called to maintain portability, even
  83. * if the underlying lock mechanism does not require it.
  84. */
  85. APR_DECLARE(fspr_status_t) fspr_proc_mutex_child_init(fspr_proc_mutex_t **mutex,
  86. const char *fname,
  87. fspr_pool_t *pool);
  88. /**
  89. * Acquire the lock for the given mutex. If the mutex is already locked,
  90. * the current thread will be put to sleep until the lock becomes available.
  91. * @param mutex the mutex on which to acquire the lock.
  92. */
  93. APR_DECLARE(fspr_status_t) fspr_proc_mutex_lock(fspr_proc_mutex_t *mutex);
  94. /**
  95. * Attempt to acquire the lock for the given mutex. If the mutex has already
  96. * been acquired, the call returns immediately with APR_EBUSY. Note: it
  97. * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
  98. * if the return value was APR_EBUSY, for portability reasons.
  99. * @param mutex the mutex on which to attempt the lock acquiring.
  100. */
  101. APR_DECLARE(fspr_status_t) fspr_proc_mutex_trylock(fspr_proc_mutex_t *mutex);
  102. /**
  103. * Release the lock for the given mutex.
  104. * @param mutex the mutex from which to release the lock.
  105. */
  106. APR_DECLARE(fspr_status_t) fspr_proc_mutex_unlock(fspr_proc_mutex_t *mutex);
  107. /**
  108. * Destroy the mutex and free the memory associated with the lock.
  109. * @param mutex the mutex to destroy.
  110. */
  111. APR_DECLARE(fspr_status_t) fspr_proc_mutex_destroy(fspr_proc_mutex_t *mutex);
  112. /**
  113. * Destroy the mutex and free the memory associated with the lock.
  114. * @param mutex the mutex to destroy.
  115. * @note This function is generally used to kill a cleanup on an already
  116. * created mutex
  117. */
  118. APR_DECLARE(fspr_status_t) fspr_proc_mutex_cleanup(void *mutex);
  119. /**
  120. * Return the name of the lockfile for the mutex, or NULL
  121. * if the mutex doesn't use a lock file
  122. */
  123. APR_DECLARE(const char *) fspr_proc_mutex_lockfile(fspr_proc_mutex_t *mutex);
  124. /**
  125. * Display the name of the mutex, as it relates to the actual method used.
  126. * This matches the valid options for Apache's AcceptMutex directive
  127. * @param mutex the name of the mutex
  128. */
  129. APR_DECLARE(const char *) fspr_proc_mutex_name(fspr_proc_mutex_t *mutex);
  130. /**
  131. * Display the name of the default mutex: APR_LOCK_DEFAULT
  132. */
  133. APR_DECLARE(const char *) fspr_proc_mutex_defname(void);
  134. /**
  135. * Get the pool used by this proc_mutex.
  136. * @return fspr_pool_t the pool
  137. */
  138. APR_POOL_DECLARE_ACCESSOR(proc_mutex);
  139. /** @} */
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. #endif /* ! APR_PROC_MUTEX_H */