atomic.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Atomic operations. sparc32 version.
  2. Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifndef _BITS_ATOMIC_H
  18. #define _BITS_ATOMIC_H 1
  19. #include <stdint.h>
  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. /* We have no compare and swap, just test and set.
  41. The following implementation contends on 64 global locks
  42. per library and assumes no variable will be accessed using atomic.h
  43. macros from two different libraries. */
  44. __make_section_unallocated
  45. (".gnu.linkonce.b.__sparc32_atomic_locks, \"aw\", %nobits");
  46. volatile unsigned char __sparc32_atomic_locks[64]
  47. __attribute__ ((nocommon, section (".gnu.linkonce.b.__sparc32_atomic_locks"
  48. __sec_comment),
  49. visibility ("hidden")));
  50. #define __sparc32_atomic_do_lock(addr) \
  51. do \
  52. { \
  53. unsigned int __old_lock; \
  54. unsigned int __idx = (((long) addr >> 2) ^ ((long) addr >> 12)) \
  55. & 63; \
  56. do \
  57. __asm__ __volatile__ ("ldstub %1, %0" \
  58. : "=r" (__old_lock), \
  59. "=m" (__sparc32_atomic_locks[__idx]) \
  60. : "m" (__sparc32_atomic_locks[__idx]) \
  61. : "memory"); \
  62. while (__old_lock); \
  63. } \
  64. while (0)
  65. #define __sparc32_atomic_do_unlock(addr) \
  66. do \
  67. { \
  68. __sparc32_atomic_locks[(((long) addr >> 2) \
  69. ^ ((long) addr >> 12)) & 63] = 0; \
  70. __asm__ __volatile__ ("" ::: "memory"); \
  71. } \
  72. while (0)
  73. #define __sparc32_atomic_do_lock24(addr) \
  74. do \
  75. { \
  76. unsigned int __old_lock; \
  77. do \
  78. __asm__ __volatile__ ("ldstub %1, %0" \
  79. : "=r" (__old_lock), "=m" (*(addr)) \
  80. : "m" (*(addr)) \
  81. : "memory"); \
  82. while (__old_lock); \
  83. } \
  84. while (0)
  85. #define __sparc32_atomic_do_unlock24(addr) \
  86. do \
  87. { \
  88. *(char *) (addr) = 0; \
  89. __asm__ __volatile__ ("" ::: "memory"); \
  90. } \
  91. while (0)
  92. #ifndef SHARED
  93. # define __v9_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  94. ({ \
  95. register __typeof (*(mem)) __acev_tmp __asm__ ("%g6"); \
  96. register __typeof (mem) __acev_mem __asm__ ("%g1") = (mem); \
  97. register __typeof (*(mem)) __acev_oldval __asm__ ("%g5"); \
  98. __acev_tmp = (newval); \
  99. __acev_oldval = (oldval); \
  100. /* .word 0xcde05005 is cas [%g1], %g5, %g6. Can't use cas here though, \
  101. because as will then mark the object file as V8+ arch. */ \
  102. __asm__ __volatile__ (".word 0xcde05005" \
  103. : "+r" (__acev_tmp), "=m" (*__acev_mem) \
  104. : "r" (__acev_oldval), "m" (*__acev_mem), \
  105. "r" (__acev_mem) : "memory"); \
  106. __acev_tmp; })
  107. #endif
  108. /* The only basic operation needed is compare and exchange. */
  109. #define __v7_compare_and_exchange_val_acq(mem, newval, oldval) \
  110. ({ __typeof (mem) __acev_memp = (mem); \
  111. __typeof (*mem) __acev_ret; \
  112. __typeof (*mem) __acev_newval = (newval); \
  113. \
  114. __sparc32_atomic_do_lock (__acev_memp); \
  115. __acev_ret = *__acev_memp; \
  116. if (__acev_ret == (oldval)) \
  117. *__acev_memp = __acev_newval; \
  118. __sparc32_atomic_do_unlock (__acev_memp); \
  119. __acev_ret; })
  120. #define __v7_compare_and_exchange_bool_acq(mem, newval, oldval) \
  121. ({ __typeof (mem) __aceb_memp = (mem); \
  122. int __aceb_ret; \
  123. __typeof (*mem) __aceb_newval = (newval); \
  124. \
  125. __sparc32_atomic_do_lock (__aceb_memp); \
  126. __aceb_ret = 0; \
  127. if (*__aceb_memp == (oldval)) \
  128. *__aceb_memp = __aceb_newval; \
  129. else \
  130. __aceb_ret = 1; \
  131. __sparc32_atomic_do_unlock (__aceb_memp); \
  132. __aceb_ret; })
  133. #define __v7_exchange_acq(mem, newval) \
  134. ({ __typeof (mem) __acev_memp = (mem); \
  135. __typeof (*mem) __acev_ret; \
  136. __typeof (*mem) __acev_newval = (newval); \
  137. \
  138. __sparc32_atomic_do_lock (__acev_memp); \
  139. __acev_ret = *__acev_memp; \
  140. *__acev_memp = __acev_newval; \
  141. __sparc32_atomic_do_unlock (__acev_memp); \
  142. __acev_ret; })
  143. #define __v7_exchange_and_add(mem, value) \
  144. ({ __typeof (mem) __acev_memp = (mem); \
  145. __typeof (*mem) __acev_ret; \
  146. \
  147. __sparc32_atomic_do_lock (__acev_memp); \
  148. __acev_ret = *__acev_memp; \
  149. *__acev_memp = __acev_ret + (value); \
  150. __sparc32_atomic_do_unlock (__acev_memp); \
  151. __acev_ret; })
  152. /* Special versions, which guarantee that top 8 bits of all values
  153. are cleared and use those bits as the ldstub lock. */
  154. #define __v7_compare_and_exchange_val_24_acq(mem, newval, oldval) \
  155. ({ __typeof (mem) __acev_memp = (mem); \
  156. __typeof (*mem) __acev_ret; \
  157. __typeof (*mem) __acev_newval = (newval); \
  158. \
  159. __sparc32_atomic_do_lock24 (__acev_memp); \
  160. __acev_ret = *__acev_memp & 0xffffff; \
  161. if (__acev_ret == (oldval)) \
  162. *__acev_memp = __acev_newval; \
  163. else \
  164. __sparc32_atomic_do_unlock24 (__acev_memp); \
  165. __asm__ __volatile__ ("" ::: "memory"); \
  166. __acev_ret; })
  167. #define __v7_exchange_24_rel(mem, newval) \
  168. ({ __typeof (mem) __acev_memp = (mem); \
  169. __typeof (*mem) __acev_ret; \
  170. __typeof (*mem) __acev_newval = (newval); \
  171. \
  172. __sparc32_atomic_do_lock24 (__acev_memp); \
  173. __acev_ret = *__acev_memp & 0xffffff; \
  174. *__acev_memp = __acev_newval; \
  175. __asm__ __volatile__ ("" ::: "memory"); \
  176. __acev_ret; })
  177. #ifdef SHARED
  178. /* When dynamically linked, we assume pre-v9 libraries are only ever
  179. used on pre-v9 CPU. */
  180. # define __atomic_is_v9 0
  181. # define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
  182. __v7_compare_and_exchange_val_acq (mem, newval, oldval)
  183. # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
  184. __v7_compare_and_exchange_bool_acq (mem, newval, oldval)
  185. # define atomic_exchange_acq(mem, newval) \
  186. __v7_exchange_acq (mem, newval)
  187. # define atomic_exchange_and_add(mem, value) \
  188. __v7_exchange_and_add (mem, value)
  189. # define atomic_compare_and_exchange_val_24_acq(mem, newval, oldval) \
  190. ({ \
  191. if (sizeof (*mem) != 4) \
  192. abort (); \
  193. __v7_compare_and_exchange_val_24_acq (mem, newval, oldval); })
  194. # define atomic_exchange_24_rel(mem, newval) \
  195. ({ \
  196. if (sizeof (*mem) != 4) \
  197. abort (); \
  198. __v7_exchange_24_rel (mem, newval); })
  199. #else
  200. /*
  201. Here's what we'd like to do:
  202. In libc.a/libpthread.a etc. we don't know if we'll be run on
  203. pre-v9 or v9 CPU. To be interoperable with dynamically linked
  204. apps on v9 CPUs e.g. with process shared primitives, use cas insn
  205. on v9 CPUs and ldstub on pre-v9.
  206. However, we have no good way to test at run time that I know of,
  207. so resort to the lowest common denominator (v7 ops) -austinf
  208. */
  209. #define __atomic_is_v9 0
  210. # define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
  211. ({ \
  212. __typeof (*mem) __acev_wret; \
  213. if (sizeof (*mem) != 4) \
  214. abort (); \
  215. if (__atomic_is_v9) \
  216. __acev_wret \
  217. = __v9_compare_and_exchange_val_32_acq (mem, newval, oldval);\
  218. else \
  219. __acev_wret \
  220. = __v7_compare_and_exchange_val_acq (mem, newval, oldval); \
  221. __acev_wret; })
  222. # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
  223. ({ \
  224. int __acev_wret; \
  225. if (sizeof (*mem) != 4) \
  226. abort (); \
  227. if (__atomic_is_v9) \
  228. { \
  229. __typeof (oldval) __acev_woldval = (oldval); \
  230. __acev_wret \
  231. = __v9_compare_and_exchange_val_32_acq (mem, newval, \
  232. __acev_woldval) \
  233. != __acev_woldval; \
  234. } \
  235. else \
  236. __acev_wret \
  237. = __v7_compare_and_exchange_bool_acq (mem, newval, oldval); \
  238. __acev_wret; })
  239. # define atomic_exchange_rel(mem, newval) \
  240. ({ \
  241. __typeof (*mem) __acev_wret; \
  242. if (sizeof (*mem) != 4) \
  243. abort (); \
  244. if (__atomic_is_v9) \
  245. { \
  246. __typeof (mem) __acev_wmemp = (mem); \
  247. __typeof (*(mem)) __acev_wval = (newval); \
  248. do \
  249. __acev_wret = *__acev_wmemp; \
  250. while (__builtin_expect \
  251. (__v9_compare_and_exchange_val_32_acq (__acev_wmemp,\
  252. __acev_wval, \
  253. __acev_wret) \
  254. != __acev_wret, 0)); \
  255. } \
  256. else \
  257. __acev_wret = __v7_exchange_acq (mem, newval); \
  258. __acev_wret; })
  259. # define atomic_compare_and_exchange_val_24_acq(mem, newval, oldval) \
  260. ({ \
  261. __typeof (*mem) __acev_wret; \
  262. if (sizeof (*mem) != 4) \
  263. abort (); \
  264. if (__atomic_is_v9) \
  265. __acev_wret \
  266. = __v9_compare_and_exchange_val_32_acq (mem, newval, oldval);\
  267. else \
  268. __acev_wret \
  269. = __v7_compare_and_exchange_val_24_acq (mem, newval, oldval);\
  270. __acev_wret; })
  271. # define atomic_exchange_24_rel(mem, newval) \
  272. ({ \
  273. __typeof (*mem) __acev_w24ret; \
  274. if (sizeof (*mem) != 4) \
  275. abort (); \
  276. if (__atomic_is_v9) \
  277. __acev_w24ret = atomic_exchange_rel (mem, newval); \
  278. else \
  279. __acev_w24ret = __v7_exchange_24_rel (mem, newval); \
  280. __acev_w24ret; })
  281. #endif
  282. #endif /* bits/atomic.h */