mutex.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "apr_arch_atomic.h"
  17. #ifdef USE_ATOMICS_GENERIC
  18. #include <stdlib.h>
  19. #if APR_HAS_THREADS
  20. # define DECLARE_MUTEX_LOCKED(name, mem) \
  21. apr_thread_mutex_t *name = mutex_hash(mem)
  22. # define MUTEX_UNLOCK(name) \
  23. do { \
  24. if (apr_thread_mutex_unlock(name) != APR_SUCCESS) \
  25. abort(); \
  26. } while (0)
  27. #else
  28. # define DECLARE_MUTEX_LOCKED(name, mem)
  29. # define MUTEX_UNLOCK(name)
  30. # warning Be warned: using stubs for all atomic operations
  31. #endif
  32. #if APR_HAS_THREADS
  33. static apr_thread_mutex_t **hash_mutex;
  34. #define NUM_ATOMIC_HASH 7
  35. /* shift by 2 to get rid of alignment issues */
  36. #define ATOMIC_HASH(x) (unsigned int)(((unsigned long)(x)>>2)%(unsigned int)NUM_ATOMIC_HASH)
  37. static apr_status_t atomic_cleanup(void *data)
  38. {
  39. if (hash_mutex == data)
  40. hash_mutex = NULL;
  41. return APR_SUCCESS;
  42. }
  43. APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p)
  44. {
  45. int i;
  46. apr_status_t rv;
  47. if (hash_mutex != NULL)
  48. return APR_SUCCESS;
  49. hash_mutex = apr_palloc(p, sizeof(apr_thread_mutex_t*) * NUM_ATOMIC_HASH);
  50. apr_pool_cleanup_register(p, hash_mutex, atomic_cleanup,
  51. apr_pool_cleanup_null);
  52. for (i = 0; i < NUM_ATOMIC_HASH; i++) {
  53. rv = apr_thread_mutex_create(&(hash_mutex[i]),
  54. APR_THREAD_MUTEX_DEFAULT, p);
  55. if (rv != APR_SUCCESS) {
  56. return rv;
  57. }
  58. }
  59. return APR_SUCCESS;
  60. }
  61. static APR_INLINE apr_thread_mutex_t *mutex_hash(volatile apr_uint32_t *mem)
  62. {
  63. apr_thread_mutex_t *mutex = hash_mutex[ATOMIC_HASH(mem)];
  64. if (apr_thread_mutex_lock(mutex) != APR_SUCCESS) {
  65. abort();
  66. }
  67. return mutex;
  68. }
  69. #else
  70. APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p)
  71. {
  72. return APR_SUCCESS;
  73. }
  74. #endif /* APR_HAS_THREADS */
  75. APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem)
  76. {
  77. return *mem;
  78. }
  79. APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
  80. {
  81. DECLARE_MUTEX_LOCKED(mutex, mem);
  82. *mem = val;
  83. MUTEX_UNLOCK(mutex);
  84. }
  85. APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
  86. {
  87. apr_uint32_t old_value;
  88. DECLARE_MUTEX_LOCKED(mutex, mem);
  89. old_value = *mem;
  90. *mem += val;
  91. MUTEX_UNLOCK(mutex);
  92. return old_value;
  93. }
  94. APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
  95. {
  96. DECLARE_MUTEX_LOCKED(mutex, mem);
  97. *mem -= val;
  98. MUTEX_UNLOCK(mutex);
  99. }
  100. APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem)
  101. {
  102. return apr_atomic_add32(mem, 1);
  103. }
  104. APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem)
  105. {
  106. apr_uint32_t new;
  107. DECLARE_MUTEX_LOCKED(mutex, mem);
  108. (*mem)--;
  109. new = *mem;
  110. MUTEX_UNLOCK(mutex);
  111. return new;
  112. }
  113. APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
  114. apr_uint32_t cmp)
  115. {
  116. apr_uint32_t prev;
  117. DECLARE_MUTEX_LOCKED(mutex, mem);
  118. prev = *mem;
  119. if (prev == cmp) {
  120. *mem = with;
  121. }
  122. MUTEX_UNLOCK(mutex);
  123. return prev;
  124. }
  125. APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
  126. {
  127. apr_uint32_t prev;
  128. DECLARE_MUTEX_LOCKED(mutex, mem);
  129. prev = *mem;
  130. *mem = val;
  131. MUTEX_UNLOCK(mutex);
  132. return prev;
  133. }
  134. APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
  135. {
  136. void *prev;
  137. DECLARE_MUTEX_LOCKED(mutex, *mem);
  138. prev = *(void **)mem;
  139. if (prev == cmp) {
  140. *mem = with;
  141. }
  142. MUTEX_UNLOCK(mutex);
  143. return prev;
  144. }
  145. APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with)
  146. {
  147. void *prev;
  148. DECLARE_MUTEX_LOCKED(mutex, *mem);
  149. prev = *(void **)mem;
  150. *mem = with;
  151. MUTEX_UNLOCK(mutex);
  152. return prev;
  153. }
  154. #endif /* USE_ATOMICS_GENERIC */