global_mutex.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include "apr.h"
  17. #include "apr_strings.h"
  18. #include "apr_arch_global_mutex.h"
  19. #include "apr_proc_mutex.h"
  20. #include "apr_thread_mutex.h"
  21. #include "apr_portable.h"
  22. static apr_status_t global_mutex_cleanup(void *data)
  23. {
  24. apr_global_mutex_t *m = (apr_global_mutex_t *)data;
  25. apr_status_t rv;
  26. rv = apr_proc_mutex_destroy(m->proc_mutex);
  27. #if APR_HAS_THREADS
  28. if (m->thread_mutex) {
  29. if (rv != APR_SUCCESS) {
  30. (void)apr_thread_mutex_destroy(m->thread_mutex);
  31. }
  32. else {
  33. rv = apr_thread_mutex_destroy(m->thread_mutex);
  34. }
  35. }
  36. #endif /* APR_HAS_THREADS */
  37. return rv;
  38. }
  39. APR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex,
  40. const char *fname,
  41. apr_lockmech_e mech,
  42. apr_pool_t *pool)
  43. {
  44. apr_status_t rv;
  45. apr_global_mutex_t *m;
  46. m = (apr_global_mutex_t *)apr_palloc(pool, sizeof(*m));
  47. m->pool = pool;
  48. rv = apr_proc_mutex_create(&m->proc_mutex, fname, mech, m->pool);
  49. if (rv != APR_SUCCESS) {
  50. return rv;
  51. }
  52. #if APR_HAS_THREADS
  53. if (m->proc_mutex->inter_meth->flags & APR_PROCESS_LOCK_MECH_IS_GLOBAL) {
  54. m->thread_mutex = NULL; /* We don't need a thread lock. */
  55. }
  56. else {
  57. rv = apr_thread_mutex_create(&m->thread_mutex,
  58. APR_THREAD_MUTEX_DEFAULT, m->pool);
  59. if (rv != APR_SUCCESS) {
  60. rv = apr_proc_mutex_destroy(m->proc_mutex);
  61. return rv;
  62. }
  63. }
  64. #endif /* APR_HAS_THREADS */
  65. apr_pool_cleanup_register(m->pool, (void *)m,
  66. global_mutex_cleanup, apr_pool_cleanup_null);
  67. *mutex = m;
  68. return APR_SUCCESS;
  69. }
  70. APR_DECLARE(apr_status_t) apr_global_mutex_child_init(
  71. apr_global_mutex_t **mutex,
  72. const char *fname,
  73. apr_pool_t *pool)
  74. {
  75. apr_status_t rv;
  76. rv = apr_proc_mutex_child_init(&((*mutex)->proc_mutex), fname, pool);
  77. return rv;
  78. }
  79. APR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex)
  80. {
  81. apr_status_t rv;
  82. #if APR_HAS_THREADS
  83. if (mutex->thread_mutex) {
  84. rv = apr_thread_mutex_lock(mutex->thread_mutex);
  85. if (rv != APR_SUCCESS) {
  86. return rv;
  87. }
  88. }
  89. #endif /* APR_HAS_THREADS */
  90. rv = apr_proc_mutex_lock(mutex->proc_mutex);
  91. #if APR_HAS_THREADS
  92. if (rv != APR_SUCCESS) {
  93. if (mutex->thread_mutex) {
  94. (void)apr_thread_mutex_unlock(mutex->thread_mutex);
  95. }
  96. }
  97. #endif /* APR_HAS_THREADS */
  98. return rv;
  99. }
  100. APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex)
  101. {
  102. apr_status_t rv;
  103. #if APR_HAS_THREADS
  104. if (mutex->thread_mutex) {
  105. rv = apr_thread_mutex_trylock(mutex->thread_mutex);
  106. if (rv != APR_SUCCESS) {
  107. return rv;
  108. }
  109. }
  110. #endif /* APR_HAS_THREADS */
  111. rv = apr_proc_mutex_trylock(mutex->proc_mutex);
  112. #if APR_HAS_THREADS
  113. if (rv != APR_SUCCESS) {
  114. if (mutex->thread_mutex) {
  115. (void)apr_thread_mutex_unlock(mutex->thread_mutex);
  116. }
  117. }
  118. #endif /* APR_HAS_THREADS */
  119. return rv;
  120. }
  121. APR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex)
  122. {
  123. apr_status_t rv;
  124. rv = apr_proc_mutex_unlock(mutex->proc_mutex);
  125. #if APR_HAS_THREADS
  126. if (mutex->thread_mutex) {
  127. if (rv != APR_SUCCESS) {
  128. (void)apr_thread_mutex_unlock(mutex->thread_mutex);
  129. }
  130. else {
  131. rv = apr_thread_mutex_unlock(mutex->thread_mutex);
  132. }
  133. }
  134. #endif /* APR_HAS_THREADS */
  135. return rv;
  136. }
  137. APR_DECLARE(apr_status_t) apr_os_global_mutex_get(apr_os_global_mutex_t *ospmutex,
  138. apr_global_mutex_t *pmutex)
  139. {
  140. ospmutex->pool = pmutex->pool;
  141. ospmutex->proc_mutex = pmutex->proc_mutex;
  142. #if APR_HAS_THREADS
  143. ospmutex->thread_mutex = pmutex->thread_mutex;
  144. #endif
  145. return APR_SUCCESS;
  146. }
  147. APR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex)
  148. {
  149. return apr_pool_cleanup_run(mutex->pool, mutex, global_mutex_cleanup);
  150. }
  151. APR_POOL_IMPLEMENT_ACCESSOR(global_mutex)