2 * Copyright 2011 Red Hat, Inc.
4 * This program 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 * License, or (at your option) any later version.
9 * See the included COPYING file for more information.
18 const gint * const *cspp;
29 g_atomic_int_set (&u, 5);
30 u2 = g_atomic_int_get (&u);
31 g_assert_cmpint (u2, ==, 5);
32 res = g_atomic_int_compare_and_exchange (&u, 6, 7);
34 g_assert_cmpint (u, ==, 5);
35 g_atomic_int_add (&u, 1);
36 g_assert_cmpint (u, ==, 6);
37 g_atomic_int_inc (&u);
38 g_assert_cmpint (u, ==, 7);
39 res = g_atomic_int_dec_and_test (&u);
41 g_assert_cmpint (u, ==, 6);
42 u2 = g_atomic_int_and (&u, 5);
43 g_assert_cmpint (u2, ==, 6);
44 g_assert_cmpint (u, ==, 4);
45 u2 = g_atomic_int_or (&u, 8);
46 g_assert_cmpint (u2, ==, 4);
47 g_assert_cmpint (u, ==, 12);
48 u2 = g_atomic_int_xor (&u, 4);
49 g_assert_cmpint (u2, ==, 12);
50 g_assert_cmpint (u, ==, 8);
52 g_atomic_int_set (&s, 5);
53 s2 = g_atomic_int_get (&s);
54 g_assert_cmpint (s2, ==, 5);
55 res = g_atomic_int_compare_and_exchange (&s, 6, 7);
57 g_assert_cmpint (s, ==, 5);
58 g_atomic_int_add (&s, 1);
59 g_assert_cmpint (s, ==, 6);
60 g_atomic_int_inc (&s);
61 g_assert_cmpint (s, ==, 7);
62 res = g_atomic_int_dec_and_test (&s);
64 g_assert_cmpint (s, ==, 6);
65 s2 = g_atomic_int_and (&s, 5);
66 g_assert_cmpint (s2, ==, 6);
67 g_assert_cmpint (s, ==, 4);
68 s2 = g_atomic_int_or (&s, 8);
69 g_assert_cmpint (s2, ==, 4);
70 g_assert_cmpint (s, ==, 12);
71 s2 = g_atomic_int_xor (&s, 4);
72 g_assert_cmpint (s2, ==, 12);
73 g_assert_cmpint (s, ==, 8);
75 g_atomic_pointer_set (&vp, 0);
76 vp2 = g_atomic_pointer_get (&vp);
78 res = g_atomic_pointer_compare_and_exchange (&vp, 0, 0);
82 g_atomic_pointer_set (&ip, 0);
83 ip2 = g_atomic_pointer_get (&ip);
85 res = g_atomic_pointer_compare_and_exchange (&ip, 0, 0);
89 g_atomic_pointer_set (&gs, 0);
90 gs2 = (gsize) g_atomic_pointer_get (&gs);
92 res = g_atomic_pointer_compare_and_exchange (&gs, 0, 0);
95 gs2 = g_atomic_pointer_add (&gs, 5);
98 gs2 = g_atomic_pointer_and (&gs, 6);
101 gs2 = g_atomic_pointer_or (&gs, 8);
104 gs2 = g_atomic_pointer_xor (&gs, 4);
105 g_assert (gs2 == 12);
108 g_assert (g_atomic_int_get (csp) == s);
109 g_assert (g_atomic_pointer_get (cspp) == csp);
111 /* repeat, without the macros */
112 #undef g_atomic_int_set
113 #undef g_atomic_int_get
114 #undef g_atomic_int_compare_and_exchange
115 #undef g_atomic_int_add
116 #undef g_atomic_int_inc
117 #undef g_atomic_int_and
118 #undef g_atomic_int_or
119 #undef g_atomic_int_xor
120 #undef g_atomic_int_dec_and_test
121 #undef g_atomic_pointer_set
122 #undef g_atomic_pointer_get
123 #undef g_atomic_pointer_compare_and_exchange
124 #undef g_atomic_pointer_add
125 #undef g_atomic_pointer_and
126 #undef g_atomic_pointer_or
127 #undef g_atomic_pointer_xor
129 g_atomic_int_set ((gint*)&u, 5);
130 u2 = g_atomic_int_get ((gint*)&u);
131 g_assert_cmpint (u2, ==, 5);
132 res = g_atomic_int_compare_and_exchange ((gint*)&u, 6, 7);
134 g_assert_cmpint (u, ==, 5);
135 g_atomic_int_add ((gint*)&u, 1);
136 g_assert_cmpint (u, ==, 6);
137 g_atomic_int_inc ((gint*)&u);
138 g_assert_cmpint (u, ==, 7);
139 res = g_atomic_int_dec_and_test ((gint*)&u);
141 g_assert_cmpint (u, ==, 6);
142 u2 = g_atomic_int_and (&u, 5);
143 g_assert_cmpint (u2, ==, 6);
144 g_assert_cmpint (u, ==, 4);
145 u2 = g_atomic_int_or (&u, 8);
146 g_assert_cmpint (u2, ==, 4);
147 g_assert_cmpint (u, ==, 12);
148 u2 = g_atomic_int_xor (&u, 4);
149 g_assert_cmpint (u2, ==, 12);
151 g_atomic_int_set (&s, 5);
152 s2 = g_atomic_int_get (&s);
153 g_assert_cmpint (s2, ==, 5);
154 res = g_atomic_int_compare_and_exchange (&s, 6, 7);
156 g_assert_cmpint (s, ==, 5);
157 g_atomic_int_add (&s, 1);
158 g_assert_cmpint (s, ==, 6);
159 g_atomic_int_inc (&s);
160 g_assert_cmpint (s, ==, 7);
161 res = g_atomic_int_dec_and_test (&s);
163 g_assert_cmpint (s, ==, 6);
164 s2 = g_atomic_int_and ((guint*)&s, 5);
165 g_assert_cmpint (s2, ==, 6);
166 g_assert_cmpint (s, ==, 4);
167 s2 = g_atomic_int_or ((guint*)&s, 8);
168 g_assert_cmpint (s2, ==, 4);
169 g_assert_cmpint (s, ==, 12);
170 s2 = g_atomic_int_xor ((guint*)&s, 4);
171 g_assert_cmpint (s2, ==, 12);
172 g_assert_cmpint (s, ==, 8);
173 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
174 s2 = g_atomic_int_exchange_and_add ((gint*)&s, 1);
175 G_GNUC_END_IGNORE_DEPRECATIONS
176 g_assert_cmpint (s2, ==, 8);
177 g_assert_cmpint (s, ==, 9);
179 g_atomic_pointer_set (&vp, 0);
180 vp2 = g_atomic_pointer_get (&vp);
182 res = g_atomic_pointer_compare_and_exchange (&vp, 0, 0);
186 g_atomic_pointer_set (&ip, 0);
187 ip2 = g_atomic_pointer_get (&ip);
189 res = g_atomic_pointer_compare_and_exchange (&ip, 0, 0);
193 g_atomic_pointer_set (&gs, 0);
194 gs2 = (gsize) g_atomic_pointer_get (&gs);
196 res = g_atomic_pointer_compare_and_exchange (&gs, 0, 0);
199 gs2 = g_atomic_pointer_add (&gs, 5);
202 gs2 = g_atomic_pointer_and (&gs, 6);
205 gs2 = g_atomic_pointer_or (&gs, 8);
208 gs2 = g_atomic_pointer_xor (&gs, 4);
209 g_assert (gs2 == 12);
212 g_assert (g_atomic_int_get (csp) == s);
213 g_assert (g_atomic_pointer_get (cspp) == csp);
219 volatile gint bucket[THREADS];
220 volatile gint atomic;
223 thread_func (gpointer data)
225 gint idx = GPOINTER_TO_INT (data);
229 for (i = 0; i < ROUNDS; i++)
231 d = g_random_int_range (-10, 100);
233 g_atomic_int_add (&atomic, d);
245 GThread *threads[THREADS];
248 for (i = 0; i < THREADS; i++)
251 for (i = 0; i < THREADS; i++)
252 threads[i] = g_thread_new ("atomic", thread_func, GINT_TO_POINTER (i));
254 for (i = 0; i < THREADS; i++)
255 g_thread_join (threads[i]);
258 for (i = 0; i < THREADS; i++)
261 g_assert_cmpint (sum, ==, atomic);
265 main (int argc, char **argv)
267 g_test_init (&argc, &argv, NULL);
269 g_test_add_func ("/atomic/types", test_types);
270 g_test_add_func ("/atomic/threaded", test_threaded);
272 return g_test_run ();