atomic.h 5.6 KB

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