apr_atomic.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #ifndef APR_ATOMIC_H
  17. #define APR_ATOMIC_H
  18. /**
  19. * @file apr_atomic.h
  20. * @brief APR Atomic Operations
  21. */
  22. #include "apr.h"
  23. #include "apr_pools.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * @defgroup apr_atomic Atomic Operations
  29. * @ingroup APR
  30. * @{
  31. */
  32. /**
  33. * this function is required on some platforms to initialize the
  34. * atomic operation's internal structures
  35. * @param p pool
  36. * @return APR_SUCCESS on successful completion
  37. * @remark Programs do NOT need to call this directly. APR will call this
  38. * automatically from apr_initialize().
  39. * @internal
  40. */
  41. APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p);
  42. /*
  43. * Atomic operations on 32-bit values
  44. * Note: Each of these functions internally implements a memory barrier
  45. * on platforms that require it
  46. */
  47. /**
  48. * atomically read an apr_uint32_t from memory
  49. * @param mem the pointer
  50. */
  51. APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem);
  52. /**
  53. * atomically set an apr_uint32_t in memory
  54. * @param mem pointer to the object
  55. * @param val value that the object will assume
  56. */
  57. APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val);
  58. /**
  59. * atomically add 'val' to an apr_uint32_t
  60. * @param mem pointer to the object
  61. * @param val amount to add
  62. * @return old value pointed to by mem
  63. */
  64. APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val);
  65. /**
  66. * atomically subtract 'val' from an apr_uint32_t
  67. * @param mem pointer to the object
  68. * @param val amount to subtract
  69. */
  70. APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val);
  71. /**
  72. * atomically increment an apr_uint32_t by 1
  73. * @param mem pointer to the object
  74. * @return old value pointed to by mem
  75. */
  76. APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem);
  77. /**
  78. * atomically decrement an apr_uint32_t by 1
  79. * @param mem pointer to the atomic value
  80. * @return zero if the value becomes zero on decrement, otherwise non-zero
  81. */
  82. APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem);
  83. /**
  84. * compare an apr_uint32_t's value with 'cmp'.
  85. * If they are the same swap the value with 'with'
  86. * @param mem pointer to the value
  87. * @param with what to swap it with
  88. * @param cmp the value to compare it to
  89. * @return the old value of *mem
  90. */
  91. APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
  92. apr_uint32_t cmp);
  93. /**
  94. * exchange an apr_uint32_t's value with 'val'.
  95. * @param mem pointer to the value
  96. * @param val what to swap it with
  97. * @return the old value of *mem
  98. */
  99. APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val);
  100. /*
  101. * Atomic operations on 64-bit values
  102. * Note: Each of these functions internally implements a memory barrier
  103. * on platforms that require it
  104. */
  105. /**
  106. * atomically read an apr_uint64_t from memory
  107. * @param mem the pointer
  108. */
  109. APR_DECLARE(apr_uint64_t) apr_atomic_read64(volatile apr_uint64_t *mem);
  110. /**
  111. * atomically set an apr_uint64_t in memory
  112. * @param mem pointer to the object
  113. * @param val value that the object will assume
  114. */
  115. APR_DECLARE(void) apr_atomic_set64(volatile apr_uint64_t *mem, apr_uint64_t val);
  116. /**
  117. * atomically add 'val' to an apr_uint64_t
  118. * @param mem pointer to the object
  119. * @param val amount to add
  120. * @return old value pointed to by mem
  121. */
  122. APR_DECLARE(apr_uint64_t) apr_atomic_add64(volatile apr_uint64_t *mem, apr_uint64_t val);
  123. /**
  124. * atomically subtract 'val' from an apr_uint64_t
  125. * @param mem pointer to the object
  126. * @param val amount to subtract
  127. */
  128. APR_DECLARE(void) apr_atomic_sub64(volatile apr_uint64_t *mem, apr_uint64_t val);
  129. /**
  130. * atomically increment an apr_uint64_t by 1
  131. * @param mem pointer to the object
  132. * @return old value pointed to by mem
  133. */
  134. APR_DECLARE(apr_uint64_t) apr_atomic_inc64(volatile apr_uint64_t *mem);
  135. /**
  136. * atomically decrement an apr_uint64_t by 1
  137. * @param mem pointer to the atomic value
  138. * @return zero if the value becomes zero on decrement, otherwise non-zero
  139. */
  140. APR_DECLARE(int) apr_atomic_dec64(volatile apr_uint64_t *mem);
  141. /**
  142. * compare an apr_uint64_t's value with 'cmp'.
  143. * If they are the same swap the value with 'with'
  144. * @param mem pointer to the value
  145. * @param with what to swap it with
  146. * @param cmp the value to compare it to
  147. * @return the old value of *mem
  148. */
  149. APR_DECLARE(apr_uint64_t) apr_atomic_cas64(volatile apr_uint64_t *mem, apr_uint64_t with,
  150. apr_uint64_t cmp);
  151. /**
  152. * exchange an apr_uint64_t's value with 'val'.
  153. * @param mem pointer to the value
  154. * @param val what to swap it with
  155. * @return the old value of *mem
  156. */
  157. APR_DECLARE(apr_uint64_t) apr_atomic_xchg64(volatile apr_uint64_t *mem, apr_uint64_t val);
  158. /**
  159. * compare the pointer's value with cmp.
  160. * If they are the same swap the value with 'with'
  161. * @param mem pointer to the pointer
  162. * @param with what to swap it with
  163. * @param cmp the value to compare it to
  164. * @return the old value of the pointer
  165. */
  166. APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp);
  167. /**
  168. * exchange a pair of pointer values
  169. * @param mem pointer to the pointer
  170. * @param with what to swap it with
  171. * @return the old value of the pointer
  172. */
  173. APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with);
  174. /** @} */
  175. #ifdef __cplusplus
  176. }
  177. #endif
  178. #endif /* !APR_ATOMIC_H */