backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / viratomictest.c
blob52f17154e9d96567df1334c7540a010b04aae5a4
1 /*
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/>.
20 #include <config.h>
22 #include <time.h>
23 #include <sched.h>
25 #include "testutils.h"
27 #include "viratomic.h"
28 #include "virrandom.h"
29 #include "virthread.h"
31 static int
32 testTypes(const void *data ATTRIBUTE_UNUSED)
34 unsigned int u, u2;
35 int s, s2;
36 bool res;
38 #define testAssertEq(a, b) \
39 if (!(a == b)) \
40 return -1;
41 virAtomicIntSet(&u, 5);
42 u2 = virAtomicIntGet(&u);
43 testAssertEq(u2, 5);
45 res = virAtomicIntCompareExchange(&u, 6, 7);
46 if (res)
47 return -1;
48 testAssertEq(u, 5);
50 testAssertEq(virAtomicIntAdd(&u, 1), 5);
51 testAssertEq(u, 6);
53 testAssertEq(virAtomicIntInc(&u), 7);
54 testAssertEq(u, 7);
56 res = virAtomicIntDecAndTest(&u);
57 if (res)
58 return -1;
59 testAssertEq(u, 6);
61 u2 = virAtomicIntAnd(&u, 5);
62 testAssertEq(u2, 6);
63 testAssertEq(u, 4);
65 u2 = virAtomicIntOr(&u, 8);
66 testAssertEq(u2, 4);
67 testAssertEq(u, 12);
69 u2 = virAtomicIntXor(&u, 4);
70 testAssertEq(u2, 12);
71 testAssertEq(u, 8);
73 virAtomicIntSet(&s, 5);
74 s2 = virAtomicIntGet(&s);
75 testAssertEq(s2, 5);
77 res = virAtomicIntCompareExchange(&s, 6, 7);
78 if (res)
79 return -1;
80 testAssertEq(s, 5);
82 virAtomicIntAdd(&s, 1);
83 testAssertEq(s, 6);
85 virAtomicIntInc(&s);
86 testAssertEq(s, 7);
88 res = virAtomicIntDecAndTest(&s);
89 if (res)
90 return -1;
91 testAssertEq(s, 6);
93 s2 = virAtomicIntAnd(&s, 5);
94 testAssertEq(s2, 6);
95 testAssertEq(s, 4);
97 s2 = virAtomicIntOr(&s, 8);
98 testAssertEq(s2, 4);
99 testAssertEq(s, 12);
101 s2 = virAtomicIntXor(&s, 4);
102 testAssertEq(s2, 12);
103 testAssertEq(s, 8);
105 return 0;
108 #define THREADS 10
109 #define ROUNDS 10000
111 volatile int bucket[THREADS];
112 volatile int atomic;
114 static void
115 thread_func(void *data)
117 int idx = (intptr_t)data;
118 size_t i;
119 int d;
121 for (i = 0; i < ROUNDS; i++) {
122 d = virRandomBits(7);
123 bucket[idx] += d;
124 virAtomicIntAdd(&atomic, d);
125 #ifdef WIN32
126 SleepEx(0, 0);
127 #else
128 sched_yield();
129 #endif
133 static int
134 testThreads(const void *data ATTRIBUTE_UNUSED)
136 int sum;
137 size_t i;
138 virThread threads[THREADS];
140 atomic = 0;
141 for (i = 0; i < THREADS; i++)
142 bucket[i] = 0;
144 for (i = 0; i < THREADS; i++) {
145 if (virThreadCreate(&(threads[i]), true, thread_func, (void*)(intptr_t)i) < 0)
146 return -1;
149 for (i = 0; i < THREADS; i++)
150 virThreadJoin(&threads[i]);
152 sum = 0;
153 for (i = 0; i < THREADS; i++)
154 sum += bucket[i];
156 if (sum != atomic)
157 return -1;
159 return 0;
162 static int
163 mymain(void)
165 int ret = 0;
167 if (virThreadInitialize() < 0)
168 return -1;
170 if (virTestRun("types", testTypes, NULL) < 0)
171 ret = -1;
172 if (virTestRun("threads", testThreads, NULL) < 0)
173 ret = -1;
175 return ret;
178 VIR_TEST_MAIN(mymain)