proc_mutex.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_private.h"
  18. #include "fspr_general.h"
  19. #include "fspr_strings.h"
  20. #include "fspr_portable.h"
  21. #include "fspr_arch_file_io.h"
  22. #include "fspr_arch_proc_mutex.h"
  23. #include "fspr_arch_misc.h"
  24. static fspr_status_t proc_mutex_cleanup(void *mutex_)
  25. {
  26. fspr_proc_mutex_t *mutex = mutex_;
  27. if (mutex->handle) {
  28. if (CloseHandle(mutex->handle) == 0) {
  29. return fspr_get_os_error();
  30. }
  31. }
  32. return APR_SUCCESS;
  33. }
  34. APR_DECLARE(fspr_status_t) fspr_proc_mutex_create(fspr_proc_mutex_t **mutex,
  35. const char *fname,
  36. fspr_lockmech_e mech,
  37. fspr_pool_t *pool)
  38. {
  39. HANDLE hMutex;
  40. void *mutexkey;
  41. /* res_name_from_filename turns fname into a pseduo-name
  42. * without slashes or backslashes, and prepends the \global
  43. * prefix on Win2K and later
  44. */
  45. if (fname) {
  46. mutexkey = res_name_from_filename(fname, 1, pool);
  47. }
  48. else {
  49. mutexkey = NULL;
  50. }
  51. #if APR_HAS_UNICODE_FS
  52. IF_WIN_OS_IS_UNICODE
  53. {
  54. hMutex = CreateMutexW(NULL, FALSE, mutexkey);
  55. }
  56. #endif
  57. #if APR_HAS_ANSI_FS
  58. ELSE_WIN_OS_IS_ANSI
  59. {
  60. hMutex = CreateMutexA(NULL, FALSE, mutexkey);
  61. }
  62. #endif
  63. if (!hMutex) {
  64. return fspr_get_os_error();
  65. }
  66. *mutex = (fspr_proc_mutex_t *)fspr_palloc(pool, sizeof(fspr_proc_mutex_t));
  67. (*mutex)->pool = pool;
  68. (*mutex)->handle = hMutex;
  69. (*mutex)->fname = fname;
  70. fspr_pool_cleanup_register((*mutex)->pool, *mutex,
  71. proc_mutex_cleanup, fspr_pool_cleanup_null);
  72. return APR_SUCCESS;
  73. }
  74. APR_DECLARE(fspr_status_t) fspr_proc_mutex_child_init(fspr_proc_mutex_t **mutex,
  75. const char *fname,
  76. fspr_pool_t *pool)
  77. {
  78. HANDLE hMutex;
  79. void *mutexkey;
  80. if (!fname) {
  81. /* Reinitializing unnamed mutexes is a noop in the Unix code. */
  82. return APR_SUCCESS;
  83. }
  84. /* res_name_from_filename turns file into a pseudo-name
  85. * without slashes or backslashes, and prepends the \global
  86. * prefix on Win2K and later
  87. */
  88. mutexkey = res_name_from_filename(fname, 1, pool);
  89. #if APR_HAS_UNICODE_FS
  90. IF_WIN_OS_IS_UNICODE
  91. {
  92. hMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, mutexkey);
  93. }
  94. #endif
  95. #if APR_HAS_ANSI_FS
  96. ELSE_WIN_OS_IS_ANSI
  97. {
  98. hMutex = OpenMutexA(MUTEX_ALL_ACCESS, FALSE, mutexkey);
  99. }
  100. #endif
  101. if (!hMutex) {
  102. return fspr_get_os_error();
  103. }
  104. *mutex = (fspr_proc_mutex_t *)fspr_palloc(pool, sizeof(fspr_proc_mutex_t));
  105. (*mutex)->pool = pool;
  106. (*mutex)->handle = hMutex;
  107. (*mutex)->fname = fname;
  108. fspr_pool_cleanup_register((*mutex)->pool, *mutex,
  109. proc_mutex_cleanup, fspr_pool_cleanup_null);
  110. return APR_SUCCESS;
  111. }
  112. APR_DECLARE(fspr_status_t) fspr_proc_mutex_lock(fspr_proc_mutex_t *mutex)
  113. {
  114. DWORD rv;
  115. rv = WaitForSingleObject(mutex->handle, INFINITE);
  116. if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
  117. return APR_SUCCESS;
  118. }
  119. else if (rv == WAIT_TIMEOUT) {
  120. return APR_EBUSY;
  121. }
  122. return fspr_get_os_error();
  123. }
  124. APR_DECLARE(fspr_status_t) fspr_proc_mutex_trylock(fspr_proc_mutex_t *mutex)
  125. {
  126. DWORD rv;
  127. rv = WaitForSingleObject(mutex->handle, 0);
  128. if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
  129. return APR_SUCCESS;
  130. }
  131. else if (rv == WAIT_TIMEOUT) {
  132. return APR_EBUSY;
  133. }
  134. return fspr_get_os_error();
  135. }
  136. APR_DECLARE(fspr_status_t) fspr_proc_mutex_unlock(fspr_proc_mutex_t *mutex)
  137. {
  138. if (ReleaseMutex(mutex->handle) == 0) {
  139. return fspr_get_os_error();
  140. }
  141. return APR_SUCCESS;
  142. }
  143. APR_DECLARE(fspr_status_t) fspr_proc_mutex_destroy(fspr_proc_mutex_t *mutex)
  144. {
  145. fspr_status_t stat;
  146. stat = proc_mutex_cleanup(mutex);
  147. if (stat == APR_SUCCESS) {
  148. fspr_pool_cleanup_kill(mutex->pool, mutex, proc_mutex_cleanup);
  149. }
  150. return stat;
  151. }
  152. APR_DECLARE(fspr_status_t) fspr_proc_mutex_cleanup(void *mutex)
  153. {
  154. return fspr_proc_mutex_destroy((fspr_proc_mutex_t *)mutex);
  155. }
  156. APR_DECLARE(const char *) fspr_proc_mutex_lockfile(fspr_proc_mutex_t *mutex)
  157. {
  158. return NULL;
  159. }
  160. APR_DECLARE(const char *) fspr_proc_mutex_name(fspr_proc_mutex_t *mutex)
  161. {
  162. return mutex->fname;
  163. }
  164. APR_DECLARE(const char *) fspr_proc_mutex_defname(void)
  165. {
  166. return "win32mutex";
  167. }
  168. APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
  169. /* Implement OS-specific accessors defined in fspr_portable.h */
  170. APR_DECLARE(fspr_status_t) fspr_os_proc_mutex_get(fspr_os_proc_mutex_t *ospmutex,
  171. fspr_proc_mutex_t *mutex)
  172. {
  173. *ospmutex = mutex->handle;
  174. return APR_SUCCESS;
  175. }
  176. APR_DECLARE(fspr_status_t) fspr_os_proc_mutex_put(fspr_proc_mutex_t **pmutex,
  177. fspr_os_proc_mutex_t *ospmutex,
  178. fspr_pool_t *pool)
  179. {
  180. if (pool == NULL) {
  181. return APR_ENOPOOL;
  182. }
  183. if ((*pmutex) == NULL) {
  184. (*pmutex) = (fspr_proc_mutex_t *)fspr_palloc(pool,
  185. sizeof(fspr_proc_mutex_t));
  186. (*pmutex)->pool = pool;
  187. }
  188. (*pmutex)->handle = *ospmutex;
  189. return APR_SUCCESS;
  190. }