atomic.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Atomic operations used inside libc. Linux/SH version.
  2. Copyright (C) 2003 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. #include <stdint.h>
  16. typedef int8_t atomic8_t;
  17. typedef uint8_t uatomic8_t;
  18. typedef int_fast8_t atomic_fast8_t;
  19. typedef uint_fast8_t uatomic_fast8_t;
  20. typedef int16_t atomic16_t;
  21. typedef uint16_t uatomic16_t;
  22. typedef int_fast16_t atomic_fast16_t;
  23. typedef uint_fast16_t uatomic_fast16_t;
  24. typedef int32_t atomic32_t;
  25. typedef uint32_t uatomic32_t;
  26. typedef int_fast32_t atomic_fast32_t;
  27. typedef uint_fast32_t uatomic_fast32_t;
  28. typedef int64_t atomic64_t;
  29. typedef uint64_t uatomic64_t;
  30. typedef int_fast64_t atomic_fast64_t;
  31. typedef uint_fast64_t uatomic_fast64_t;
  32. typedef intptr_t atomicptr_t;
  33. typedef uintptr_t uatomicptr_t;
  34. typedef intmax_t atomic_max_t;
  35. typedef uintmax_t uatomic_max_t;
  36. /* SH kernel has implemented a gUSA ("g" User Space Atomicity) support
  37. for the user space atomicity. The atomicity macros use this scheme.
  38. Reference:
  39. Niibe Yutaka, "gUSA: Simple and Efficient User Space Atomicity
  40. Emulation with Little Kernel Modification", Linux Conference 2002,
  41. Japan. http://lc.linux.or.jp/lc2002/papers/niibe0919h.pdf (in
  42. Japanese).
  43. Niibe Yutaka, "gUSA: User Space Atomicity with Little Kernel
  44. Modification", LinuxTag 2003, Rome.
  45. http://www.semmel.ch/Linuxtag-DVD/talks/170/paper.html (in English).
  46. B.N. Bershad, D. Redell, and J. Ellis, "Fast Mutual Exclusion for
  47. Uniprocessors", Proceedings of the Fifth Architectural Support for
  48. Programming Languages and Operating Systems (ASPLOS), pp. 223-233,
  49. October 1992. http://www.cs.washington.edu/homes/bershad/Papers/Rcs.ps
  50. SuperH ABI:
  51. r15: -(size of atomic instruction sequence) < 0
  52. r0: end point
  53. r1: saved stack pointer
  54. */
  55. #if __GNUC_PREREQ (4, 7)
  56. # define rNOSP "u"
  57. #else
  58. # define rNOSP "r"
  59. #endif
  60. /* Avoid having lots of different versions of compare and exchange,
  61. by having this one complicated version. Parameters:
  62. bwl: b, w or l for 8, 16 and 32 bit versions.
  63. version: val or bool, depending on whether the result is the
  64. previous value or a bool indicating whether the transfer
  65. did happen (note this needs inverting before being
  66. returned in atomic_compare_and_exchange_bool).
  67. */
  68. #define __arch_compare_and_exchange_n(mem, newval, oldval, bwl, version) \
  69. ({ signed long __arch_result; \
  70. __asm__ __volatile__ ("\
  71. .align 2\n\
  72. mova 1f,r0\n\
  73. nop\n\
  74. mov r15,r1\n\
  75. mov #-8,r15\n\
  76. 0: mov." #bwl " @%1,%0\n\
  77. cmp/eq %0,%3\n\
  78. bf 1f\n\
  79. mov." #bwl " %2,@%1\n\
  80. 1: mov r1,r15\n\
  81. .ifeqs \"bool\",\"" #version "\"\n\
  82. movt %0\n\
  83. .endif\n" \
  84. : "=&r" (__arch_result) \
  85. : rNOSP (mem), rNOSP (newval), rNOSP (oldval) \
  86. : "r0", "r1", "t", "memory"); \
  87. __arch_result; })
  88. #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
  89. __arch_compare_and_exchange_n(mem, newval, (int8_t)(oldval), b, val)
  90. #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
  91. __arch_compare_and_exchange_n(mem, newval, (int16_t)(oldval), w, val)
  92. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  93. __arch_compare_and_exchange_n(mem, newval, (int32_t)(oldval), l, val)
  94. /* XXX We do not really need 64-bit compare-and-exchange. At least
  95. not in the moment. Using it would mean causing portability
  96. problems since not many other 32-bit architectures have support for
  97. such an operation. So don't define any code for now. */
  98. # define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  99. (abort (), 0)
  100. /* For "bool" routines, return if the exchange did NOT occur */
  101. #define __arch_compare_and_exchange_bool_8_acq(mem, newval, oldval) \
  102. (! __arch_compare_and_exchange_n(mem, newval, (int8_t)(oldval), b, bool))
  103. #define __arch_compare_and_exchange_bool_16_acq(mem, newval, oldval) \
  104. (! __arch_compare_and_exchange_n(mem, newval, (int16_t)(oldval), w, bool))
  105. #define __arch_compare_and_exchange_bool_32_acq(mem, newval, oldval) \
  106. (! __arch_compare_and_exchange_n(mem, newval, (int32_t)(oldval), l, bool))
  107. # define __arch_compare_and_exchange_bool_64_acq(mem, newval, oldval) \
  108. (abort (), 0)
  109. /* Similar to the above, have one template which can be used in a
  110. number of places. This version returns both the old and the new
  111. values of the location. Parameters:
  112. bwl: b, w or l for 8, 16 and 32 bit versions.
  113. oper: The instruction to perform on the old value.
  114. Note old is not sign extended, so should be an unsigned long.
  115. */
  116. #define __arch_operate_old_new_n(mem, value, old, new, bwl, oper) \
  117. (void) ({ __asm__ __volatile__ ("\
  118. .align 2\n\
  119. mova 1f,r0\n\
  120. mov r15,r1\n\
  121. nop\n\
  122. mov #-8,r15\n\
  123. 0: mov." #bwl " @%2,%0\n\
  124. mov %0,%1\n\
  125. " #oper " %3,%1\n\
  126. mov." #bwl " %1,@%2\n\
  127. 1: mov r1,r15" \
  128. : "=&r" (old), "=&r"(new) \
  129. : rNOSP (mem), rNOSP (value) \
  130. : "r0", "r1", "memory"); \
  131. })
  132. #define __arch_exchange_and_add_8_int(mem, value) \
  133. ({ int32_t __value = (value), __new, __old; \
  134. __arch_operate_old_new_n((mem), __value, __old, __new, b, add); \
  135. __old; })
  136. #define __arch_exchange_and_add_16_int(mem, value) \
  137. ({ int32_t __value = (value), __new, __old; \
  138. __arch_operate_old_new_n((mem), __value, __old, __new, w, add); \
  139. __old; })
  140. #define __arch_exchange_and_add_32_int(mem, value) \
  141. ({ int32_t __value = (value), __new, __old; \
  142. __arch_operate_old_new_n((mem), __value, __old, __new, l, add); \
  143. __old; })
  144. #define __arch_exchange_and_add_64_int(mem, value) \
  145. (abort (), 0)
  146. #define atomic_exchange_and_add(mem, value) \
  147. __atomic_val_bysize (__arch_exchange_and_add, int, mem, value)
  148. /* Again, another template. We get a slight optimisation when the old value
  149. does not need to be returned. Parameters:
  150. bwl: b, w or l for 8, 16 and 32 bit versions.
  151. oper: The instruction to perform on the old value.
  152. */
  153. #define __arch_operate_new_n(mem, value, bwl, oper) \
  154. ({ int32_t __value = (value), __new; \
  155. __asm__ __volatile__ ("\
  156. .align 2\n\
  157. mova 1f,r0\n\
  158. mov r15,r1\n\
  159. mov #-6,r15\n\
  160. 0: mov." #bwl " @%1,%0\n\
  161. " #oper " %2,%0\n\
  162. mov." #bwl " %0,@%1\n\
  163. 1: mov r1,r15" \
  164. : "=&r" (__new) \
  165. : rNOSP (mem), rNOSP (__value) \
  166. : "r0", "r1", "memory"); \
  167. __new; \
  168. })
  169. #define __arch_add_8_int(mem, value) \
  170. __arch_operate_new_n(mem, value, b, add)
  171. #define __arch_add_16_int(mem, value) \
  172. __arch_operate_new_n(mem, value, w, add)
  173. #define __arch_add_32_int(mem, value) \
  174. __arch_operate_new_n(mem, value, l, add)
  175. #define __arch_add_64_int(mem, value) \
  176. (abort (), 0)
  177. #define atomic_add(mem, value) \
  178. ((void) __atomic_val_bysize (__arch_add, int, mem, value))
  179. #define __arch_add_negative_8_int(mem, value) \
  180. (__arch_operate_new_n(mem, value, b, add) < 0)
  181. #define __arch_add_negative_16_int(mem, value) \
  182. (__arch_operate_new_n(mem, value, w, add) < 0)
  183. #define __arch_add_negative_32_int(mem, value) \
  184. (__arch_operate_new_n(mem, value, l, add) < 0)
  185. #define __arch_add_negative_64_int(mem, value) \
  186. (abort (), 0)
  187. #define atomic_add_negative(mem, value) \
  188. __atomic_bool_bysize (__arch_add_negative, int, mem, value)
  189. #define __arch_add_zero_8_int(mem, value) \
  190. (__arch_operate_new_n(mem, value, b, add) == 0)
  191. #define __arch_add_zero_16_int(mem, value) \
  192. (__arch_operate_new_n(mem, value, w, add) == 0)
  193. #define __arch_add_zero_32_int(mem, value) \
  194. (__arch_operate_new_n(mem, value, l, add) == 0)
  195. #define __arch_add_zero_64_int(mem, value) \
  196. (abort (), 0)
  197. #define atomic_add_zero(mem, value) \
  198. __atomic_bool_bysize (__arch_add_zero, int, mem, value)
  199. #define atomic_increment_and_test(mem) atomic_add_zero((mem), 1)
  200. #define atomic_decrement_and_test(mem) atomic_add_zero((mem), -1)
  201. #define __arch_bit_set_8_int(mem, value) \
  202. __arch_operate_new_n(mem, 1<<(value), b, or)
  203. #define __arch_bit_set_16_int(mem, value) \
  204. __arch_operate_new_n(mem, 1<<(value), w, or)
  205. #define __arch_bit_set_32_int(mem, value) \
  206. __arch_operate_new_n(mem, 1<<(value), l, or)
  207. #define __arch_bit_set_64_int(mem, value) \
  208. (abort (), 0)
  209. #define __arch_add_64_int(mem, value) \
  210. (abort (), 0)
  211. #define atomic_bit_set(mem, value) \
  212. ((void) __atomic_val_bysize (__arch_bit_set, int, mem, value))
  213. #define __arch_bit_test_set_8_int(mem, value) \
  214. ({ int32_t __value = 1<<(value), __new, __old; \
  215. __arch_operate_old_new_n((mem), __value, __old, __new, b, or); \
  216. __old & __value; })
  217. #define __arch_bit_test_set_16_int(mem, value) \
  218. ({ int32_t __value = 1<<(value), __new, __old; \
  219. __arch_operate_old_new_n((mem), __value, __old, __new, w, or); \
  220. __old & __value; })
  221. #define __arch_bit_test_set_32_int(mem, value) \
  222. ({ int32_t __value = 1<<(value), __new, __old; \
  223. __arch_operate_old_new_n((mem), __value, __old, __new, l, or); \
  224. __old & __value; })
  225. #define __arch_bit_test_set_64_int(mem, value) \
  226. (abort (), 0)
  227. #define atomic_bit_test_set(mem, value) \
  228. __atomic_val_bysize (__arch_bit_test_set, int, mem, value)