fspr_global_mutex.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 fspr_global_mutex.h
  20. * @brief APR Global Locking Routines
  21. */
  22. #include "fspr.h"
  23. #include "fspr_proc_mutex.h" /* only for fspr_lockmech_e */
  24. #include "fspr_pools.h"
  25. #include "fspr_errno.h"
  26. #if APR_PROC_MUTEX_IS_GLOBAL
  27. #include "fspr_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 fspr_global_mutex_t fspr_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 fspr_proc_mutex.h and fspr_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(fspr_status_t) fspr_global_mutex_create(fspr_global_mutex_t **mutex,
  66. const char *fname,
  67. fspr_lockmech_e mech,
  68. fspr_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 fspr_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(fspr_status_t) fspr_global_mutex_child_init(
  81. fspr_global_mutex_t **mutex,
  82. const char *fname,
  83. fspr_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(fspr_status_t) fspr_global_mutex_lock(fspr_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(fspr_status_t) fspr_global_mutex_trylock(fspr_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(fspr_status_t) fspr_global_mutex_unlock(fspr_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(fspr_status_t) fspr_global_mutex_destroy(fspr_global_mutex_t *mutex);
  108. /**
  109. * Get the pool used by this global_mutex.
  110. * @return fspr_pool_t the pool
  111. */
  112. APR_POOL_DECLARE_ACCESSOR(global_mutex);
  113. #else /* APR_PROC_MUTEX_IS_GLOBAL */
  114. /* Some platforms [e.g. Win32] have cross process locks that are truly
  115. * global locks, since there isn't the concept of cross-process locks.
  116. * Define these platforms in terms of an fspr_proc_mutex_t.
  117. */
  118. #define fspr_global_mutex_t fspr_proc_mutex_t
  119. #define fspr_global_mutex_create fspr_proc_mutex_create
  120. #define fspr_global_mutex_child_init fspr_proc_mutex_child_init
  121. #define fspr_global_mutex_lock fspr_proc_mutex_lock
  122. #define fspr_global_mutex_trylock fspr_proc_mutex_trylock
  123. #define fspr_global_mutex_unlock fspr_proc_mutex_unlock
  124. #define fspr_global_mutex_destroy fspr_proc_mutex_destroy
  125. #define fspr_global_mutex_pool_get fspr_proc_mutex_pool_get
  126. #endif
  127. /** @} */
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif /* ndef APR_GLOBAL_MUTEX_H */