atomic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <stdint.h>
  15. typedef int8_t atomic8_t;
  16. typedef uint8_t uatomic8_t;
  17. typedef int_fast8_t atomic_fast8_t;
  18. typedef uint_fast8_t uatomic_fast8_t;
  19. typedef int16_t atomic16_t;
  20. typedef uint16_t uatomic16_t;
  21. typedef int_fast16_t atomic_fast16_t;
  22. typedef uint_fast16_t uatomic_fast16_t;
  23. typedef int32_t atomic32_t;
  24. typedef uint32_t uatomic32_t;
  25. typedef int_fast32_t atomic_fast32_t;
  26. typedef uint_fast32_t uatomic_fast32_t;
  27. typedef int64_t atomic64_t;
  28. typedef uint64_t uatomic64_t;
  29. typedef int_fast64_t atomic_fast64_t;
  30. typedef uint_fast64_t uatomic_fast64_t;
  31. typedef intptr_t atomicptr_t;
  32. typedef uintptr_t uatomicptr_t;
  33. typedef intmax_t atomic_max_t;
  34. typedef uintmax_t uatomic_max_t;
  35. #ifdef UP
  36. # define __MB /* nothing */
  37. #else
  38. # define __MB " mb\n"
  39. #endif
  40. /* Compare and exchange. For all of the "xxx" routines, we expect a
  41. "__prev" and a "__cmp" variable to be provided by the enclosing scope,
  42. in which values are returned. */
  43. #define __arch_compare_and_exchange_xxx_8_int(mem, new, old, mb1, mb2) \
  44. ({ \
  45. unsigned long __tmp, __snew, __addr64; \
  46. __asm__ __volatile__ ( \
  47. mb1 \
  48. " andnot %[__addr8],7,%[__addr64]\n" \
  49. " insbl %[__new],%[__addr8],%[__snew]\n" \
  50. "1: ldq_l %[__tmp],0(%[__addr64])\n" \
  51. " extbl %[__tmp],%[__addr8],%[__prev]\n" \
  52. " cmpeq %[__prev],%[__old],%[__cmp]\n" \
  53. " beq %[__cmp],2f\n" \
  54. " mskbl %[__tmp],%[__addr8],%[__tmp]\n" \
  55. " or %[__snew],%[__tmp],%[__tmp]\n" \
  56. " stq_c %[__tmp],0(%[__addr64])\n" \
  57. " beq %[__tmp],1b\n" \
  58. mb2 \
  59. "2:" \
  60. : [__prev] "=&r" (__prev), \
  61. [__snew] "=&r" (__snew), \
  62. [__tmp] "=&r" (__tmp), \
  63. [__cmp] "=&r" (__cmp), \
  64. [__addr64] "=&r" (__addr64) \
  65. : [__addr8] "r" (mem), \
  66. [__old] "Ir" ((uint64_t)(uint8_t)(uint64_t)(old)), \
  67. [__new] "r" (new) \
  68. : "memory"); \
  69. })
  70. #define __arch_compare_and_exchange_xxx_16_int(mem, new, old, mb1, mb2) \
  71. ({ \
  72. unsigned long __tmp, __snew, __addr64; \
  73. __asm__ __volatile__ ( \
  74. mb1 \
  75. " andnot %[__addr16],7,%[__addr64]\n" \
  76. " inswl %[__new],%[__addr16],%[__snew]\n" \
  77. "1: ldq_l %[__tmp],0(%[__addr64])\n" \
  78. " extwl %[__tmp],%[__addr16],%[__prev]\n" \
  79. " cmpeq %[__prev],%[__old],%[__cmp]\n" \
  80. " beq %[__cmp],2f\n" \
  81. " mskwl %[__tmp],%[__addr16],%[__tmp]\n" \
  82. " or %[__snew],%[__tmp],%[__tmp]\n" \
  83. " stq_c %[__tmp],0(%[__addr64])\n" \
  84. " beq %[__tmp],1b\n" \
  85. mb2 \
  86. "2:" \
  87. : [__prev] "=&r" (__prev), \
  88. [__snew] "=&r" (__snew), \
  89. [__tmp] "=&r" (__tmp), \
  90. [__cmp] "=&r" (__cmp), \
  91. [__addr64] "=&r" (__addr64) \
  92. : [__addr16] "r" (mem), \
  93. [__old] "Ir" ((uint64_t)(uint16_t)(uint64_t)(old)), \
  94. [__new] "r" (new) \
  95. : "memory"); \
  96. })
  97. #define __arch_compare_and_exchange_xxx_32_int(mem, new, old, mb1, mb2) \
  98. ({ \
  99. __asm__ __volatile__ ( \
  100. mb1 \
  101. "1: ldl_l %[__prev],%[__mem]\n" \
  102. " cmpeq %[__prev],%[__old],%[__cmp]\n" \
  103. " beq %[__cmp],2f\n" \
  104. " mov %[__new],%[__cmp]\n" \
  105. " stl_c %[__cmp],%[__mem]\n" \
  106. " beq %[__cmp],1b\n" \
  107. mb2 \
  108. "2:" \
  109. : [__prev] "=&r" (__prev), \
  110. [__cmp] "=&r" (__cmp) \
  111. : [__mem] "m" (*(mem)), \
  112. [__old] "Ir" ((uint64_t)(atomic32_t)(uint64_t)(old)), \
  113. [__new] "Ir" (new) \
  114. : "memory"); \
  115. })
  116. #define __arch_compare_and_exchange_xxx_64_int(mem, new, old, mb1, mb2) \
  117. ({ \
  118. __asm__ __volatile__ ( \
  119. mb1 \
  120. "1: ldq_l %[__prev],%[__mem]\n" \
  121. " cmpeq %[__prev],%[__old],%[__cmp]\n" \
  122. " beq %[__cmp],2f\n" \
  123. " mov %[__new],%[__cmp]\n" \
  124. " stq_c %[__cmp],%[__mem]\n" \
  125. " beq %[__cmp],1b\n" \
  126. mb2 \
  127. "2:" \
  128. : [__prev] "=&r" (__prev), \
  129. [__cmp] "=&r" (__cmp) \
  130. : [__mem] "m" (*(mem)), \
  131. [__old] "Ir" ((uint64_t)(old)), \
  132. [__new] "Ir" (new) \
  133. : "memory"); \
  134. })
  135. /* For all "bool" routines, we return FALSE if exchange succesful. */
  136. #define __arch_compare_and_exchange_bool_8_int(mem, new, old, mb1, mb2) \
  137. ({ unsigned long __prev; int __cmp; \
  138. __arch_compare_and_exchange_xxx_8_int(mem, new, old, mb1, mb2); \
  139. !__cmp; })
  140. #define __arch_compare_and_exchange_bool_16_int(mem, new, old, mb1, mb2) \
  141. ({ unsigned long __prev; int __cmp; \
  142. __arch_compare_and_exchange_xxx_16_int(mem, new, old, mb1, mb2); \
  143. !__cmp; })
  144. #define __arch_compare_and_exchange_bool_32_int(mem, new, old, mb1, mb2) \
  145. ({ unsigned long __prev; int __cmp; \
  146. __arch_compare_and_exchange_xxx_32_int(mem, new, old, mb1, mb2); \
  147. !__cmp; })
  148. #define __arch_compare_and_exchange_bool_64_int(mem, new, old, mb1, mb2) \
  149. ({ unsigned long __prev; int __cmp; \
  150. __arch_compare_and_exchange_xxx_64_int(mem, new, old, mb1, mb2); \
  151. !__cmp; })
  152. /* For all "val" routines, return the old value whether exchange
  153. successful or not. */
  154. #define __arch_compare_and_exchange_val_8_int(mem, new, old, mb1, mb2) \
  155. ({ unsigned long __prev; int __cmp; \
  156. __arch_compare_and_exchange_xxx_8_int(mem, new, old, mb1, mb2); \
  157. (__typeof (*mem))__prev; })
  158. #define __arch_compare_and_exchange_val_16_int(mem, new, old, mb1, mb2) \
  159. ({ unsigned long __prev; int __cmp; \
  160. __arch_compare_and_exchange_xxx_16_int(mem, new, old, mb1, mb2); \
  161. (__typeof (*mem))__prev; })
  162. #define __arch_compare_and_exchange_val_32_int(mem, new, old, mb1, mb2) \
  163. ({ unsigned long __prev; int __cmp; \
  164. __arch_compare_and_exchange_xxx_32_int(mem, new, old, mb1, mb2); \
  165. (__typeof (*mem))__prev; })
  166. #define __arch_compare_and_exchange_val_64_int(mem, new, old, mb1, mb2) \
  167. ({ unsigned long __prev; int __cmp; \
  168. __arch_compare_and_exchange_xxx_64_int(mem, new, old, mb1, mb2); \
  169. (__typeof (*mem))__prev; })
  170. /* Compare and exchange with "acquire" semantics, ie barrier after. */
  171. #define atomic_compare_and_exchange_bool_acq(mem, new, old) \
  172. __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
  173. mem, new, old, "", __MB)
  174. #define atomic_compare_and_exchange_val_acq(mem, new, old) \
  175. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  176. mem, new, old, "", __MB)
  177. /* Compare and exchange with "release" semantics, ie barrier before. */
  178. #define atomic_compare_and_exchange_bool_rel(mem, new, old) \
  179. __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
  180. mem, new, old, __MB, "")
  181. #define atomic_compare_and_exchange_val_rel(mem, new, old) \
  182. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  183. mem, new, old, __MB, "")
  184. /* Atomically store value and return the previous value. */
  185. #define __arch_exchange_8_int(mem, value, mb1, mb2) \
  186. ({ \
  187. unsigned long __ret, __tmp, __addr64, __sval; \
  188. __asm__ __volatile__ ( \
  189. mb1 \
  190. " andnot %[__addr8],7,%[__addr64]\n" \
  191. " insbl %[__value],%[__addr8],%[__sval]\n" \
  192. "1: ldq_l %[__tmp],0(%[__addr64])\n" \
  193. " extbl %[__tmp],%[__addr8],%[__ret]\n" \
  194. " mskbl %[__tmp],%[__addr8],%[__tmp]\n" \
  195. " or %[__sval],%[__tmp],%[__tmp]\n" \
  196. " stq_c %[__tmp],0(%[__addr64])\n" \
  197. " beq %[__tmp],1b\n" \
  198. mb2 \
  199. : [__ret] "=&r" (__ret), \
  200. [__sval] "=&r" (__sval), \
  201. [__tmp] "=&r" (__tmp), \
  202. [__addr64] "=&r" (__addr64) \
  203. : [__addr8] "r" (mem), \
  204. [__value] "r" (value) \
  205. : "memory"); \
  206. __ret; })
  207. #define __arch_exchange_16_int(mem, value, mb1, mb2) \
  208. ({ \
  209. unsigned long __ret, __tmp, __addr64, __sval; \
  210. __asm__ __volatile__ ( \
  211. mb1 \
  212. " andnot %[__addr16],7,%[__addr64]\n" \
  213. " inswl %[__value],%[__addr16],%[__sval]\n" \
  214. "1: ldq_l %[__tmp],0(%[__addr64])\n" \
  215. " extwl %[__tmp],%[__addr16],%[__ret]\n" \
  216. " mskwl %[__tmp],%[__addr16],%[__tmp]\n" \
  217. " or %[__sval],%[__tmp],%[__tmp]\n" \
  218. " stq_c %[__tmp],0(%[__addr64])\n" \
  219. " beq %[__tmp],1b\n" \
  220. mb2 \
  221. : [__ret] "=&r" (__ret), \
  222. [__sval] "=&r" (__sval), \
  223. [__tmp] "=&r" (__tmp), \
  224. [__addr64] "=&r" (__addr64) \
  225. : [__addr16] "r" (mem), \
  226. [__value] "r" (value) \
  227. : "memory"); \
  228. __ret; })
  229. #define __arch_exchange_32_int(mem, value, mb1, mb2) \
  230. ({ \
  231. signed int __ret, __tmp; \
  232. __asm__ __volatile__ ( \
  233. mb1 \
  234. "1: ldl_l %[__ret],%[__mem]\n" \
  235. " mov %[__val],%[__tmp]\n" \
  236. " stl_c %[__tmp],%[__mem]\n" \
  237. " beq %[__tmp],1b\n" \
  238. mb2 \
  239. : [__ret] "=&r" (__ret), \
  240. [__tmp] "=&r" (__tmp) \
  241. : [__mem] "m" (*(mem)), \
  242. [__val] "Ir" (value) \
  243. : "memory"); \
  244. __ret; })
  245. #define __arch_exchange_64_int(mem, value, mb1, mb2) \
  246. ({ \
  247. unsigned long __ret, __tmp; \
  248. __asm__ __volatile__ ( \
  249. mb1 \
  250. "1: ldq_l %[__ret],%[__mem]\n" \
  251. " mov %[__val],%[__tmp]\n" \
  252. " stq_c %[__tmp],%[__mem]\n" \
  253. " beq %[__tmp],1b\n" \
  254. mb2 \
  255. : [__ret] "=&r" (__ret), \
  256. [__tmp] "=&r" (__tmp) \
  257. : [__mem] "m" (*(mem)), \
  258. [__val] "Ir" (value) \
  259. : "memory"); \
  260. __ret; })
  261. #define atomic_exchange_acq(mem, value) \
  262. __atomic_val_bysize (__arch_exchange, int, mem, value, "", __MB)
  263. #define atomic_exchange_rel(mem, value) \
  264. __atomic_val_bysize (__arch_exchange, int, mem, value, __MB, "")
  265. /* Atomically add value and return the previous (unincremented) value. */
  266. #define __arch_exchange_and_add_8_int(mem, value, mb1, mb2) \
  267. ({ __builtin_trap (); 0; })
  268. #define __arch_exchange_and_add_16_int(mem, value, mb1, mb2) \
  269. ({ __builtin_trap (); 0; })
  270. #define __arch_exchange_and_add_32_int(mem, value, mb1, mb2) \
  271. ({ \
  272. signed int __ret, __tmp; \
  273. __asm__ __volatile__ ( \
  274. mb1 \
  275. "1: ldl_l %[__ret],%[__mem]\n" \
  276. " addl %[__ret],%[__val],%[__tmp]\n" \
  277. " stl_c %[__tmp],%[__mem]\n" \
  278. " beq %[__tmp],1b\n" \
  279. mb2 \
  280. : [__ret] "=&r" (__ret), \
  281. [__tmp] "=&r" (__tmp) \
  282. : [__mem] "m" (*(mem)), \
  283. [__val] "Ir" ((signed int)(value)) \
  284. : "memory"); \
  285. __ret; })
  286. #define __arch_exchange_and_add_64_int(mem, value, mb1, mb2) \
  287. ({ \
  288. unsigned long __ret, __tmp; \
  289. __asm__ __volatile__ ( \
  290. mb1 \
  291. "1: ldq_l %[__ret],%[__mem]\n" \
  292. " addq %[__ret],%[__val],%[__tmp]\n" \
  293. " stq_c %[__tmp],%[__mem]\n" \
  294. " beq %[__tmp],1b\n" \
  295. mb2 \
  296. : [__ret] "=&r" (__ret), \
  297. [__tmp] "=&r" (__tmp) \
  298. : [__mem] "m" (*(mem)), \
  299. [__val] "Ir" ((unsigned long)(value)) \
  300. : "memory"); \
  301. __ret; })
  302. /* ??? Barrier semantics for atomic_exchange_and_add appear to be
  303. undefined. Use full barrier for now, as that's safe. */
  304. #define atomic_exchange_and_add(mem, value) \
  305. __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, __MB, __MB)
  306. /* ??? Blah, I'm lazy. Implement these later. Can do better than the
  307. compare-and-exchange loop provided by generic code.
  308. #define atomic_decrement_if_positive(mem)
  309. #define atomic_bit_test_set(mem, bit)
  310. */
  311. #ifndef UP
  312. # define atomic_full_barrier() __asm__ ("mb" : : : "memory");
  313. # define atomic_read_barrier() __asm__ ("mb" : : : "memory");
  314. # define atomic_write_barrier() __asm__ ("wmb" : : : "memory");
  315. #endif