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 "fspr.h"
  17. #include "fspr_strings.h"
  18. #include "fspr_arch_global_mutex.h"
  19. #include "fspr_proc_mutex.h"
  20. #include "fspr_thread_mutex.h"
  21. #include "fspr_portable.h"
  22. static fspr_status_t global_mutex_cleanup(void *data)
  23. {
  24. fspr_global_mutex_t *m = (fspr_global_mutex_t *)data;
  25. fspr_status_t rv;
  26. rv = fspr_proc_mutex_destroy(m->proc_mutex);
  27. #if APR_HAS_THREADS
  28. if (m->thread_mutex) {
  29. if (rv != APR_SUCCESS) {
  30. (void)fspr_thread_mutex_destroy(m->thread_mutex);
  31. }
  32. else {
  33. rv = fspr_thread_mutex_destroy(m->thread_mutex);
  34. }
  35. }
  36. #endif /* APR_HAS_THREADS */
  37. return rv;
  38. }
  39. APR_DECLARE(fspr_status_t) fspr_global_mutex_create(fspr_global_mutex_t **mutex,
  40. const char *fname,
  41. fspr_lockmech_e mech,
  42. fspr_pool_t *pool)
  43. {
  44. fspr_status_t rv;
  45. fspr_global_mutex_t *m;
  46. m = (fspr_global_mutex_t *)fspr_palloc(pool, sizeof(*m));
  47. m->pool = pool;
  48. rv = fspr_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 = fspr_thread_mutex_create(&m->thread_mutex,
  58. APR_THREAD_MUTEX_DEFAULT, m->pool);
  59. if (rv != APR_SUCCESS) {
  60. rv = fspr_proc_mutex_destroy(m->proc_mutex);
  61. return rv;
  62. }
  63. }
  64. #endif /* APR_HAS_THREADS */
  65. fspr_pool_cleanup_register(m->pool, (void *)m,
  66. global_mutex_cleanup, fspr_pool_cleanup_null);
  67. *mutex = m;
  68. return APR_SUCCESS;
  69. }
  70. APR_DECLARE(fspr_status_t) fspr_global_mutex_child_init(
  71. fspr_global_mutex_t **mutex,
  72. const char *fname,
  73. fspr_pool_t *pool)
  74. {
  75. fspr_status_t rv;
  76. rv = fspr_proc_mutex_child_init(&((*mutex)->proc_mutex), fname, pool);
  77. return rv;
  78. }
  79. APR_DECLARE(fspr_status_t) fspr_global_mutex_lock(fspr_global_mutex_t *mutex)
  80. {
  81. fspr_status_t rv;
  82. #if APR_HAS_THREADS
  83. if (mutex->thread_mutex) {
  84. rv = fspr_thread_mutex_lock(mutex->thread_mutex);
  85. if (rv != APR_SUCCESS) {
  86. return rv;
  87. }
  88. }
  89. #endif /* APR_HAS_THREADS */
  90. rv = fspr_proc_mutex_lock(mutex->proc_mutex);
  91. #if APR_HAS_THREADS
  92. if (rv != APR_SUCCESS) {
  93. if (mutex->thread_mutex) {
  94. (void)fspr_thread_mutex_unlock(mutex->thread_mutex);
  95. }
  96. }
  97. #endif /* APR_HAS_THREADS */
  98. return rv;
  99. }
  100. APR_DECLARE(fspr_status_t) fspr_global_mutex_trylock(fspr_global_mutex_t *mutex)
  101. {
  102. fspr_status_t rv;
  103. #if APR_HAS_THREADS
  104. if (mutex->thread_mutex) {
  105. rv = fspr_thread_mutex_trylock(mutex->thread_mutex);
  106. if (rv != APR_SUCCESS) {
  107. return rv;
  108. }
  109. }
  110. #endif /* APR_HAS_THREADS */
  111. rv = fspr_proc_mutex_trylock(mutex->proc_mutex);
  112. #if APR_HAS_THREADS
  113. if (rv != APR_SUCCESS) {
  114. if (mutex->thread_mutex) {
  115. (void)fspr_thread_mutex_unlock(mutex->thread_mutex);
  116. }
  117. }
  118. #endif /* APR_HAS_THREADS */
  119. return rv;
  120. }
  121. APR_DECLARE(fspr_status_t) fspr_global_mutex_unlock(fspr_global_mutex_t *mutex)
  122. {
  123. fspr_status_t rv;
  124. rv = fspr_proc_mutex_unlock(mutex->proc_mutex);
  125. #if APR_HAS_THREADS
  126. if (mutex->thread_mutex) {
  127. if (rv != APR_SUCCESS) {
  128. (void)fspr_thread_mutex_unlock(mutex->thread_mutex);
  129. }
  130. else {
  131. rv = fspr_thread_mutex_unlock(mutex->thread_mutex);
  132. }
  133. }
  134. #endif /* APR_HAS_THREADS */
  135. return rv;
  136. }
  137. APR_DECLARE(fspr_status_t) fspr_os_global_mutex_get(fspr_os_global_mutex_t *ospmutex,
  138. fspr_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(fspr_status_t) fspr_global_mutex_destroy(fspr_global_mutex_t *mutex)
  148. {
  149. return fspr_pool_cleanup_run(mutex->pool, mutex, global_mutex_cleanup);
  150. }
  151. APR_POOL_IMPLEMENT_ACCESSOR(global_mutex)