gatomic.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright © 2011 Ryan Lortie
  3. *
  4. * This 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. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * 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. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Author: Ryan Lortie <desrt@desrt.ca>
  18. */
  19. #ifndef __G_ATOMIC_H__
  20. #define __G_ATOMIC_H__
  21. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  22. #error "Only <glib.h> can be included directly."
  23. #endif
  24. #include <glib/gtypes.h>
  25. G_BEGIN_DECLS
  26. GLIB_AVAILABLE_IN_ALL
  27. gint g_atomic_int_get (const volatile gint *atomic);
  28. GLIB_AVAILABLE_IN_ALL
  29. void g_atomic_int_set (volatile gint *atomic,
  30. gint newval);
  31. GLIB_AVAILABLE_IN_ALL
  32. void g_atomic_int_inc (volatile gint *atomic);
  33. GLIB_AVAILABLE_IN_ALL
  34. gboolean g_atomic_int_dec_and_test (volatile gint *atomic);
  35. GLIB_AVAILABLE_IN_ALL
  36. gboolean g_atomic_int_compare_and_exchange (volatile gint *atomic,
  37. gint oldval,
  38. gint newval);
  39. GLIB_AVAILABLE_IN_ALL
  40. gint g_atomic_int_add (volatile gint *atomic,
  41. gint val);
  42. GLIB_AVAILABLE_IN_2_30
  43. guint g_atomic_int_and (volatile guint *atomic,
  44. guint val);
  45. GLIB_AVAILABLE_IN_2_30
  46. guint g_atomic_int_or (volatile guint *atomic,
  47. guint val);
  48. GLIB_AVAILABLE_IN_ALL
  49. guint g_atomic_int_xor (volatile guint *atomic,
  50. guint val);
  51. GLIB_AVAILABLE_IN_ALL
  52. gpointer g_atomic_pointer_get (const volatile void *atomic);
  53. GLIB_AVAILABLE_IN_ALL
  54. void g_atomic_pointer_set (volatile void *atomic,
  55. gpointer newval);
  56. GLIB_AVAILABLE_IN_ALL
  57. gboolean g_atomic_pointer_compare_and_exchange (volatile void *atomic,
  58. gpointer oldval,
  59. gpointer newval);
  60. GLIB_AVAILABLE_IN_ALL
  61. gssize g_atomic_pointer_add (volatile void *atomic,
  62. gssize val);
  63. GLIB_AVAILABLE_IN_2_30
  64. gsize g_atomic_pointer_and (volatile void *atomic,
  65. gsize val);
  66. GLIB_AVAILABLE_IN_2_30
  67. gsize g_atomic_pointer_or (volatile void *atomic,
  68. gsize val);
  69. GLIB_AVAILABLE_IN_ALL
  70. gsize g_atomic_pointer_xor (volatile void *atomic,
  71. gsize val);
  72. GLIB_DEPRECATED_IN_2_30_FOR(g_atomic_int_add)
  73. gint g_atomic_int_exchange_and_add (volatile gint *atomic,
  74. gint val);
  75. G_END_DECLS
  76. #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  77. /* We prefer the new C11-style atomic extension of GCC if available */
  78. #if defined(__ATOMIC_SEQ_CST) && !defined(__clang__)
  79. /* This assumes sizeof(int) is 4: gatomic.c statically
  80. * asserts that (using G_STATIC_ASSERT at top-level in a header was
  81. * problematic, see #730932) */
  82. #define g_atomic_int_get(atomic) \
  83. (G_GNUC_EXTENSION ({ \
  84. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  85. (void) (0 ? *(atomic) ^ *(atomic) : 1); \
  86. (gint) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST); \
  87. }))
  88. #define g_atomic_int_set(atomic, newval) \
  89. (G_GNUC_EXTENSION ({ \
  90. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  91. (void) (0 ? *(atomic) ^ (newval) : 1); \
  92. __atomic_store_4 ((atomic), (newval), __ATOMIC_SEQ_CST); \
  93. }))
  94. #if GLIB_SIZEOF_VOID_P == 8
  95. #define g_atomic_pointer_get(atomic) \
  96. (G_GNUC_EXTENSION ({ \
  97. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  98. guint64 gapg_temp = __atomic_load_8 ((atomic), __ATOMIC_SEQ_CST); \
  99. (gpointer) gapg_temp; \
  100. }))
  101. #define g_atomic_pointer_set(atomic, newval) \
  102. (G_GNUC_EXTENSION ({ \
  103. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  104. (void) (0 ? (gpointer) *(atomic) : NULL); \
  105. __atomic_store_8 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST); \
  106. }))
  107. #else /* GLIB_SIZEOF_VOID_P == 8 */
  108. /* This assumes that if sizeof(void *) is not 8, then it is 4:
  109. * gatomic.c statically asserts that (using G_STATIC_ASSERT
  110. * at top-level in a header was problematic, see #730932) */
  111. #define g_atomic_pointer_get(atomic) \
  112. (G_GNUC_EXTENSION ({ \
  113. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  114. guint32 gapg_temp = __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST); \
  115. (gpointer) gapg_temp; \
  116. }))
  117. #define g_atomic_pointer_set(atomic, newval) \
  118. (G_GNUC_EXTENSION ({ \
  119. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  120. (void) (0 ? (gpointer) *(atomic) : NULL); \
  121. __atomic_store_4 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST); \
  122. }))
  123. #endif /* GLIB_SIZEOF_VOID_P == 8 */
  124. #else /* defined(__ATOMIC_SEQ_CST) */
  125. #define g_atomic_int_get(atomic) \
  126. (G_GNUC_EXTENSION ({ \
  127. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  128. (void) (0 ? *(atomic) ^ *(atomic) : 1); \
  129. __sync_synchronize (); \
  130. (gint) *(atomic); \
  131. }))
  132. #define g_atomic_int_set(atomic, newval) \
  133. (G_GNUC_EXTENSION ({ \
  134. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  135. (void) (0 ? *(atomic) ^ (newval) : 1); \
  136. *(atomic) = (newval); \
  137. __sync_synchronize (); \
  138. }))
  139. #define g_atomic_pointer_get(atomic) \
  140. (G_GNUC_EXTENSION ({ \
  141. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  142. __sync_synchronize (); \
  143. (gpointer) *(atomic); \
  144. }))
  145. #define g_atomic_pointer_set(atomic, newval) \
  146. (G_GNUC_EXTENSION ({ \
  147. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  148. (void) (0 ? (gpointer) *(atomic) : NULL); \
  149. *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
  150. __sync_synchronize (); \
  151. }))
  152. #endif /* !defined(__ATOMIC_SEQ_CST) */
  153. #define g_atomic_int_inc(atomic) \
  154. (G_GNUC_EXTENSION ({ \
  155. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  156. (void) (0 ? *(atomic) ^ *(atomic) : 1); \
  157. (void) __sync_fetch_and_add ((atomic), 1); \
  158. }))
  159. #define g_atomic_int_dec_and_test(atomic) \
  160. (G_GNUC_EXTENSION ({ \
  161. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  162. (void) (0 ? *(atomic) ^ *(atomic) : 1); \
  163. __sync_fetch_and_sub ((atomic), 1) == 1; \
  164. }))
  165. #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
  166. (G_GNUC_EXTENSION ({ \
  167. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  168. (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 1); \
  169. __sync_bool_compare_and_swap ((atomic), (oldval), (newval)) ? TRUE : FALSE; \
  170. }))
  171. #define g_atomic_int_add(atomic, val) \
  172. (G_GNUC_EXTENSION ({ \
  173. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  174. (void) (0 ? *(atomic) ^ (val) : 1); \
  175. (gint) __sync_fetch_and_add ((atomic), (val)); \
  176. }))
  177. #define g_atomic_int_and(atomic, val) \
  178. (G_GNUC_EXTENSION ({ \
  179. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  180. (void) (0 ? *(atomic) ^ (val) : 1); \
  181. (guint) __sync_fetch_and_and ((atomic), (val)); \
  182. }))
  183. #define g_atomic_int_or(atomic, val) \
  184. (G_GNUC_EXTENSION ({ \
  185. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  186. (void) (0 ? *(atomic) ^ (val) : 1); \
  187. (guint) __sync_fetch_and_or ((atomic), (val)); \
  188. }))
  189. #define g_atomic_int_xor(atomic, val) \
  190. (G_GNUC_EXTENSION ({ \
  191. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  192. (void) (0 ? *(atomic) ^ (val) : 1); \
  193. (guint) __sync_fetch_and_xor ((atomic), (val)); \
  194. }))
  195. #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
  196. (G_GNUC_EXTENSION ({ \
  197. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  198. (void) (0 ? (gpointer) *(atomic) : NULL); \
  199. __sync_bool_compare_and_swap ((atomic), (oldval), (newval)) ? TRUE : FALSE; \
  200. }))
  201. #define g_atomic_pointer_add(atomic, val) \
  202. (G_GNUC_EXTENSION ({ \
  203. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  204. (void) (0 ? (gpointer) *(atomic) : NULL); \
  205. (void) (0 ? (val) ^ (val) : 1); \
  206. (gssize) __sync_fetch_and_add ((atomic), (val)); \
  207. }))
  208. #define g_atomic_pointer_and(atomic, val) \
  209. (G_GNUC_EXTENSION ({ \
  210. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  211. (void) (0 ? (gpointer) *(atomic) : NULL); \
  212. (void) (0 ? (val) ^ (val) : 1); \
  213. (gsize) __sync_fetch_and_and ((atomic), (val)); \
  214. }))
  215. #define g_atomic_pointer_or(atomic, val) \
  216. (G_GNUC_EXTENSION ({ \
  217. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  218. (void) (0 ? (gpointer) *(atomic) : NULL); \
  219. (void) (0 ? (val) ^ (val) : 1); \
  220. (gsize) __sync_fetch_and_or ((atomic), (val)); \
  221. }))
  222. #define g_atomic_pointer_xor(atomic, val) \
  223. (G_GNUC_EXTENSION ({ \
  224. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  225. (void) (0 ? (gpointer) *(atomic) : NULL); \
  226. (void) (0 ? (val) ^ (val) : 1); \
  227. (gsize) __sync_fetch_and_xor ((atomic), (val)); \
  228. }))
  229. #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
  230. #define g_atomic_int_get(atomic) \
  231. (g_atomic_int_get ((gint *) (atomic)))
  232. #define g_atomic_int_set(atomic, newval) \
  233. (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
  234. #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
  235. (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
  236. #define g_atomic_int_add(atomic, val) \
  237. (g_atomic_int_add ((gint *) (atomic), (val)))
  238. #define g_atomic_int_and(atomic, val) \
  239. (g_atomic_int_and ((guint *) (atomic), (val)))
  240. #define g_atomic_int_or(atomic, val) \
  241. (g_atomic_int_or ((guint *) (atomic), (val)))
  242. #define g_atomic_int_xor(atomic, val) \
  243. (g_atomic_int_xor ((guint *) (atomic), (val)))
  244. #define g_atomic_int_inc(atomic) \
  245. (g_atomic_int_inc ((gint *) (atomic)))
  246. #define g_atomic_int_dec_and_test(atomic) \
  247. (g_atomic_int_dec_and_test ((gint *) (atomic)))
  248. #define g_atomic_pointer_get(atomic) \
  249. (g_atomic_pointer_get (atomic))
  250. #define g_atomic_pointer_set(atomic, newval) \
  251. (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
  252. #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
  253. (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
  254. #define g_atomic_pointer_add(atomic, val) \
  255. (g_atomic_pointer_add ((atomic), (gssize) (val)))
  256. #define g_atomic_pointer_and(atomic, val) \
  257. (g_atomic_pointer_and ((atomic), (gsize) (val)))
  258. #define g_atomic_pointer_or(atomic, val) \
  259. (g_atomic_pointer_or ((atomic), (gsize) (val)))
  260. #define g_atomic_pointer_xor(atomic, val) \
  261. (g_atomic_pointer_xor ((atomic), (gsize) (val)))
  262. #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
  263. #endif /* __G_ATOMIC_H__ */