proc_mutex.c 4.8 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 "apr_arch_proc_mutex.h"
  20. #include "apr_strings.h"
  21. #include "apr_portable.h"
  22. static apr_status_t _proc_mutex_cleanup(void * data)
  23. {
  24. apr_proc_mutex_t *lock = (apr_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(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
  39. const char *fname,
  40. apr_lockmech_e mech,
  41. apr_pool_t *pool)
  42. {
  43. apr_proc_mutex_t *new;
  44. apr_status_t stat = APR_SUCCESS;
  45. if (mech != APR_LOCK_DEFAULT) {
  46. return APR_ENOTIMPL;
  47. }
  48. new = (apr_proc_mutex_t *)apr_pcalloc(pool, sizeof(apr_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. apr_pool_cleanup_register(new->pool, (void *)new, _proc_mutex_cleanup,
  60. apr_pool_cleanup_null);
  61. (*mutex) = new;
  62. return APR_SUCCESS;
  63. }
  64. APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
  65. const char *fname,
  66. apr_pool_t *pool)
  67. {
  68. return APR_SUCCESS;
  69. }
  70. APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_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(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
  82. {
  83. return APR_ENOTIMPL;
  84. }
  85. APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_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(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
  97. {
  98. apr_status_t stat;
  99. if ((stat = _proc_mutex_cleanup(mutex)) == APR_SUCCESS) {
  100. apr_pool_cleanup_kill(mutex->pool, mutex, _proc_mutex_cleanup);
  101. return APR_SUCCESS;
  102. }
  103. return stat;
  104. }
  105. APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex)
  106. {
  107. return _proc_mutex_cleanup(mutex);
  108. }
  109. APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex)
  110. {
  111. return NULL;
  112. }
  113. APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex)
  114. {
  115. return "beossem";
  116. }
  117. APR_DECLARE(const char *) apr_proc_mutex_defname(void)
  118. {
  119. return "beossem";
  120. }
  121. APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
  122. /* Implement OS-specific accessors defined in apr_portable.h */
  123. APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
  124. apr_proc_mutex_t *pmutex)
  125. {
  126. ospmutex->sem = pmutex->Lock;
  127. ospmutex->ben = pmutex->LockCount;
  128. return APR_SUCCESS;
  129. }
  130. APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
  131. apr_os_proc_mutex_t *ospmutex,
  132. apr_pool_t *pool)
  133. {
  134. if (pool == NULL) {
  135. return APR_ENOPOOL;
  136. }
  137. if ((*pmutex) == NULL) {
  138. (*pmutex) = (apr_proc_mutex_t *)apr_pcalloc(pool, sizeof(apr_proc_mutex_t));
  139. (*pmutex)->pool = pool;
  140. }
  141. (*pmutex)->Lock = ospmutex->sem;
  142. (*pmutex)->LockCount = ospmutex->ben;
  143. return APR_SUCCESS;
  144. }