thread_rwlock.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_arch_thread_rwlock.h"
  17. #include "fspr_private.h"
  18. #if APR_HAS_THREADS
  19. #ifdef HAVE_PTHREAD_RWLOCKS
  20. /* The rwlock must be initialized but not locked by any thread when
  21. * cleanup is called. */
  22. static fspr_status_t thread_rwlock_cleanup(void *data)
  23. {
  24. fspr_thread_rwlock_t *rwlock = (fspr_thread_rwlock_t *)data;
  25. fspr_status_t stat;
  26. stat = pthread_rwlock_destroy(&rwlock->rwlock);
  27. #ifdef PTHREAD_SETS_ERRNO
  28. if (stat) {
  29. stat = errno;
  30. }
  31. #endif
  32. return stat;
  33. }
  34. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_create(fspr_thread_rwlock_t **rwlock,
  35. fspr_pool_t *pool)
  36. {
  37. fspr_thread_rwlock_t *new_rwlock;
  38. fspr_status_t stat;
  39. new_rwlock = fspr_palloc(pool, sizeof(fspr_thread_rwlock_t));
  40. new_rwlock->pool = pool;
  41. if ((stat = pthread_rwlock_init(&new_rwlock->rwlock, NULL))) {
  42. #ifdef PTHREAD_SETS_ERRNO
  43. stat = errno;
  44. #endif
  45. return stat;
  46. }
  47. fspr_pool_cleanup_register(new_rwlock->pool,
  48. (void *)new_rwlock, thread_rwlock_cleanup,
  49. fspr_pool_cleanup_null);
  50. *rwlock = new_rwlock;
  51. return APR_SUCCESS;
  52. }
  53. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_rdlock(fspr_thread_rwlock_t *rwlock)
  54. {
  55. fspr_status_t stat;
  56. stat = pthread_rwlock_rdlock(&rwlock->rwlock);
  57. #ifdef PTHREAD_SETS_ERRNO
  58. if (stat) {
  59. stat = errno;
  60. }
  61. #endif
  62. return stat;
  63. }
  64. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_tryrdlock(fspr_thread_rwlock_t *rwlock)
  65. {
  66. fspr_status_t stat;
  67. stat = pthread_rwlock_tryrdlock(&rwlock->rwlock);
  68. #ifdef PTHREAD_SETS_ERRNO
  69. if (stat) {
  70. stat = errno;
  71. }
  72. #endif
  73. /* Normalize the return code. */
  74. if (stat == EBUSY)
  75. stat = APR_EBUSY;
  76. return stat;
  77. }
  78. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_wrlock(fspr_thread_rwlock_t *rwlock)
  79. {
  80. fspr_status_t stat;
  81. stat = pthread_rwlock_wrlock(&rwlock->rwlock);
  82. #ifdef PTHREAD_SETS_ERRNO
  83. if (stat) {
  84. stat = errno;
  85. }
  86. #endif
  87. return stat;
  88. }
  89. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_trywrlock(fspr_thread_rwlock_t *rwlock)
  90. {
  91. fspr_status_t stat;
  92. stat = pthread_rwlock_trywrlock(&rwlock->rwlock);
  93. #ifdef PTHREAD_SETS_ERRNO
  94. if (stat) {
  95. stat = errno;
  96. }
  97. #endif
  98. /* Normalize the return code. */
  99. if (stat == EBUSY)
  100. stat = APR_EBUSY;
  101. return stat;
  102. }
  103. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_unlock(fspr_thread_rwlock_t *rwlock)
  104. {
  105. fspr_status_t stat;
  106. stat = pthread_rwlock_unlock(&rwlock->rwlock);
  107. #ifdef PTHREAD_SETS_ERRNO
  108. if (stat) {
  109. stat = errno;
  110. }
  111. #endif
  112. return stat;
  113. }
  114. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_destroy(fspr_thread_rwlock_t *rwlock)
  115. {
  116. return fspr_pool_cleanup_run(rwlock->pool, rwlock, thread_rwlock_cleanup);
  117. }
  118. #else /* HAVE_PTHREAD_RWLOCKS */
  119. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_create(fspr_thread_rwlock_t **rwlock,
  120. fspr_pool_t *pool)
  121. {
  122. return APR_ENOTIMPL;
  123. }
  124. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_rdlock(fspr_thread_rwlock_t *rwlock)
  125. {
  126. return APR_ENOTIMPL;
  127. }
  128. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_tryrdlock(fspr_thread_rwlock_t *rwlock)
  129. {
  130. return APR_ENOTIMPL;
  131. }
  132. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_wrlock(fspr_thread_rwlock_t *rwlock)
  133. {
  134. return APR_ENOTIMPL;
  135. }
  136. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_trywrlock(fspr_thread_rwlock_t *rwlock)
  137. {
  138. return APR_ENOTIMPL;
  139. }
  140. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_unlock(fspr_thread_rwlock_t *rwlock)
  141. {
  142. return APR_ENOTIMPL;
  143. }
  144. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_destroy(fspr_thread_rwlock_t *rwlock)
  145. {
  146. return APR_ENOTIMPL;
  147. }
  148. #endif /* HAVE_PTHREAD_RWLOCKS */
  149. APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)
  150. #endif /* APR_HAS_THREADS */