thread_mutex.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_arch_thread_mutex.h"
  21. #include "fspr_thread_mutex.h"
  22. #include "fspr_portable.h"
  23. #include "fspr_arch_misc.h"
  24. static fspr_status_t thread_mutex_cleanup(void *data)
  25. {
  26. fspr_thread_mutex_t *lock = data;
  27. if (lock->type == thread_mutex_critical_section) {
  28. lock->type = -1;
  29. DeleteCriticalSection(&lock->section);
  30. }
  31. else {
  32. if (!CloseHandle(lock->handle)) {
  33. return fspr_get_os_error();
  34. }
  35. }
  36. return APR_SUCCESS;
  37. }
  38. APR_DECLARE(fspr_status_t) fspr_thread_mutex_create(fspr_thread_mutex_t **mutex,
  39. unsigned int flags,
  40. fspr_pool_t *pool)
  41. {
  42. (*mutex) = (fspr_thread_mutex_t *)fspr_palloc(pool, sizeof(**mutex));
  43. (*mutex)->pool = pool;
  44. if (flags & APR_THREAD_MUTEX_UNNESTED) {
  45. /* Use an auto-reset signaled event, ready to accept one
  46. * waiting thread.
  47. */
  48. (*mutex)->type = thread_mutex_unnested_event;
  49. (*mutex)->handle = CreateEvent(NULL, FALSE, TRUE, NULL);
  50. }
  51. else {
  52. #if APR_HAS_UNICODE_FS
  53. /* Critical Sections are terrific, performance-wise, on NT.
  54. * On Win9x, we cannot 'try' on a critical section, so we
  55. * use a [slower] mutex object, instead.
  56. */
  57. IF_WIN_OS_IS_UNICODE {
  58. InitializeCriticalSection(&(*mutex)->section);
  59. (*mutex)->type = thread_mutex_critical_section;
  60. }
  61. #endif
  62. #if APR_HAS_ANSI_FS
  63. ELSE_WIN_OS_IS_ANSI {
  64. (*mutex)->type = thread_mutex_nested_mutex;
  65. (*mutex)->handle = CreateMutex(NULL, FALSE, NULL);
  66. }
  67. #endif
  68. }
  69. fspr_pool_cleanup_register((*mutex)->pool, (*mutex), thread_mutex_cleanup,
  70. fspr_pool_cleanup_null);
  71. return APR_SUCCESS;
  72. }
  73. APR_DECLARE(fspr_status_t) fspr_thread_mutex_lock(fspr_thread_mutex_t *mutex)
  74. {
  75. if (mutex->type == thread_mutex_critical_section) {
  76. EnterCriticalSection(&mutex->section);
  77. }
  78. else {
  79. DWORD rv = WaitForSingleObject(mutex->handle, INFINITE);
  80. if ((rv != WAIT_OBJECT_0) && (rv != WAIT_ABANDONED)) {
  81. return (rv == WAIT_TIMEOUT) ? APR_EBUSY : fspr_get_os_error();
  82. }
  83. }
  84. return APR_SUCCESS;
  85. }
  86. APR_DECLARE(fspr_status_t) fspr_thread_mutex_trylock(fspr_thread_mutex_t *mutex)
  87. {
  88. if (mutex->type == thread_mutex_critical_section) {
  89. if (!TryEnterCriticalSection(&mutex->section)) {
  90. return APR_EBUSY;
  91. }
  92. }
  93. else {
  94. DWORD rv = WaitForSingleObject(mutex->handle, 0);
  95. if ((rv != WAIT_OBJECT_0) && (rv != WAIT_ABANDONED)) {
  96. return (rv == WAIT_TIMEOUT) ? APR_EBUSY : fspr_get_os_error();
  97. }
  98. }
  99. return APR_SUCCESS;
  100. }
  101. APR_DECLARE(fspr_status_t) fspr_thread_mutex_unlock(fspr_thread_mutex_t *mutex)
  102. {
  103. if (mutex->type == thread_mutex_critical_section) {
  104. LeaveCriticalSection(&mutex->section);
  105. }
  106. else if (mutex->type == thread_mutex_unnested_event) {
  107. if (!SetEvent(mutex->handle)) {
  108. return fspr_get_os_error();
  109. }
  110. }
  111. else if (mutex->type == thread_mutex_nested_mutex) {
  112. if (!ReleaseMutex(mutex->handle)) {
  113. return fspr_get_os_error();
  114. }
  115. }
  116. return APR_SUCCESS;
  117. }
  118. APR_DECLARE(fspr_status_t) fspr_thread_mutex_destroy(fspr_thread_mutex_t *mutex)
  119. {
  120. return fspr_pool_cleanup_run(mutex->pool, mutex, thread_mutex_cleanup);
  121. }
  122. APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)