thread_mutex.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /*Read/Write locking implementation based on the MultiLock code from
  17. * Stephen Beaulieu <hippo@be.com>
  18. */
  19. #include "fspr_arch_thread_mutex.h"
  20. #include "fspr_strings.h"
  21. #include "fspr_portable.h"
  22. static fspr_status_t _thread_mutex_cleanup(void * data)
  23. {
  24. fspr_thread_mutex_t *lock = (fspr_thread_mutex_t*)data;
  25. if (lock->LockCount != 0) {
  26. /* we're still locked... */
  27. while (atomic_add(&lock->LockCount , -1) > 1){
  28. /* OK we had more than one person waiting on the lock so
  29. * the sem is also locked. Release it until we have no more
  30. * locks left.
  31. */
  32. release_sem (lock->Lock);
  33. }
  34. }
  35. delete_sem(lock->Lock);
  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. fspr_thread_mutex_t *new_m;
  43. fspr_status_t stat = APR_SUCCESS;
  44. new_m = (fspr_thread_mutex_t *)fspr_pcalloc(pool, sizeof(fspr_thread_mutex_t));
  45. if (new_m == NULL){
  46. return APR_ENOMEM;
  47. }
  48. if ((stat = create_sem(0, "APR_Lock")) < B_NO_ERROR) {
  49. _thread_mutex_cleanup(new_m);
  50. return stat;
  51. }
  52. new_m->LockCount = 0;
  53. new_m->Lock = stat;
  54. new_m->pool = pool;
  55. /* Optimal default is APR_THREAD_MUTEX_UNNESTED,
  56. * no additional checks required for either flag.
  57. */
  58. new_m->nested = flags & APR_THREAD_MUTEX_NESTED;
  59. fspr_pool_cleanup_register(new_m->pool, (void *)new_m, _thread_mutex_cleanup,
  60. fspr_pool_cleanup_null);
  61. (*mutex) = new_m;
  62. return APR_SUCCESS;
  63. }
  64. #if APR_HAS_CREATE_LOCKS_NP
  65. APR_DECLARE(fspr_status_t) fspr_thread_mutex_create_np(fspr_thread_mutex_t **mutex,
  66. const char *fname,
  67. fspr_lockmech_e_np mech,
  68. fspr_pool_t *pool)
  69. {
  70. return APR_ENOTIMPL;
  71. }
  72. #endif
  73. APR_DECLARE(fspr_status_t) fspr_thread_mutex_lock(fspr_thread_mutex_t *mutex)
  74. {
  75. int32 stat;
  76. thread_id me = find_thread(NULL);
  77. if (mutex->nested && mutex->owner == me) {
  78. mutex->owner_ref++;
  79. return APR_SUCCESS;
  80. }
  81. if (atomic_add(&mutex->LockCount, 1) > 0) {
  82. if ((stat = acquire_sem(mutex->Lock)) < B_NO_ERROR) {
  83. /* Oh dear, acquire_sem failed!! */
  84. atomic_add(&mutex->LockCount, -1);
  85. return stat;
  86. }
  87. }
  88. mutex->owner = me;
  89. mutex->owner_ref = 1;
  90. return APR_SUCCESS;
  91. }
  92. APR_DECLARE(fspr_status_t) fspr_thread_mutex_trylock(fspr_thread_mutex_t *mutex)
  93. {
  94. return APR_ENOTIMPL;
  95. }
  96. APR_DECLARE(fspr_status_t) fspr_thread_mutex_unlock(fspr_thread_mutex_t *mutex)
  97. {
  98. int32 stat;
  99. if (mutex->nested && mutex->owner == find_thread(NULL)) {
  100. mutex->owner_ref--;
  101. if (mutex->owner_ref > 0)
  102. return APR_SUCCESS;
  103. }
  104. if (atomic_add(&mutex->LockCount, -1) > 1) {
  105. if ((stat = release_sem(mutex->Lock)) < B_NO_ERROR) {
  106. atomic_add(&mutex->LockCount, 1);
  107. return stat;
  108. }
  109. }
  110. mutex->owner = -1;
  111. mutex->owner_ref = 0;
  112. return APR_SUCCESS;
  113. }
  114. APR_DECLARE(fspr_status_t) fspr_thread_mutex_destroy(fspr_thread_mutex_t *mutex)
  115. {
  116. fspr_status_t stat;
  117. if ((stat = _thread_mutex_cleanup(mutex)) == APR_SUCCESS) {
  118. fspr_pool_cleanup_kill(mutex->pool, mutex, _thread_mutex_cleanup);
  119. return APR_SUCCESS;
  120. }
  121. return stat;
  122. }
  123. APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)