gatomic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright © 2011 Ryan Lortie
  3. *
  4. * This library is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * licence, 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. #define g_atomic_int_get(atomic) \
  80. (G_GNUC_EXTENSION ({ \
  81. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  82. (void) (0 ? *(atomic) ^ *(atomic) : 0); \
  83. (gint) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST); \
  84. }))
  85. #define g_atomic_int_set(atomic, newval) \
  86. (G_GNUC_EXTENSION ({ \
  87. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  88. (void) (0 ? *(atomic) ^ (newval) : 0); \
  89. __atomic_store_4 ((atomic), (newval), __ATOMIC_SEQ_CST); \
  90. }))
  91. #if GLIB_SIZEOF_VOID_P == 8
  92. #define g_atomic_pointer_get(atomic) \
  93. (G_GNUC_EXTENSION ({ \
  94. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  95. (gpointer) __atomic_load_8 ((atomic), __ATOMIC_SEQ_CST); \
  96. }))
  97. #define g_atomic_pointer_set(atomic, newval) \
  98. (G_GNUC_EXTENSION ({ \
  99. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  100. (void) (0 ? (gpointer) *(atomic) : 0); \
  101. __atomic_store_8 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST); \
  102. }))
  103. #else /* GLIB_SIZEOF_VOID_P == 8 */
  104. #define g_atomic_pointer_get(atomic) \
  105. (G_GNUC_EXTENSION ({ \
  106. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  107. (gpointer) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST); \
  108. }))
  109. #define g_atomic_pointer_set(atomic, newval) \
  110. (G_GNUC_EXTENSION ({ \
  111. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  112. (void) (0 ? (gpointer) *(atomic) : 0); \
  113. __atomic_store_4 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST); \
  114. }))
  115. #endif /* GLIB_SIZEOF_VOID_P == 8 */
  116. #else /* defined(__ATOMIC_SEQ_CST) */
  117. #define g_atomic_int_get(atomic) \
  118. (G_GNUC_EXTENSION ({ \
  119. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  120. (void) (0 ? *(atomic) ^ *(atomic) : 0); \
  121. __sync_synchronize (); \
  122. (gint) *(atomic); \
  123. }))
  124. #define g_atomic_int_set(atomic, newval) \
  125. (G_GNUC_EXTENSION ({ \
  126. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  127. (void) (0 ? *(atomic) ^ (newval) : 0); \
  128. *(atomic) = (newval); \
  129. __sync_synchronize (); \
  130. }))
  131. #define g_atomic_pointer_get(atomic) \
  132. (G_GNUC_EXTENSION ({ \
  133. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  134. __sync_synchronize (); \
  135. (gpointer) *(atomic); \
  136. }))
  137. #define g_atomic_pointer_set(atomic, newval) \
  138. (G_GNUC_EXTENSION ({ \
  139. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  140. (void) (0 ? (gpointer) *(atomic) : 0); \
  141. *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
  142. __sync_synchronize (); \
  143. }))
  144. #endif /* !defined(__ATOMIC_SEQ_CST) */
  145. #define g_atomic_int_inc(atomic) \
  146. (G_GNUC_EXTENSION ({ \
  147. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  148. (void) (0 ? *(atomic) ^ *(atomic) : 0); \
  149. (void) __sync_fetch_and_add ((atomic), 1); \
  150. }))
  151. #define g_atomic_int_dec_and_test(atomic) \
  152. (G_GNUC_EXTENSION ({ \
  153. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  154. (void) (0 ? *(atomic) ^ *(atomic) : 0); \
  155. __sync_fetch_and_sub ((atomic), 1) == 1; \
  156. }))
  157. #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
  158. (G_GNUC_EXTENSION ({ \
  159. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  160. (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0); \
  161. (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
  162. }))
  163. #define g_atomic_int_add(atomic, val) \
  164. (G_GNUC_EXTENSION ({ \
  165. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  166. (void) (0 ? *(atomic) ^ (val) : 0); \
  167. (gint) __sync_fetch_and_add ((atomic), (val)); \
  168. }))
  169. #define g_atomic_int_and(atomic, val) \
  170. (G_GNUC_EXTENSION ({ \
  171. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  172. (void) (0 ? *(atomic) ^ (val) : 0); \
  173. (guint) __sync_fetch_and_and ((atomic), (val)); \
  174. }))
  175. #define g_atomic_int_or(atomic, val) \
  176. (G_GNUC_EXTENSION ({ \
  177. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  178. (void) (0 ? *(atomic) ^ (val) : 0); \
  179. (guint) __sync_fetch_and_or ((atomic), (val)); \
  180. }))
  181. #define g_atomic_int_xor(atomic, val) \
  182. (G_GNUC_EXTENSION ({ \
  183. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  184. (void) (0 ? *(atomic) ^ (val) : 0); \
  185. (guint) __sync_fetch_and_xor ((atomic), (val)); \
  186. }))
  187. #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
  188. (G_GNUC_EXTENSION ({ \
  189. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  190. (void) (0 ? (gpointer) *(atomic) : 0); \
  191. (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
  192. }))
  193. #define g_atomic_pointer_add(atomic, val) \
  194. (G_GNUC_EXTENSION ({ \
  195. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  196. (void) (0 ? (gpointer) *(atomic) : 0); \
  197. (void) (0 ? (val) ^ (val) : 0); \
  198. (gssize) __sync_fetch_and_add ((atomic), (val)); \
  199. }))
  200. #define g_atomic_pointer_and(atomic, val) \
  201. (G_GNUC_EXTENSION ({ \
  202. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  203. (void) (0 ? (gpointer) *(atomic) : 0); \
  204. (void) (0 ? (val) ^ (val) : 0); \
  205. (gsize) __sync_fetch_and_and ((atomic), (val)); \
  206. }))
  207. #define g_atomic_pointer_or(atomic, val) \
  208. (G_GNUC_EXTENSION ({ \
  209. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  210. (void) (0 ? (gpointer) *(atomic) : 0); \
  211. (void) (0 ? (val) ^ (val) : 0); \
  212. (gsize) __sync_fetch_and_or ((atomic), (val)); \
  213. }))
  214. #define g_atomic_pointer_xor(atomic, val) \
  215. (G_GNUC_EXTENSION ({ \
  216. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  217. (void) (0 ? (gpointer) *(atomic) : 0); \
  218. (void) (0 ? (val) ^ (val) : 0); \
  219. (gsize) __sync_fetch_and_xor ((atomic), (val)); \
  220. }))
  221. #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
  222. #define g_atomic_int_get(atomic) \
  223. (g_atomic_int_get ((gint *) (atomic)))
  224. #define g_atomic_int_set(atomic, newval) \
  225. (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
  226. #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
  227. (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
  228. #define g_atomic_int_add(atomic, val) \
  229. (g_atomic_int_add ((gint *) (atomic), (val)))
  230. #define g_atomic_int_and(atomic, val) \
  231. (g_atomic_int_and ((guint *) (atomic), (val)))
  232. #define g_atomic_int_or(atomic, val) \
  233. (g_atomic_int_or ((guint *) (atomic), (val)))
  234. #define g_atomic_int_xor(atomic, val) \
  235. (g_atomic_int_xor ((guint *) (atomic), (val)))
  236. #define g_atomic_int_inc(atomic) \
  237. (g_atomic_int_inc ((gint *) (atomic)))
  238. #define g_atomic_int_dec_and_test(atomic) \
  239. (g_atomic_int_dec_and_test ((gint *) (atomic)))
  240. #define g_atomic_pointer_get(atomic) \
  241. (g_atomic_pointer_get (atomic))
  242. #define g_atomic_pointer_set(atomic, newval) \
  243. (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
  244. #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
  245. (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
  246. #define g_atomic_pointer_add(atomic, val) \
  247. (g_atomic_pointer_add ((atomic), (gssize) (val)))
  248. #define g_atomic_pointer_and(atomic, val) \
  249. (g_atomic_pointer_and ((atomic), (gsize) (val)))
  250. #define g_atomic_pointer_or(atomic, val) \
  251. (g_atomic_pointer_or ((atomic), (gsize) (val)))
  252. #define g_atomic_pointer_xor(atomic, val) \
  253. (g_atomic_pointer_xor ((atomic), (gsize) (val)))
  254. #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
  255. #endif /* __G_ATOMIC_H__ */