atomic.h 11 KB

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