atomic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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. #if defined __CONFIG_GENERIC_I386__ || defined __CONFIG_I386__
  17. # warning this file is only good for 486 or better
  18. #endif
  19. typedef int8_t atomic8_t;
  20. typedef uint8_t uatomic8_t;
  21. typedef int_fast8_t atomic_fast8_t;
  22. typedef uint_fast8_t uatomic_fast8_t;
  23. typedef int16_t atomic16_t;
  24. typedef uint16_t uatomic16_t;
  25. typedef int_fast16_t atomic_fast16_t;
  26. typedef uint_fast16_t uatomic_fast16_t;
  27. typedef int32_t atomic32_t;
  28. typedef uint32_t uatomic32_t;
  29. typedef int_fast32_t atomic_fast32_t;
  30. typedef uint_fast32_t uatomic_fast32_t;
  31. typedef int64_t atomic64_t;
  32. typedef uint64_t uatomic64_t;
  33. typedef int_fast64_t atomic_fast64_t;
  34. typedef uint_fast64_t uatomic_fast64_t;
  35. typedef intptr_t atomicptr_t;
  36. typedef uintptr_t uatomicptr_t;
  37. typedef intmax_t atomic_max_t;
  38. typedef uintmax_t uatomic_max_t;
  39. #ifndef LOCK_PREFIX
  40. # ifdef UP
  41. # define LOCK_PREFIX /* nothing */
  42. # else
  43. # define LOCK_PREFIX "lock;"
  44. # endif
  45. #endif
  46. #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
  47. ({ __typeof (*mem) ret; \
  48. __asm__ __volatile__ (LOCK_PREFIX "cmpxchgb %b2, %1" \
  49. : "=a" (ret), "=m" (*mem) \
  50. : "q" (newval), "m" (*mem), "0" (oldval)); \
  51. ret; })
  52. #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
  53. ({ __typeof (*mem) ret; \
  54. __asm__ __volatile__ (LOCK_PREFIX "cmpxchgw %w2, %1" \
  55. : "=a" (ret), "=m" (*mem) \
  56. : "r" (newval), "m" (*mem), "0" (oldval)); \
  57. ret; })
  58. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  59. ({ __typeof (*mem) ret; \
  60. __asm__ __volatile__ (LOCK_PREFIX "cmpxchgl %2, %1" \
  61. : "=a" (ret), "=m" (*mem) \
  62. : "r" (newval), "m" (*mem), "0" (oldval)); \
  63. ret; })
  64. /* XXX We do not really need 64-bit compare-and-exchange. At least
  65. not in the moment. Using it would mean causing portability
  66. problems since not many other 32-bit architectures have support for
  67. such an operation. So don't define any code for now. If it is
  68. really going to be used the code below can be used on Intel Pentium
  69. and later, but NOT on i486. */
  70. #if 1
  71. # define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  72. ({ __typeof (*mem) ret = *(mem); abort (); ret = (newval); ret = (oldval); })
  73. #else
  74. # ifdef __PIC__
  75. # define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  76. ({ __typeof (*mem) ret; \
  77. __asm__ __volatile__ ("xchgl %2, %%ebx\n\t" \
  78. LOCK_PREFIX "cmpxchg8b %1\n\t" \
  79. "xchgl %2, %%ebx" \
  80. : "=A" (ret), "=m" (*mem) \
  81. : "DS" (((unsigned long long int) (newval)) \
  82. & 0xffffffff), \
  83. "c" (((unsigned long long int) (newval)) >> 32), \
  84. "m" (*mem), "a" (((unsigned long long int) (oldval)) \
  85. & 0xffffffff), \
  86. "d" (((unsigned long long int) (oldval)) >> 32)); \
  87. ret; })
  88. # else
  89. # define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  90. ({ __typeof (*mem) ret; \
  91. __asm__ __volatile__ (LOCK_PREFIX "cmpxchg8b %1" \
  92. : "=A" (ret), "=m" (*mem) \
  93. : "b" (((unsigned long long int) (newval)) \
  94. & 0xffffffff), \
  95. "c" (((unsigned long long int) (newval)) >> 32), \
  96. "m" (*mem), "a" (((unsigned long long int) (oldval)) \
  97. & 0xffffffff), \
  98. "d" (((unsigned long long int) (oldval)) >> 32)); \
  99. ret; })
  100. # endif
  101. #endif
  102. /* Note that we need no lock prefix. */
  103. #define atomic_exchange_acq(mem, newvalue) \
  104. ({ __typeof (*mem) result; \
  105. if (sizeof (*mem) == 1) \
  106. __asm__ __volatile__ ("xchgb %b0, %1" \
  107. : "=r" (result), "=m" (*mem) \
  108. : "0" (newvalue), "m" (*mem)); \
  109. else if (sizeof (*mem) == 2) \
  110. __asm__ __volatile__ ("xchgw %w0, %1" \
  111. : "=r" (result), "=m" (*mem) \
  112. : "0" (newvalue), "m" (*mem)); \
  113. else if (sizeof (*mem) == 4) \
  114. __asm__ __volatile__ ("xchgl %0, %1" \
  115. : "=r" (result), "=m" (*mem) \
  116. : "0" (newvalue), "m" (*mem)); \
  117. else \
  118. { \
  119. result = 0; \
  120. abort (); \
  121. } \
  122. result; })
  123. #define atomic_exchange_and_add(mem, value) \
  124. ({ __typeof (*mem) __result; \
  125. __typeof (value) __addval = (value); \
  126. if (sizeof (*mem) == 1) \
  127. __asm__ __volatile__ (LOCK_PREFIX "xaddb %b0, %1" \
  128. : "=r" (__result), "=m" (*mem) \
  129. : "0" (__addval), "m" (*mem)); \
  130. else if (sizeof (*mem) == 2) \
  131. __asm__ __volatile__ (LOCK_PREFIX "xaddw %w0, %1" \
  132. : "=r" (__result), "=m" (*mem) \
  133. : "0" (__addval), "m" (*mem)); \
  134. else if (sizeof (*mem) == 4) \
  135. __asm__ __volatile__ (LOCK_PREFIX "xaddl %0, %1" \
  136. : "=r" (__result), "=m" (*mem) \
  137. : "0" (__addval), "m" (*mem)); \
  138. else \
  139. { \
  140. __typeof (mem) __memp = (mem); \
  141. __typeof (*mem) __tmpval; \
  142. __result = *__memp; \
  143. do \
  144. __tmpval = __result; \
  145. while ((__result = __arch_compare_and_exchange_val_64_acq \
  146. (__memp, __result + __addval, __result)) == __tmpval); \
  147. } \
  148. __result; })
  149. #define atomic_add(mem, value) \
  150. (void) ({ if (__builtin_constant_p (value) && (value) == 1) \
  151. atomic_increment (mem); \
  152. else if (__builtin_constant_p (value) && (value) == -1) \
  153. atomic_decrement (mem); \
  154. else if (sizeof (*mem) == 1) \
  155. __asm__ __volatile__ (LOCK_PREFIX "addb %b1, %0" \
  156. : "=m" (*mem) \
  157. : "ir" (value), "m" (*mem)); \
  158. else if (sizeof (*mem) == 2) \
  159. __asm__ __volatile__ (LOCK_PREFIX "addw %w1, %0" \
  160. : "=m" (*mem) \
  161. : "ir" (value), "m" (*mem)); \
  162. else if (sizeof (*mem) == 4) \
  163. __asm__ __volatile__ (LOCK_PREFIX "addl %1, %0" \
  164. : "=m" (*mem) \
  165. : "ir" (value), "m" (*mem)); \
  166. else \
  167. { \
  168. __typeof (value) __addval = (value); \
  169. __typeof (mem) __memp = (mem); \
  170. __typeof (*mem) __oldval = *__memp; \
  171. __typeof (*mem) __tmpval; \
  172. do \
  173. __tmpval = __oldval; \
  174. while ((__oldval = __arch_compare_and_exchange_val_64_acq \
  175. (__memp, __oldval + __addval, __oldval)) == __tmpval); \
  176. } \
  177. })
  178. #define atomic_add_negative(mem, value) \
  179. ({ unsigned char __result; \
  180. if (sizeof (*mem) == 1) \
  181. __asm__ __volatile__ (LOCK_PREFIX "addb %b2, %0; sets %1" \
  182. : "=m" (*mem), "=qm" (__result) \
  183. : "iq" (value), "m" (*mem)); \
  184. else if (sizeof (*mem) == 2) \
  185. __asm__ __volatile__ (LOCK_PREFIX "addw %w2, %0; sets %1" \
  186. : "=m" (*mem), "=qm" (__result) \
  187. : "ir" (value), "m" (*mem)); \
  188. else if (sizeof (*mem) == 4) \
  189. __asm__ __volatile__ (LOCK_PREFIX "addl %2, %0; sets %1" \
  190. : "=m" (*mem), "=qm" (__result) \
  191. : "ir" (value), "m" (*mem)); \
  192. else \
  193. abort (); \
  194. __result; })
  195. #define atomic_add_zero(mem, value) \
  196. ({ unsigned char __result; \
  197. if (sizeof (*mem) == 1) \
  198. __asm__ __volatile__ (LOCK_PREFIX "addb %b2, %0; setz %1" \
  199. : "=m" (*mem), "=qm" (__result) \
  200. : "ir" (value), "m" (*mem)); \
  201. else if (sizeof (*mem) == 2) \
  202. __asm__ __volatile__ (LOCK_PREFIX "addw %w2, %0; setz %1" \
  203. : "=m" (*mem), "=qm" (__result) \
  204. : "ir" (value), "m" (*mem)); \
  205. else if (sizeof (*mem) == 4) \
  206. __asm__ __volatile__ (LOCK_PREFIX "addl %2, %0; setz %1" \
  207. : "=m" (*mem), "=qm" (__result) \
  208. : "ir" (value), "m" (*mem)); \
  209. else \
  210. abort (); \
  211. __result; })
  212. #define atomic_increment(mem) \
  213. (void) ({ if (sizeof (*mem) == 1) \
  214. __asm__ __volatile__ (LOCK_PREFIX "incb %b0" \
  215. : "=m" (*mem) \
  216. : "m" (*mem)); \
  217. else if (sizeof (*mem) == 2) \
  218. __asm__ __volatile__ (LOCK_PREFIX "incw %w0" \
  219. : "=m" (*mem) \
  220. : "m" (*mem)); \
  221. else if (sizeof (*mem) == 4) \
  222. __asm__ __volatile__ (LOCK_PREFIX "incl %0" \
  223. : "=m" (*mem) \
  224. : "m" (*mem)); \
  225. else \
  226. { \
  227. __typeof (mem) __memp = (mem); \
  228. __typeof (*mem) __oldval = *__memp; \
  229. __typeof (*mem) __tmpval; \
  230. do \
  231. __tmpval = __oldval; \
  232. while ((__oldval = __arch_compare_and_exchange_val_64_acq \
  233. (__memp, __oldval + 1, __oldval)) == __tmpval); \
  234. } \
  235. })
  236. #define atomic_increment_and_test(mem) \
  237. ({ unsigned char __result; \
  238. if (sizeof (*mem) == 1) \
  239. __asm__ __volatile__ (LOCK_PREFIX "incb %0; sete %b1" \
  240. : "=m" (*mem), "=qm" (__result) \
  241. : "m" (*mem)); \
  242. else if (sizeof (*mem) == 2) \
  243. __asm__ __volatile__ (LOCK_PREFIX "incw %0; sete %w1" \
  244. : "=m" (*mem), "=qm" (__result) \
  245. : "m" (*mem)); \
  246. else if (sizeof (*mem) == 4) \
  247. __asm__ __volatile__ (LOCK_PREFIX "incl %0; sete %1" \
  248. : "=m" (*mem), "=qm" (__result) \
  249. : "m" (*mem)); \
  250. else \
  251. abort (); \
  252. __result; })
  253. #define atomic_decrement(mem) \
  254. (void) ({ if (sizeof (*mem) == 1) \
  255. __asm__ __volatile__ (LOCK_PREFIX "decb %b0" \
  256. : "=m" (*mem) \
  257. : "m" (*mem)); \
  258. else if (sizeof (*mem) == 2) \
  259. __asm__ __volatile__ (LOCK_PREFIX "decw %w0" \
  260. : "=m" (*mem) \
  261. : "m" (*mem)); \
  262. else if (sizeof (*mem) == 4) \
  263. __asm__ __volatile__ (LOCK_PREFIX "decl %0" \
  264. : "=m" (*mem) \
  265. : "m" (*mem)); \
  266. else \
  267. { \
  268. __typeof (mem) __memp = (mem); \
  269. __typeof (*mem) __oldval = *__memp; \
  270. __typeof (*mem) __tmpval; \
  271. do \
  272. __tmpval = __oldval; \
  273. while ((__oldval = __arch_compare_and_exchange_val_64_acq \
  274. (__memp, __oldval - 1, __oldval)) == __tmpval); \
  275. } \
  276. })
  277. #define atomic_decrement_and_test(mem) \
  278. ({ unsigned char __result; \
  279. if (sizeof (*mem) == 1) \
  280. __asm__ __volatile__ (LOCK_PREFIX "decb %b0; sete %1" \
  281. : "=m" (*mem), "=qm" (__result) \
  282. : "m" (*mem)); \
  283. else if (sizeof (*mem) == 2) \
  284. __asm__ __volatile__ (LOCK_PREFIX "decw %w0; sete %1" \
  285. : "=m" (*mem), "=qm" (__result) \
  286. : "m" (*mem)); \
  287. else if (sizeof (*mem) == 4) \
  288. __asm__ __volatile__ (LOCK_PREFIX "decl %0; sete %1" \
  289. : "=m" (*mem), "=qm" (__result) \
  290. : "m" (*mem)); \
  291. else \
  292. abort (); \
  293. __result; })
  294. #define atomic_bit_set(mem, bit) \
  295. (void) ({ if (sizeof (*mem) == 1) \
  296. __asm__ __volatile__ (LOCK_PREFIX "orb %b2, %0" \
  297. : "=m" (*mem) \
  298. : "m" (*mem), "ir" (1 << (bit))); \
  299. else if (sizeof (*mem) == 2) \
  300. __asm__ __volatile__ (LOCK_PREFIX "orw %w2, %0" \
  301. : "=m" (*mem) \
  302. : "m" (*mem), "ir" (1 << (bit))); \
  303. else if (sizeof (*mem) == 4) \
  304. __asm__ __volatile__ (LOCK_PREFIX "orl %2, %0" \
  305. : "=m" (*mem) \
  306. : "m" (*mem), "ir" (1 << (bit))); \
  307. else \
  308. abort (); \
  309. })
  310. #define atomic_bit_test_set(mem, bit) \
  311. ({ unsigned char __result; \
  312. if (sizeof (*mem) == 1) \
  313. __asm__ __volatile__ (LOCK_PREFIX "btsb %3, %1; setc %0" \
  314. : "=q" (__result), "=m" (*mem) \
  315. : "m" (*mem), "ir" (bit)); \
  316. else if (sizeof (*mem) == 2) \
  317. __asm__ __volatile__ (LOCK_PREFIX "btsw %3, %1; setc %0" \
  318. : "=q" (__result), "=m" (*mem) \
  319. : "m" (*mem), "ir" (bit)); \
  320. else if (sizeof (*mem) == 4) \
  321. __asm__ __volatile__ (LOCK_PREFIX "btsl %3, %1; setc %0" \
  322. : "=q" (__result), "=m" (*mem) \
  323. : "m" (*mem), "ir" (bit)); \
  324. else \
  325. abort (); \
  326. __result; })
  327. #define atomic_delay() __asm__ ("rep; nop")