atomic.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 2003-2017 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifndef _AARCH64_ATOMIC_MACHINE_H
  15. #define _AARCH64_ATOMIC_MACHINE_H 1
  16. #define typeof __typeof__
  17. #include <stdint.h>
  18. #include <sysdep.h>
  19. typedef int8_t atomic8_t;
  20. typedef int16_t atomic16_t;
  21. typedef int32_t atomic32_t;
  22. typedef int64_t atomic64_t;
  23. typedef uint8_t uatomic8_t;
  24. typedef uint16_t uatomic16_t;
  25. typedef uint32_t uatomic32_t;
  26. typedef uint64_t uatomic64_t;
  27. typedef intptr_t atomicptr_t;
  28. typedef uintptr_t uatomicptr_t;
  29. typedef intmax_t atomic_max_t;
  30. typedef uintmax_t uatomic_max_t;
  31. #define __HAVE_64B_ATOMICS 1
  32. #define USE_ATOMIC_COMPILER_BUILTINS 1
  33. /* Compare and exchange.
  34. For all "bool" routines, we return FALSE if exchange succesful. */
  35. # define __arch_compare_and_exchange_bool_8_int(mem, newval, oldval, model) \
  36. ({ \
  37. typeof (*mem) __oldval = (oldval); \
  38. !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
  39. model, __ATOMIC_RELAXED); \
  40. })
  41. # define __arch_compare_and_exchange_bool_16_int(mem, newval, oldval, model) \
  42. ({ \
  43. typeof (*mem) __oldval = (oldval); \
  44. !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
  45. model, __ATOMIC_RELAXED); \
  46. })
  47. # define __arch_compare_and_exchange_bool_32_int(mem, newval, oldval, model) \
  48. ({ \
  49. typeof (*mem) __oldval = (oldval); \
  50. !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
  51. model, __ATOMIC_RELAXED); \
  52. })
  53. # define __arch_compare_and_exchange_bool_64_int(mem, newval, oldval, model) \
  54. ({ \
  55. typeof (*mem) __oldval = (oldval); \
  56. !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
  57. model, __ATOMIC_RELAXED); \
  58. })
  59. # define __arch_compare_and_exchange_val_8_int(mem, newval, oldval, model) \
  60. ({ \
  61. typeof (*mem) __oldval = (oldval); \
  62. __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
  63. model, __ATOMIC_RELAXED); \
  64. __oldval; \
  65. })
  66. # define __arch_compare_and_exchange_val_16_int(mem, newval, oldval, model) \
  67. ({ \
  68. typeof (*mem) __oldval = (oldval); \
  69. __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
  70. model, __ATOMIC_RELAXED); \
  71. __oldval; \
  72. })
  73. # define __arch_compare_and_exchange_val_32_int(mem, newval, oldval, model) \
  74. ({ \
  75. typeof (*mem) __oldval = (oldval); \
  76. __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
  77. model, __ATOMIC_RELAXED); \
  78. __oldval; \
  79. })
  80. # define __arch_compare_and_exchange_val_64_int(mem, newval, oldval, model) \
  81. ({ \
  82. typeof (*mem) __oldval = (oldval); \
  83. __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
  84. model, __ATOMIC_RELAXED); \
  85. __oldval; \
  86. })
  87. /* Compare and exchange with "acquire" semantics, ie barrier after. */
  88. # define atomic_compare_and_exchange_bool_acq(mem, new, old) \
  89. __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
  90. mem, new, old, __ATOMIC_ACQUIRE)
  91. # define atomic_compare_and_exchange_val_acq(mem, new, old) \
  92. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  93. mem, new, old, __ATOMIC_ACQUIRE)
  94. /* Compare and exchange with "release" semantics, ie barrier before. */
  95. # define atomic_compare_and_exchange_val_rel(mem, new, old) \
  96. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  97. mem, new, old, __ATOMIC_RELEASE)
  98. /* Atomic exchange (without compare). */
  99. # define __arch_exchange_8_int(mem, newval, model) \
  100. __atomic_exchange_n (mem, newval, model)
  101. # define __arch_exchange_16_int(mem, newval, model) \
  102. __atomic_exchange_n (mem, newval, model)
  103. # define __arch_exchange_32_int(mem, newval, model) \
  104. __atomic_exchange_n (mem, newval, model)
  105. # define __arch_exchange_64_int(mem, newval, model) \
  106. __atomic_exchange_n (mem, newval, model)
  107. # define atomic_exchange_acq(mem, value) \
  108. __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_ACQUIRE)
  109. # define atomic_exchange_rel(mem, value) \
  110. __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_RELEASE)
  111. /* Atomically add value and return the previous (unincremented) value. */
  112. # define __arch_exchange_and_add_8_int(mem, value, model) \
  113. __atomic_fetch_add (mem, value, model)
  114. # define __arch_exchange_and_add_16_int(mem, value, model) \
  115. __atomic_fetch_add (mem, value, model)
  116. # define __arch_exchange_and_add_32_int(mem, value, model) \
  117. __atomic_fetch_add (mem, value, model)
  118. # define __arch_exchange_and_add_64_int(mem, value, model) \
  119. __atomic_fetch_add (mem, value, model)
  120. # define atomic_exchange_and_add_acq(mem, value) \
  121. __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
  122. __ATOMIC_ACQUIRE)
  123. # define atomic_exchange_and_add_rel(mem, value) \
  124. __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
  125. __ATOMIC_RELEASE)
  126. /* Barrier macro. */
  127. #define atomic_full_barrier() __sync_synchronize()
  128. #endif