gatomic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <[email protected]>
  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_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. #define g_atomic_int_get(atomic) \
  78. (G_GNUC_EXTENSION ({ \
  79. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  80. (void) (0 ? *(atomic) ^ *(atomic) : 0); \
  81. __sync_synchronize (); \
  82. (gint) *(atomic); \
  83. }))
  84. #define g_atomic_int_set(atomic, newval) \
  85. (G_GNUC_EXTENSION ({ \
  86. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  87. (void) (0 ? *(atomic) ^ (newval) : 0); \
  88. *(atomic) = (newval); \
  89. __sync_synchronize (); \
  90. }))
  91. #define g_atomic_int_inc(atomic) \
  92. (G_GNUC_EXTENSION ({ \
  93. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  94. (void) (0 ? *(atomic) ^ *(atomic) : 0); \
  95. (void) __sync_fetch_and_add ((atomic), 1); \
  96. }))
  97. #define g_atomic_int_dec_and_test(atomic) \
  98. (G_GNUC_EXTENSION ({ \
  99. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  100. (void) (0 ? *(atomic) ^ *(atomic) : 0); \
  101. __sync_fetch_and_sub ((atomic), 1) == 1; \
  102. }))
  103. #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
  104. (G_GNUC_EXTENSION ({ \
  105. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  106. (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0); \
  107. (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
  108. }))
  109. #define g_atomic_int_add(atomic, val) \
  110. (G_GNUC_EXTENSION ({ \
  111. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  112. (void) (0 ? *(atomic) ^ (val) : 0); \
  113. (gint) __sync_fetch_and_add ((atomic), (val)); \
  114. }))
  115. #define g_atomic_int_and(atomic, val) \
  116. (G_GNUC_EXTENSION ({ \
  117. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  118. (void) (0 ? *(atomic) ^ (val) : 0); \
  119. (guint) __sync_fetch_and_and ((atomic), (val)); \
  120. }))
  121. #define g_atomic_int_or(atomic, val) \
  122. (G_GNUC_EXTENSION ({ \
  123. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  124. (void) (0 ? *(atomic) ^ (val) : 0); \
  125. (guint) __sync_fetch_and_or ((atomic), (val)); \
  126. }))
  127. #define g_atomic_int_xor(atomic, val) \
  128. (G_GNUC_EXTENSION ({ \
  129. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
  130. (void) (0 ? *(atomic) ^ (val) : 0); \
  131. (guint) __sync_fetch_and_xor ((atomic), (val)); \
  132. }))
  133. #define g_atomic_pointer_get(atomic) \
  134. (G_GNUC_EXTENSION ({ \
  135. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  136. __sync_synchronize (); \
  137. (gpointer) *(atomic); \
  138. }))
  139. #define g_atomic_pointer_set(atomic, newval) \
  140. (G_GNUC_EXTENSION ({ \
  141. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  142. (void) (0 ? (gpointer) *(atomic) : 0); \
  143. *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
  144. __sync_synchronize (); \
  145. }))
  146. #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
  147. (G_GNUC_EXTENSION ({ \
  148. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  149. (void) (0 ? (gpointer) *(atomic) : 0); \
  150. (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
  151. }))
  152. #define g_atomic_pointer_add(atomic, val) \
  153. (G_GNUC_EXTENSION ({ \
  154. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  155. (void) (0 ? (gpointer) *(atomic) : 0); \
  156. (void) (0 ? (val) ^ (val) : 0); \
  157. (gssize) __sync_fetch_and_add ((atomic), (val)); \
  158. }))
  159. #define g_atomic_pointer_and(atomic, val) \
  160. (G_GNUC_EXTENSION ({ \
  161. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  162. (void) (0 ? (gpointer) *(atomic) : 0); \
  163. (void) (0 ? (val) ^ (val) : 0); \
  164. (gsize) __sync_fetch_and_and ((atomic), (val)); \
  165. }))
  166. #define g_atomic_pointer_or(atomic, val) \
  167. (G_GNUC_EXTENSION ({ \
  168. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  169. (void) (0 ? (gpointer) *(atomic) : 0); \
  170. (void) (0 ? (val) ^ (val) : 0); \
  171. (gsize) __sync_fetch_and_or ((atomic), (val)); \
  172. }))
  173. #define g_atomic_pointer_xor(atomic, val) \
  174. (G_GNUC_EXTENSION ({ \
  175. G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
  176. (void) (0 ? (gpointer) *(atomic) : 0); \
  177. (void) (0 ? (val) ^ (val) : 0); \
  178. (gsize) __sync_fetch_and_xor ((atomic), (val)); \
  179. }))
  180. #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
  181. #define g_atomic_int_get(atomic) \
  182. (g_atomic_int_get ((gint *) (atomic)))
  183. #define g_atomic_int_set(atomic, newval) \
  184. (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
  185. #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
  186. (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
  187. #define g_atomic_int_add(atomic, val) \
  188. (g_atomic_int_add ((gint *) (atomic), (val)))
  189. #define g_atomic_int_and(atomic, val) \
  190. (g_atomic_int_and ((guint *) (atomic), (val)))
  191. #define g_atomic_int_or(atomic, val) \
  192. (g_atomic_int_or ((guint *) (atomic), (val)))
  193. #define g_atomic_int_xor(atomic, val) \
  194. (g_atomic_int_xor ((guint *) (atomic), (val)))
  195. #define g_atomic_int_inc(atomic) \
  196. (g_atomic_int_inc ((gint *) (atomic)))
  197. #define g_atomic_int_dec_and_test(atomic) \
  198. (g_atomic_int_dec_and_test ((gint *) (atomic)))
  199. #define g_atomic_pointer_get(atomic) \
  200. (g_atomic_pointer_get (atomic))
  201. #define g_atomic_pointer_set(atomic, newval) \
  202. (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
  203. #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
  204. (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
  205. #define g_atomic_pointer_add(atomic, val) \
  206. (g_atomic_pointer_add ((atomic), (gssize) (val)))
  207. #define g_atomic_pointer_and(atomic, val) \
  208. (g_atomic_pointer_and ((atomic), (gsize) (val)))
  209. #define g_atomic_pointer_or(atomic, val) \
  210. (g_atomic_pointer_or ((atomic), (gsize) (val)))
  211. #define g_atomic_pointer_xor(atomic, val) \
  212. (g_atomic_pointer_xor ((atomic), (gsize) (val)))
  213. #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
  214. #endif /* __G_ATOMIC_H__ */