proc_mutex.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_proc_mutex.h"
  20. #include "fspr_strings.h"
  21. #include "fspr_portable.h"
  22. static fspr_status_t _proc_mutex_cleanup(void * data)
  23. {
  24. fspr_proc_mutex_t *lock = (fspr_proc_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_proc_mutex_create(fspr_proc_mutex_t **mutex,
  39. const char *fname,
  40. fspr_lockmech_e mech,
  41. fspr_pool_t *pool)
  42. {
  43. fspr_proc_mutex_t *new;
  44. fspr_status_t stat = APR_SUCCESS;
  45. if (mech != APR_LOCK_DEFAULT) {
  46. return APR_ENOTIMPL;
  47. }
  48. new = (fspr_proc_mutex_t *)fspr_pcalloc(pool, sizeof(fspr_proc_mutex_t));
  49. if (new == NULL){
  50. return APR_ENOMEM;
  51. }
  52. if ((stat = create_sem(0, "APR_Lock")) < B_NO_ERROR) {
  53. _proc_mutex_cleanup(new);
  54. return stat;
  55. }
  56. new->LockCount = 0;
  57. new->Lock = stat;
  58. new->pool = pool;
  59. fspr_pool_cleanup_register(new->pool, (void *)new, _proc_mutex_cleanup,
  60. fspr_pool_cleanup_null);
  61. (*mutex) = new;
  62. return APR_SUCCESS;
  63. }
  64. APR_DECLARE(fspr_status_t) fspr_proc_mutex_child_init(fspr_proc_mutex_t **mutex,
  65. const char *fname,
  66. fspr_pool_t *pool)
  67. {
  68. return APR_SUCCESS;
  69. }
  70. APR_DECLARE(fspr_status_t) fspr_proc_mutex_lock(fspr_proc_mutex_t *mutex)
  71. {
  72. int32 stat;
  73. if (atomic_add(&mutex->LockCount, 1) > 0) {
  74. if ((stat = acquire_sem(mutex->Lock)) < B_NO_ERROR) {
  75. atomic_add(&mutex->LockCount, -1);
  76. return stat;
  77. }
  78. }
  79. return APR_SUCCESS;
  80. }
  81. APR_DECLARE(fspr_status_t) fspr_proc_mutex_trylock(fspr_proc_mutex_t *mutex)
  82. {
  83. return APR_ENOTIMPL;
  84. }
  85. APR_DECLARE(fspr_status_t) fspr_proc_mutex_unlock(fspr_proc_mutex_t *mutex)
  86. {
  87. int32 stat;
  88. if (atomic_add(&mutex->LockCount, -1) > 1) {
  89. if ((stat = release_sem(mutex->Lock)) < B_NO_ERROR) {
  90. atomic_add(&mutex->LockCount, 1);
  91. return stat;
  92. }
  93. }
  94. return APR_SUCCESS;
  95. }
  96. APR_DECLARE(fspr_status_t) fspr_proc_mutex_destroy(fspr_proc_mutex_t *mutex)
  97. {
  98. fspr_status_t stat;
  99. if ((stat = _proc_mutex_cleanup(mutex)) == APR_SUCCESS) {
  100. fspr_pool_cleanup_kill(mutex->pool, mutex, _proc_mutex_cleanup);
  101. return APR_SUCCESS;
  102. }
  103. return stat;
  104. }
  105. APR_DECLARE(fspr_status_t) fspr_proc_mutex_cleanup(void *mutex)
  106. {
  107. return _proc_mutex_cleanup(mutex);
  108. }
  109. APR_DECLARE(const char *) fspr_proc_mutex_lockfile(fspr_proc_mutex_t *mutex)
  110. {
  111. return NULL;
  112. }
  113. APR_DECLARE(const char *) fspr_proc_mutex_name(fspr_proc_mutex_t *mutex)
  114. {
  115. return "beossem";
  116. }
  117. APR_DECLARE(const char *) fspr_proc_mutex_defname(void)
  118. {
  119. return "beossem";
  120. }
  121. APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
  122. /* Implement OS-specific accessors defined in fspr_portable.h */
  123. APR_DECLARE(fspr_status_t) fspr_os_proc_mutex_get(fspr_os_proc_mutex_t *ospmutex,
  124. fspr_proc_mutex_t *pmutex)
  125. {
  126. ospmutex->sem = pmutex->Lock;
  127. ospmutex->ben = pmutex->LockCount;
  128. return APR_SUCCESS;
  129. }
  130. APR_DECLARE(fspr_status_t) fspr_os_proc_mutex_put(fspr_proc_mutex_t **pmutex,
  131. fspr_os_proc_mutex_t *ospmutex,
  132. fspr_pool_t *pool)
  133. {
  134. if (pool == NULL) {
  135. return APR_ENOPOOL;
  136. }
  137. if ((*pmutex) == NULL) {
  138. (*pmutex) = (fspr_proc_mutex_t *)fspr_pcalloc(pool, sizeof(fspr_proc_mutex_t));
  139. (*pmutex)->pool = pool;
  140. }
  141. (*pmutex)->Lock = ospmutex->sem;
  142. (*pmutex)->LockCount = ospmutex->ben;
  143. return APR_SUCCESS;
  144. }