2
0

thread_rwlock.c 5.4 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_private.h"
  18. #include "fspr_general.h"
  19. #include "fspr_strings.h"
  20. #include "win32/fspr_arch_thread_rwlock.h"
  21. #include "fspr_portable.h"
  22. static fspr_status_t thread_rwlock_cleanup(void *data)
  23. {
  24. fspr_thread_rwlock_t *rwlock = data;
  25. if (! CloseHandle(rwlock->read_event))
  26. return fspr_get_os_error();
  27. DeleteCriticalSection(&rwlock->read_section);
  28. if (! CloseHandle(rwlock->write_mutex))
  29. return fspr_get_os_error();
  30. return APR_SUCCESS;
  31. }
  32. APR_DECLARE(fspr_status_t)fspr_thread_rwlock_create(fspr_thread_rwlock_t **rwlock,
  33. fspr_pool_t *pool)
  34. {
  35. *rwlock = fspr_palloc(pool, sizeof(**rwlock));
  36. (*rwlock)->pool = pool;
  37. (*rwlock)->readers = 0;
  38. if (! ((*rwlock)->read_event = CreateEvent(NULL, TRUE, FALSE, NULL))) {
  39. *rwlock = NULL;
  40. return fspr_get_os_error();
  41. }
  42. if (! ((*rwlock)->write_mutex = CreateMutex(NULL, FALSE, NULL))) {
  43. CloseHandle((*rwlock)->read_event);
  44. *rwlock = NULL;
  45. return fspr_get_os_error();
  46. }
  47. InitializeCriticalSection(&(*rwlock)->read_section);
  48. fspr_pool_cleanup_register(pool, *rwlock, thread_rwlock_cleanup,
  49. fspr_pool_cleanup_null);
  50. return APR_SUCCESS;
  51. }
  52. static fspr_status_t fspr_thread_rwlock_rdlock_core(fspr_thread_rwlock_t *rwlock,
  53. DWORD milliseconds)
  54. {
  55. DWORD code;
  56. EnterCriticalSection(&rwlock->read_section);
  57. code = WaitForSingleObject(rwlock->write_mutex, milliseconds);
  58. if (code == WAIT_FAILED || code == WAIT_TIMEOUT) {
  59. LeaveCriticalSection(&rwlock->read_section);
  60. return APR_FROM_OS_ERROR(code);
  61. }
  62. /* We've successfully acquired the writer mutex, we can't be locked
  63. * for write, so it's OK to add the reader lock. The writer mutex
  64. * doubles as race condition protection for the readers counter.
  65. */
  66. InterlockedIncrement(&rwlock->readers);
  67. if (! ResetEvent(rwlock->read_event)) {
  68. LeaveCriticalSection(&rwlock->read_section);
  69. return fspr_get_os_error();
  70. }
  71. if (! ReleaseMutex(rwlock->write_mutex)) {
  72. LeaveCriticalSection(&rwlock->read_section);
  73. return fspr_get_os_error();
  74. }
  75. LeaveCriticalSection(&rwlock->read_section);
  76. return APR_SUCCESS;
  77. }
  78. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_rdlock(fspr_thread_rwlock_t *rwlock)
  79. {
  80. return fspr_thread_rwlock_rdlock_core(rwlock, INFINITE);
  81. }
  82. APR_DECLARE(fspr_status_t)
  83. fspr_thread_rwlock_tryrdlock(fspr_thread_rwlock_t *rwlock)
  84. {
  85. return fspr_thread_rwlock_rdlock_core(rwlock, 0);
  86. }
  87. static fspr_status_t
  88. fspr_thread_rwlock_wrlock_core(fspr_thread_rwlock_t *rwlock, DWORD milliseconds)
  89. {
  90. DWORD code = WaitForSingleObject(rwlock->write_mutex, milliseconds);
  91. if (code == WAIT_FAILED || code == WAIT_TIMEOUT)
  92. return APR_FROM_OS_ERROR(code);
  93. /* We've got the writer lock but we have to wait for all readers to
  94. * unlock before it's ok to use it.
  95. */
  96. if (rwlock->readers) {
  97. /* Must wait for readers to finish before returning, unless this
  98. * is an trywrlock (milliseconds == 0):
  99. */
  100. code = milliseconds
  101. ? WaitForSingleObject(rwlock->read_event, milliseconds)
  102. : WAIT_TIMEOUT;
  103. if (code == WAIT_FAILED || code == WAIT_TIMEOUT) {
  104. /* Unable to wait for readers to finish, release write lock: */
  105. if (! ReleaseMutex(rwlock->write_mutex))
  106. return fspr_get_os_error();
  107. return APR_FROM_OS_ERROR(code);
  108. }
  109. }
  110. return APR_SUCCESS;
  111. }
  112. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_wrlock(fspr_thread_rwlock_t *rwlock)
  113. {
  114. return fspr_thread_rwlock_wrlock_core(rwlock, INFINITE);
  115. }
  116. APR_DECLARE(fspr_status_t)fspr_thread_rwlock_trywrlock(fspr_thread_rwlock_t *rwlock)
  117. {
  118. return fspr_thread_rwlock_wrlock_core(rwlock, 0);
  119. }
  120. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_unlock(fspr_thread_rwlock_t *rwlock)
  121. {
  122. fspr_status_t rv = 0;
  123. /* First, guess that we're unlocking a writer */
  124. if (! ReleaseMutex(rwlock->write_mutex))
  125. rv = fspr_get_os_error();
  126. if (rv == APR_FROM_OS_ERROR(ERROR_NOT_OWNER)) {
  127. /* Nope, we must have a read lock */
  128. if (rwlock->readers &&
  129. ! InterlockedDecrement(&rwlock->readers) &&
  130. ! SetEvent(rwlock->read_event)) {
  131. rv = fspr_get_os_error();
  132. }
  133. else {
  134. rv = 0;
  135. }
  136. }
  137. return rv;
  138. }
  139. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_destroy(fspr_thread_rwlock_t *rwlock)
  140. {
  141. return fspr_pool_cleanup_run(rwlock->pool, rwlock, thread_rwlock_cleanup);
  142. }
  143. APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)