thread_rwlock.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_general.h"
  17. #include "fspr_lib.h"
  18. #include "fspr_strings.h"
  19. #include "fspr_portable.h"
  20. #include "fspr_arch_thread_rwlock.h"
  21. #include "fspr_arch_file_io.h"
  22. #include <string.h>
  23. static fspr_status_t thread_rwlock_cleanup(void *therwlock)
  24. {
  25. fspr_thread_rwlock_t *rwlock = therwlock;
  26. return fspr_thread_rwlock_destroy(rwlock);
  27. }
  28. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_create(fspr_thread_rwlock_t **rwlock,
  29. fspr_pool_t *pool)
  30. {
  31. fspr_thread_rwlock_t *new_rwlock;
  32. ULONG rc;
  33. new_rwlock = (fspr_thread_rwlock_t *)fspr_palloc(pool, sizeof(fspr_thread_rwlock_t));
  34. new_rwlock->pool = pool;
  35. new_rwlock->readers = 0;
  36. rc = DosCreateMutexSem(NULL, &(new_rwlock->write_lock), 0, FALSE);
  37. if (rc)
  38. return APR_FROM_OS_ERROR(rc);
  39. rc = DosCreateEventSem(NULL, &(new_rwlock->read_done), 0, FALSE);
  40. if (rc)
  41. return APR_FROM_OS_ERROR(rc);
  42. *rwlock = new_rwlock;
  43. if (!rc)
  44. fspr_pool_cleanup_register(pool, new_rwlock, thread_rwlock_cleanup,
  45. fspr_pool_cleanup_null);
  46. return APR_FROM_OS_ERROR(rc);
  47. }
  48. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_rdlock(fspr_thread_rwlock_t *rwlock)
  49. {
  50. ULONG rc, posts;
  51. rc = DosRequestMutexSem(rwlock->write_lock, SEM_INDEFINITE_WAIT);
  52. if (rc)
  53. return APR_FROM_OS_ERROR(rc);
  54. /* We've successfully acquired the writer mutex so we can't be locked
  55. * for write which means it's ok to add a reader lock. The writer mutex
  56. * doubles as race condition protection for the readers counter.
  57. */
  58. rwlock->readers++;
  59. DosResetEventSem(rwlock->read_done, &posts);
  60. rc = DosReleaseMutexSem(rwlock->write_lock);
  61. return APR_FROM_OS_ERROR(rc);
  62. }
  63. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_tryrdlock(fspr_thread_rwlock_t *rwlock)
  64. {
  65. /* As above but with different wait time */
  66. ULONG rc, posts;
  67. rc = DosRequestMutexSem(rwlock->write_lock, SEM_IMMEDIATE_RETURN);
  68. if (rc)
  69. return APR_FROM_OS_ERROR(rc);
  70. rwlock->readers++;
  71. DosResetEventSem(rwlock->read_done, &posts);
  72. rc = DosReleaseMutexSem(rwlock->write_lock);
  73. return APR_FROM_OS_ERROR(rc);
  74. }
  75. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_wrlock(fspr_thread_rwlock_t *rwlock)
  76. {
  77. ULONG rc;
  78. rc = DosRequestMutexSem(rwlock->write_lock, SEM_INDEFINITE_WAIT);
  79. if (rc)
  80. return APR_FROM_OS_ERROR(rc);
  81. /* We've got the writer lock but we have to wait for all readers to
  82. * unlock before it's ok to use it
  83. */
  84. if (rwlock->readers) {
  85. rc = DosWaitEventSem(rwlock->read_done, SEM_INDEFINITE_WAIT);
  86. if (rc)
  87. DosReleaseMutexSem(rwlock->write_lock);
  88. }
  89. return APR_FROM_OS_ERROR(rc);
  90. }
  91. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_trywrlock(fspr_thread_rwlock_t *rwlock)
  92. {
  93. ULONG rc;
  94. rc = DosRequestMutexSem(rwlock->write_lock, SEM_IMMEDIATE_RETURN);
  95. if (rc)
  96. return APR_FROM_OS_ERROR(rc);
  97. /* We've got the writer lock but we have to wait for all readers to
  98. * unlock before it's ok to use it
  99. */
  100. if (rwlock->readers) {
  101. /* There are readers active, give up */
  102. DosReleaseMutexSem(rwlock->write_lock);
  103. rc = ERROR_TIMEOUT;
  104. }
  105. return APR_FROM_OS_ERROR(rc);
  106. }
  107. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_unlock(fspr_thread_rwlock_t *rwlock)
  108. {
  109. ULONG rc;
  110. /* First, guess that we're unlocking a writer */
  111. rc = DosReleaseMutexSem(rwlock->write_lock);
  112. if (rc == ERROR_NOT_OWNER) {
  113. /* Nope, we must have a read lock */
  114. if (rwlock->readers) {
  115. DosEnterCritSec();
  116. rwlock->readers--;
  117. if (rwlock->readers == 0) {
  118. DosPostEventSem(rwlock->read_done);
  119. }
  120. DosExitCritSec();
  121. rc = 0;
  122. }
  123. }
  124. return APR_FROM_OS_ERROR(rc);
  125. }
  126. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_destroy(fspr_thread_rwlock_t *rwlock)
  127. {
  128. ULONG rc;
  129. if (rwlock->write_lock == 0)
  130. return APR_SUCCESS;
  131. while (DosReleaseMutexSem(rwlock->write_lock) == 0);
  132. rc = DosCloseMutexSem(rwlock->write_lock);
  133. if (!rc) {
  134. rwlock->write_lock = 0;
  135. DosCloseEventSem(rwlock->read_done);
  136. return APR_SUCCESS;
  137. }
  138. return APR_FROM_OS_ERROR(rc);
  139. }
  140. APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)