atomic.h 9.5 KB

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