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 testAssertEq(a, b) \
41 virAtomicIntSet(&u
, 5);
42 u2
= virAtomicIntGet(&u
);
45 res
= virAtomicIntCompareExchange(&u
, 6, 7);
50 testAssertEq(virAtomicIntAdd(&u
, 1), 5);
53 testAssertEq(virAtomicIntInc(&u
), 7);
56 res
= virAtomicIntDecAndTest(&u
);
61 u2
= virAtomicIntAnd(&u
, 5);
65 u2
= virAtomicIntOr(&u
, 8);
69 u2
= virAtomicIntXor(&u
, 4);
73 virAtomicIntSet(&s
, 5);
74 s2
= virAtomicIntGet(&s
);
77 res
= virAtomicIntCompareExchange(&s
, 6, 7);
82 virAtomicIntAdd(&s
, 1);
88 res
= virAtomicIntDecAndTest(&s
);
93 s2
= virAtomicIntAnd(&s
, 5);
97 s2
= virAtomicIntOr(&s
, 8);
101 s2
= virAtomicIntXor(&s
, 4);
102 testAssertEq(s2
, 12);
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 (virTestRun("types", testTypes
, NULL
) < 0)
172 if (virTestRun("threads", testThreads
, NULL
) < 0)
178 VIR_TEST_MAIN(mymain
)