atomic.h 14 KB

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