2 * Copyright (C) 2011-2013 Red Hat, Inc.
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.
9 * This 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
25 #include "testutils.h"
27 #include "viratomic.h"
28 #include "virrandom.h"
29 #include "virthread.h"
32 testTypes(const void *data ATTRIBUTE_UNUSED)
38 #define virAssertCmpInt(a, op, b) \
41 virAtomicIntSet(&u, 5);
42 u2 = virAtomicIntGet(&u);
43 virAssertCmpInt(u2, ==, 5);
45 res = virAtomicIntCompareExchange(&u, 6, 7);
48 virAssertCmpInt(u, ==, 5);
50 virAssertCmpInt(virAtomicIntAdd(&u, 1), ==, 5);
51 virAssertCmpInt(u, ==, 6);
53 virAssertCmpInt(virAtomicIntInc(&u), ==, 7);
54 virAssertCmpInt(u, ==, 7);
56 res = virAtomicIntDecAndTest(&u);
59 virAssertCmpInt(u, ==, 6);
61 u2 = virAtomicIntAnd(&u, 5);
62 virAssertCmpInt(u2, ==, 6);
63 virAssertCmpInt(u, ==, 4);
65 u2 = virAtomicIntOr(&u, 8);
66 virAssertCmpInt(u2, ==, 4);
67 virAssertCmpInt(u, ==, 12);
69 u2 = virAtomicIntXor(&u, 4);
70 virAssertCmpInt(u2, ==, 12);
71 virAssertCmpInt(u, ==, 8);
73 virAtomicIntSet(&s, 5);
74 s2 = virAtomicIntGet(&s);
75 virAssertCmpInt(s2, ==, 5);
77 res = virAtomicIntCompareExchange(&s, 6, 7);
80 virAssertCmpInt(s, ==, 5);
82 virAtomicIntAdd(&s, 1);
83 virAssertCmpInt(s, ==, 6);
86 virAssertCmpInt(s, ==, 7);
88 res = virAtomicIntDecAndTest(&s);
91 virAssertCmpInt(s, ==, 6);
93 s2 = virAtomicIntAnd(&s, 5);
94 virAssertCmpInt(s2, ==, 6);
95 virAssertCmpInt(s, ==, 4);
97 s2 = virAtomicIntOr(&s, 8);
98 virAssertCmpInt(s2, ==, 4);
99 virAssertCmpInt(s, ==, 12);
101 s2 = virAtomicIntXor(&s, 4);
102 virAssertCmpInt(s2, ==, 12);
103 virAssertCmpInt(s, ==, 8);
111 volatile int bucket[THREADS];
115 thread_func(void *data)
117 int idx = (intptr_t)data;
121 for (i = 0; i < ROUNDS; i++) {
122 d = virRandomBits(7);
124 virAtomicIntAdd(&atomic, d);
134 testThreads(const void *data ATTRIBUTE_UNUSED)
138 virThread threads[THREADS];
141 for (i = 0; i < THREADS; i++)
144 for (i = 0; i < THREADS; i++) {
145 if (virThreadCreate(&(threads[i]), true, thread_func, (void*)(intptr_t)i) < 0)
149 for (i = 0; i < THREADS; i++)
150 virThreadJoin(&threads[i]);
153 for (i = 0; i < THREADS; i++)
167 if (virThreadInitialize() < 0)
170 if (virtTestRun("types", testTypes, NULL) < 0)
172 if (virtTestRun("threads", testThreads, NULL) < 0)
178 VIRT_TEST_MAIN(mymain)