atomic-machine.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Low-level functions for atomic operations. Mips version.
  2. Copyright (C) 2005-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _MIPS_ATOMIC_MACHINE_H
  16. #define _MIPS_ATOMIC_MACHINE_H 1
  17. #include <stdint.h>
  18. #include <inttypes.h>
  19. #include <sgidefs.h>
  20. typedef int32_t atomic32_t;
  21. typedef uint32_t uatomic32_t;
  22. typedef int_fast32_t atomic_fast32_t;
  23. typedef uint_fast32_t uatomic_fast32_t;
  24. typedef int64_t atomic64_t;
  25. typedef uint64_t uatomic64_t;
  26. typedef int_fast64_t atomic_fast64_t;
  27. typedef uint_fast64_t uatomic_fast64_t;
  28. typedef intptr_t atomicptr_t;
  29. typedef uintptr_t uatomicptr_t;
  30. typedef intmax_t atomic_max_t;
  31. typedef uintmax_t uatomic_max_t;
  32. #if _MIPS_SIM == _ABIO32 && __mips < 2
  33. #define MIPS_PUSH_MIPS2 ".set mips2\n\t"
  34. #else
  35. #define MIPS_PUSH_MIPS2
  36. #endif
  37. #if _MIPS_SIM == _ABIO32 || _MIPS_SIM == _ABIN32
  38. #define __HAVE_64B_ATOMICS 0
  39. #else
  40. #define __HAVE_64B_ATOMICS 1
  41. #endif
  42. /* See the comments in <sys/asm.h> about the use of the sync instruction. */
  43. #ifndef MIPS_SYNC
  44. # define MIPS_SYNC sync
  45. #endif
  46. #define MIPS_SYNC_STR_2(X) #X
  47. #define MIPS_SYNC_STR_1(X) MIPS_SYNC_STR_2(X)
  48. #define MIPS_SYNC_STR MIPS_SYNC_STR_1(MIPS_SYNC)
  49. #define USE_ATOMIC_COMPILER_BUILTINS 1
  50. /* MIPS is an LL/SC machine. However, XLP has a direct atomic exchange
  51. instruction which will be used by __atomic_exchange_n. */
  52. #ifdef _MIPS_ARCH_XLP
  53. # define ATOMIC_EXCHANGE_USES_CAS 0
  54. #else
  55. # define ATOMIC_EXCHANGE_USES_CAS 1
  56. #endif
  57. /* Compare and exchange.
  58. For all "bool" routines, we return FALSE if exchange succesful. */
  59. #define __arch_compare_and_exchange_bool_8_int(mem, newval, oldval, model) \
  60. (abort (), 0)
  61. #define __arch_compare_and_exchange_bool_16_int(mem, newval, oldval, model) \
  62. (abort (), 0)
  63. #define __arch_compare_and_exchange_bool_32_int(mem, newval, oldval, model) \
  64. ({ \
  65. typeof (*mem) __oldval = (oldval); \
  66. !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
  67. model, __ATOMIC_RELAXED); \
  68. })
  69. #define __arch_compare_and_exchange_val_8_int(mem, newval, oldval, model) \
  70. (abort (), (typeof(*mem)) 0)
  71. #define __arch_compare_and_exchange_val_16_int(mem, newval, oldval, model) \
  72. (abort (), (typeof(*mem)) 0)
  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. #if _MIPS_SIM == _ABIO32
  81. /* We can't do an atomic 64-bit operation in O32. */
  82. # define __arch_compare_and_exchange_bool_64_int(mem, newval, oldval, model) \
  83. (abort (), 0)
  84. # define __arch_compare_and_exchange_val_64_int(mem, newval, oldval, model) \
  85. (abort (), (typeof(*mem)) 0)
  86. #else
  87. # define __arch_compare_and_exchange_bool_64_int(mem, newval, oldval, model) \
  88. __arch_compare_and_exchange_bool_32_int (mem, newval, oldval, model)
  89. # define __arch_compare_and_exchange_val_64_int(mem, newval, oldval, model) \
  90. __arch_compare_and_exchange_val_32_int (mem, newval, oldval, model)
  91. #endif
  92. /* Compare and exchange with "acquire" semantics, ie barrier after. */
  93. #define atomic_compare_and_exchange_bool_acq(mem, new, old) \
  94. __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
  95. mem, new, old, __ATOMIC_ACQUIRE)
  96. #define atomic_compare_and_exchange_val_acq(mem, new, old) \
  97. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  98. mem, new, old, __ATOMIC_ACQUIRE)
  99. /* Compare and exchange with "release" semantics, ie barrier before. */
  100. #define atomic_compare_and_exchange_val_rel(mem, new, old) \
  101. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  102. mem, new, old, __ATOMIC_RELEASE)
  103. /* Atomic exchange (without compare). */
  104. #define __arch_exchange_8_int(mem, newval, model) \
  105. (abort (), (typeof(*mem)) 0)
  106. #define __arch_exchange_16_int(mem, newval, model) \
  107. (abort (), (typeof(*mem)) 0)
  108. #define __arch_exchange_32_int(mem, newval, model) \
  109. __atomic_exchange_n (mem, newval, model)
  110. #if _MIPS_SIM == _ABIO32
  111. /* We can't do an atomic 64-bit operation in O32. */
  112. # define __arch_exchange_64_int(mem, newval, model) \
  113. (abort (), (typeof(*mem)) 0)
  114. #else
  115. # define __arch_exchange_64_int(mem, newval, model) \
  116. __atomic_exchange_n (mem, newval, model)
  117. #endif
  118. #define atomic_exchange_acq(mem, value) \
  119. __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_ACQUIRE)
  120. #define atomic_exchange_rel(mem, value) \
  121. __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_RELEASE)
  122. /* Atomically add value and return the previous (unincremented) value. */
  123. #define __arch_exchange_and_add_8_int(mem, value, model) \
  124. (abort (), (typeof(*mem)) 0)
  125. #define __arch_exchange_and_add_16_int(mem, value, model) \
  126. (abort (), (typeof(*mem)) 0)
  127. #define __arch_exchange_and_add_32_int(mem, value, model) \
  128. __atomic_fetch_add (mem, value, model)
  129. #if _MIPS_SIM == _ABIO32
  130. /* We can't do an atomic 64-bit operation in O32. */
  131. # define __arch_exchange_and_add_64_int(mem, value, model) \
  132. (abort (), (typeof(*mem)) 0)
  133. #else
  134. # define __arch_exchange_and_add_64_int(mem, value, model) \
  135. __atomic_fetch_add (mem, value, model)
  136. #endif
  137. #define atomic_exchange_and_add_acq(mem, value) \
  138. __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
  139. __ATOMIC_ACQUIRE)
  140. #define atomic_exchange_and_add_rel(mem, value) \
  141. __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
  142. __ATOMIC_RELEASE)
  143. /* TODO: More atomic operations could be implemented efficiently; only the
  144. basic requirements are done. */
  145. #ifdef __mips16
  146. # define atomic_full_barrier() __sync_synchronize ()
  147. #else /* !__mips16 */
  148. # define atomic_full_barrier() \
  149. __asm__ __volatile__ (".set push\n\t" \
  150. MIPS_PUSH_MIPS2 \
  151. MIPS_SYNC_STR "\n\t" \
  152. ".set pop" : : : "memory")
  153. #endif /* !__mips16 */
  154. #endif /* atomic-machine.h */