atomic.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.h"
  17. #include "apr_atomic.h"
  18. #include <stdlib.h>
  19. apr_status_t apr_atomic_init(apr_pool_t *p)
  20. {
  21. return APR_SUCCESS;
  22. }
  23. apr_uint32_t apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
  24. {
  25. apr_uint32_t old, new_val;
  26. old = *mem; /* old is automatically updated on cs failure */
  27. do {
  28. new_val = old + val;
  29. } while (__cs(&old, (cs_t *)mem, new_val));
  30. return old;
  31. }
  32. void apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
  33. {
  34. apr_uint32_t old, new_val;
  35. old = *mem; /* old is automatically updated on cs failure */
  36. do {
  37. new_val = old - val;
  38. } while (__cs(&old, (cs_t *)mem, new_val));
  39. }
  40. apr_uint32_t apr_atomic_inc32(volatile apr_uint32_t *mem)
  41. {
  42. return apr_atomic_add32(mem, 1);
  43. }
  44. int apr_atomic_dec32(volatile apr_uint32_t *mem)
  45. {
  46. apr_uint32_t old, new_val;
  47. old = *mem; /* old is automatically updated on cs failure */
  48. do {
  49. new_val = old - 1;
  50. } while (__cs(&old, (cs_t *)mem, new_val));
  51. return new_val != 0;
  52. }
  53. apr_uint32_t apr_atomic_read32(volatile apr_uint32_t *mem)
  54. {
  55. return *mem;
  56. }
  57. void apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
  58. {
  59. *mem = val;
  60. }
  61. apr_uint32_t apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t swap,
  62. apr_uint32_t cmp)
  63. {
  64. apr_uint32_t old = cmp;
  65. __cs(&old, (cs_t *)mem, swap);
  66. return old; /* old is automatically updated from mem on cs failure */
  67. }
  68. #if APR_SIZEOF_VOIDP == 4
  69. void *apr_atomic_casptr(volatile void **mem_ptr,
  70. void *swap_ptr,
  71. const void *cmp_ptr)
  72. {
  73. __cs1(&cmp_ptr, /* automatically updated from mem on __cs1 failure */
  74. mem_ptr, /* set from swap when __cs1 succeeds */
  75. &swap_ptr);
  76. return (void *)cmp_ptr;
  77. }
  78. #elif APR_SIZEOF_VOIDP == 8
  79. void *apr_atomic_casptr(volatile void **mem_ptr,
  80. void *swap_ptr,
  81. const void *cmp_ptr)
  82. {
  83. __csg(&cmp_ptr, /* automatically updated from mem on __csg failure */
  84. mem_ptr, /* set from swap when __csg succeeds */
  85. &swap_ptr);
  86. return (void *)cmp_ptr;
  87. }
  88. #else
  89. #error APR_SIZEOF_VOIDP value not supported
  90. #endif /* APR_SIZEOF_VOIDP */
  91. apr_uint32_t apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
  92. {
  93. apr_uint32_t old, new_val;
  94. old = *mem; /* old is automatically updated on cs failure */
  95. do {
  96. new_val = val;
  97. } while (__cs(&old, (cs_t *)mem, new_val));
  98. return old;
  99. }
  100. APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem_ptr, void *new_ptr)
  101. {
  102. void *old_ptr;
  103. old_ptr = *(void **)mem_ptr; /* old is automatically updated on cs failure */
  104. #if APR_SIZEOF_VOIDP == 4
  105. do {
  106. } while (__cs1(&old_ptr, mem_ptr, &new_ptr));
  107. #elif APR_SIZEOF_VOIDP == 8
  108. do {
  109. } while (__csg(&old_ptr, mem_ptr, &new_ptr));
  110. #else
  111. #error APR_SIZEOF_VOIDP value not supported
  112. #endif /* APR_SIZEOF_VOIDP */
  113. return old_ptr;
  114. }