2
0

thread_mutex.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_mutex.h"
  17. #define APR_WANT_MEMFUNC
  18. #include "fspr_want.h"
  19. #if APR_HAS_THREADS
  20. static fspr_status_t thread_mutex_cleanup(void *data)
  21. {
  22. fspr_thread_mutex_t *mutex = data;
  23. fspr_status_t rv;
  24. rv = pthread_mutex_destroy(&mutex->mutex);
  25. #ifdef PTHREAD_SETS_ERRNO
  26. if (rv) {
  27. rv = errno;
  28. }
  29. #endif
  30. return rv;
  31. }
  32. APR_DECLARE(fspr_status_t) fspr_thread_mutex_create(fspr_thread_mutex_t **mutex,
  33. unsigned int flags,
  34. fspr_pool_t *pool)
  35. {
  36. fspr_thread_mutex_t *new_mutex;
  37. fspr_status_t rv;
  38. #ifndef HAVE_PTHREAD_MUTEX_RECURSIVE
  39. if (flags & APR_THREAD_MUTEX_NESTED) {
  40. return APR_ENOTIMPL;
  41. }
  42. #endif
  43. new_mutex = fspr_pcalloc(pool, sizeof(fspr_thread_mutex_t));
  44. new_mutex->pool = pool;
  45. #ifdef HAVE_PTHREAD_MUTEX_RECURSIVE
  46. if (flags & APR_THREAD_MUTEX_NESTED) {
  47. pthread_mutexattr_t mattr;
  48. rv = pthread_mutexattr_init(&mattr);
  49. if (rv) return rv;
  50. rv = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
  51. if (rv) {
  52. pthread_mutexattr_destroy(&mattr);
  53. return rv;
  54. }
  55. rv = pthread_mutex_init(&new_mutex->mutex, &mattr);
  56. pthread_mutexattr_destroy(&mattr);
  57. } else
  58. #endif
  59. rv = pthread_mutex_init(&new_mutex->mutex, NULL);
  60. if (rv) {
  61. #ifdef PTHREAD_SETS_ERRNO
  62. rv = errno;
  63. #endif
  64. return rv;
  65. }
  66. fspr_pool_cleanup_register(new_mutex->pool,
  67. new_mutex, thread_mutex_cleanup,
  68. fspr_pool_cleanup_null);
  69. *mutex = new_mutex;
  70. return APR_SUCCESS;
  71. }
  72. APR_DECLARE(fspr_status_t) fspr_thread_mutex_lock(fspr_thread_mutex_t *mutex)
  73. {
  74. fspr_status_t rv;
  75. rv = pthread_mutex_lock(&mutex->mutex);
  76. #ifdef PTHREAD_SETS_ERRNO
  77. if (rv) {
  78. rv = errno;
  79. }
  80. #endif
  81. return rv;
  82. }
  83. APR_DECLARE(fspr_status_t) fspr_thread_mutex_trylock(fspr_thread_mutex_t *mutex)
  84. {
  85. fspr_status_t rv;
  86. rv = pthread_mutex_trylock(&mutex->mutex);
  87. if (rv) {
  88. #ifdef PTHREAD_SETS_ERRNO
  89. rv = errno;
  90. #endif
  91. return (rv == EBUSY) ? APR_EBUSY : rv;
  92. }
  93. return APR_SUCCESS;
  94. }
  95. APR_DECLARE(fspr_status_t) fspr_thread_mutex_unlock(fspr_thread_mutex_t *mutex)
  96. {
  97. fspr_status_t status;
  98. status = pthread_mutex_unlock(&mutex->mutex);
  99. #ifdef PTHREAD_SETS_ERRNO
  100. if (status) {
  101. status = errno;
  102. }
  103. #endif
  104. return status;
  105. }
  106. APR_DECLARE(fspr_status_t) fspr_thread_mutex_destroy(fspr_thread_mutex_t *mutex)
  107. {
  108. return fspr_pool_cleanup_run(mutex->pool, mutex, thread_mutex_cleanup);
  109. }
  110. APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)
  111. #endif /* APR_HAS_THREADS */