atomic.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /* Avoid having lots of different versions of compare and exchange,
  56. by having this one complicated version. Parameters:
  57. bwl: b, w or l for 8, 16 and 32 bit versions.
  58. version: val or bool, depending on whether the result is the
  59. previous value or a bool indicating whether the transfer
  60. did happen (note this needs inverting before being
  61. returned in atomic_compare_and_exchange_bool).
  62. */
  63. #define __arch_compare_and_exchange_n(mem, newval, oldval, bwl, version) \
  64. ({ signed long __arch_result; \
  65. __asm__ __volatile__ ("\
  66. .align 2\n\
  67. mova 1f,r0\n\
  68. nop\n\
  69. mov r15,r1\n\
  70. mov #-8,r15\n\
  71. 0: mov." #bwl " @%1,%0\n\
  72. cmp/eq %0,%3\n\
  73. bf 1f\n\
  74. mov." #bwl " %2,@%1\n\
  75. 1: mov r1,r15\n\
  76. .ifeqs \"bool\",\"" #version "\"\n\
  77. movt %0\n\
  78. .endif\n" \
  79. : "=&r" (__arch_result) \
  80. : "r" (mem), "r" (newval), "r" (oldval) \
  81. : "r0", "r1", "t", "memory"); \
  82. __arch_result; })
  83. #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
  84. __arch_compare_and_exchange_n(mem, newval, (int8_t)(oldval), b, val)
  85. #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
  86. __arch_compare_and_exchange_n(mem, newval, (int16_t)(oldval), w, val)
  87. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  88. __arch_compare_and_exchange_n(mem, newval, (int32_t)(oldval), l, val)
  89. /* XXX We do not really need 64-bit compare-and-exchange. At least
  90. not in the moment. Using it would mean causing portability
  91. problems since not many other 32-bit architectures have support for
  92. such an operation. So don't define any code for now. */
  93. # define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  94. (abort (), 0)
  95. /* For "bool" routines, return if the exchange did NOT occur */
  96. #define __arch_compare_and_exchange_bool_8_acq(mem, newval, oldval) \
  97. (! __arch_compare_and_exchange_n(mem, newval, (int8_t)(oldval), b, bool))
  98. #define __arch_compare_and_exchange_bool_16_acq(mem, newval, oldval) \
  99. (! __arch_compare_and_exchange_n(mem, newval, (int16_t)(oldval), w, bool))
  100. #define __arch_compare_and_exchange_bool_32_acq(mem, newval, oldval) \
  101. (! __arch_compare_and_exchange_n(mem, newval, (int32_t)(oldval), l, bool))
  102. # define __arch_compare_and_exchange_bool_64_acq(mem, newval, oldval) \
  103. (abort (), 0)
  104. /* Similar to the above, have one template which can be used in a
  105. number of places. This version returns both the old and the new
  106. values of the location. Parameters:
  107. bwl: b, w or l for 8, 16 and 32 bit versions.
  108. oper: The instruction to perform on the old value.
  109. Note old is not sign extended, so should be an unsigned long.
  110. */
  111. #define __arch_operate_old_new_n(mem, value, old, new, bwl, oper) \
  112. (void) ({ __asm__ __volatile__ ("\
  113. .align 2\n\
  114. mova 1f,r0\n\
  115. mov r15,r1\n\
  116. nop\n\
  117. mov #-8,r15\n\
  118. 0: mov." #bwl " @%2,%0\n\
  119. mov %0,%1\n\
  120. " #oper " %3,%1\n\
  121. mov." #bwl " %1,@%2\n\
  122. 1: mov r1,r15" \
  123. : "=&r" (old), "=&r"(new) \
  124. : "r" (mem), "r" (value) \
  125. : "r0", "r1", "memory"); \
  126. })
  127. #define __arch_exchange_and_add_8_int(mem, value) \
  128. ({ int32_t __value = (value), __new, __old; \
  129. __arch_operate_old_new_n((mem), __value, __old, __new, b, add); \
  130. __old; })
  131. #define __arch_exchange_and_add_16_int(mem, value) \
  132. ({ int32_t __value = (value), __new, __old; \
  133. __arch_operate_old_new_n((mem), __value, __old, __new, w, add); \
  134. __old; })
  135. #define __arch_exchange_and_add_32_int(mem, value) \
  136. ({ int32_t __value = (value), __new, __old; \
  137. __arch_operate_old_new_n((mem), __value, __old, __new, l, add); \
  138. __old; })
  139. #define __arch_exchange_and_add_64_int(mem, value) \
  140. (abort (), 0)
  141. #define atomic_exchange_and_add(mem, value) \
  142. __atomic_val_bysize (__arch_exchange_and_add, int, mem, value)
  143. /* Again, another template. We get a slight optimisation when the old value
  144. does not need to be returned. Parameters:
  145. bwl: b, w or l for 8, 16 and 32 bit versions.
  146. oper: The instruction to perform on the old value.
  147. */
  148. #define __arch_operate_new_n(mem, value, bwl, oper) \
  149. ({ int32_t __value = (value), __new; \
  150. __asm__ __volatile__ ("\
  151. .align 2\n\
  152. mova 1f,r0\n\
  153. mov r15,r1\n\
  154. mov #-6,r15\n\
  155. 0: mov." #bwl " @%1,%0\n\
  156. " #oper " %2,%0\n\
  157. mov." #bwl " %0,@%1\n\
  158. 1: mov r1,r15" \
  159. : "=&r" (__new) \
  160. : "r" (mem), "r" (__value) \
  161. : "r0", "r1", "memory"); \
  162. __new; \
  163. })
  164. #define __arch_add_8_int(mem, value) \
  165. __arch_operate_new_n(mem, value, b, add)
  166. #define __arch_add_16_int(mem, value) \
  167. __arch_operate_new_n(mem, value, w, add)
  168. #define __arch_add_32_int(mem, value) \
  169. __arch_operate_new_n(mem, value, l, add)
  170. #define __arch_add_64_int(mem, value) \
  171. (abort (), 0)
  172. #define atomic_add(mem, value) \
  173. ((void) __atomic_val_bysize (__arch_add, int, mem, value))
  174. #define __arch_add_negative_8_int(mem, value) \
  175. (__arch_operate_new_n(mem, value, b, add) < 0)
  176. #define __arch_add_negative_16_int(mem, value) \
  177. (__arch_operate_new_n(mem, value, w, add) < 0)
  178. #define __arch_add_negative_32_int(mem, value) \
  179. (__arch_operate_new_n(mem, value, l, add) < 0)
  180. #define __arch_add_negative_64_int(mem, value) \
  181. (abort (), 0)
  182. #define atomic_add_negative(mem, value) \
  183. __atomic_bool_bysize (__arch_add_negative, int, mem, value)
  184. #define __arch_add_zero_8_int(mem, value) \
  185. (__arch_operate_new_n(mem, value, b, add) == 0)
  186. #define __arch_add_zero_16_int(mem, value) \
  187. (__arch_operate_new_n(mem, value, w, add) == 0)
  188. #define __arch_add_zero_32_int(mem, value) \
  189. (__arch_operate_new_n(mem, value, l, add) == 0)
  190. #define __arch_add_zero_64_int(mem, value) \
  191. (abort (), 0)
  192. #define atomic_add_zero(mem, value) \
  193. __atomic_bool_bysize (__arch_add_zero, int, mem, value)
  194. #define atomic_increment_and_test(mem) atomic_add_zero((mem), 1)
  195. #define atomic_decrement_and_test(mem) atomic_add_zero((mem), -1)
  196. #define __arch_bit_set_8_int(mem, value) \
  197. __arch_operate_new_n(mem, 1<<(value), b, or)
  198. #define __arch_bit_set_16_int(mem, value) \
  199. __arch_operate_new_n(mem, 1<<(value), w, or)
  200. #define __arch_bit_set_32_int(mem, value) \
  201. __arch_operate_new_n(mem, 1<<(value), l, or)
  202. #define __arch_bit_set_64_int(mem, value) \
  203. (abort (), 0)
  204. #define __arch_add_64_int(mem, value) \
  205. (abort (), 0)
  206. #define atomic_bit_set(mem, value) \
  207. ((void) __atomic_val_bysize (__arch_bit_set, int, mem, value))
  208. #define __arch_bit_test_set_8_int(mem, value) \
  209. ({ int32_t __value = 1<<(value), __new, __old; \
  210. __arch_operate_old_new_n((mem), __value, __old, __new, b, or); \
  211. __old & __value; })
  212. #define __arch_bit_test_set_16_int(mem, value) \
  213. ({ int32_t __value = 1<<(value), __new, __old; \
  214. __arch_operate_old_new_n((mem), __value, __old, __new, w, or); \
  215. __old & __value; })
  216. #define __arch_bit_test_set_32_int(mem, value) \
  217. ({ int32_t __value = 1<<(value), __new, __old; \
  218. __arch_operate_old_new_n((mem), __value, __old, __new, l, or); \
  219. __old & __value; })
  220. #define __arch_bit_test_set_64_int(mem, value) \
  221. (abort (), 0)
  222. #define atomic_bit_test_set(mem, value) \
  223. __atomic_val_bysize (__arch_bit_test_set, int, mem, value)