2012-07-11 13:35:43 +00:00
|
|
|
/*
|
2013-11-19 22:50:56 +00:00
|
|
|
* Copyright (C) 2011-2013 Red Hat, Inc.
|
2012-07-11 13:35:43 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-11 13:35:43 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <sched.h>
|
|
|
|
|
|
|
|
#include "testutils.h"
|
|
|
|
|
|
|
|
#include "viratomic.h"
|
|
|
|
#include "virrandom.h"
|
2012-12-13 15:49:48 +00:00
|
|
|
#include "virthread.h"
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testTypes(const void *data G_GNUC_UNUSED)
|
2012-07-11 13:35:43 +00:00
|
|
|
{
|
|
|
|
unsigned int u, u2;
|
|
|
|
int s, s2;
|
|
|
|
bool res;
|
|
|
|
|
2016-06-15 08:17:05 +00:00
|
|
|
#define testAssertEq(a, b) \
|
|
|
|
if (!(a == b)) \
|
2012-07-11 13:35:43 +00:00
|
|
|
return -1;
|
|
|
|
virAtomicIntSet(&u, 5);
|
|
|
|
u2 = virAtomicIntGet(&u);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(u2, 5);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
res = virAtomicIntCompareExchange(&u, 6, 7);
|
|
|
|
if (res)
|
|
|
|
return -1;
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(u, 5);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(virAtomicIntAdd(&u, 1), 5);
|
|
|
|
testAssertEq(u, 6);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(virAtomicIntInc(&u), 7);
|
|
|
|
testAssertEq(u, 7);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
res = virAtomicIntDecAndTest(&u);
|
|
|
|
if (res)
|
|
|
|
return -1;
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(u, 6);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
u2 = virAtomicIntAnd(&u, 5);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(u2, 6);
|
|
|
|
testAssertEq(u, 4);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
u2 = virAtomicIntOr(&u, 8);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(u2, 4);
|
|
|
|
testAssertEq(u, 12);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
u2 = virAtomicIntXor(&u, 4);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(u2, 12);
|
|
|
|
testAssertEq(u, 8);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
virAtomicIntSet(&s, 5);
|
|
|
|
s2 = virAtomicIntGet(&s);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(s2, 5);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
res = virAtomicIntCompareExchange(&s, 6, 7);
|
|
|
|
if (res)
|
|
|
|
return -1;
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(s, 5);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
virAtomicIntAdd(&s, 1);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(s, 6);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
virAtomicIntInc(&s);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(s, 7);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
res = virAtomicIntDecAndTest(&s);
|
|
|
|
if (res)
|
|
|
|
return -1;
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(s, 6);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
s2 = virAtomicIntAnd(&s, 5);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(s2, 6);
|
|
|
|
testAssertEq(s, 4);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
s2 = virAtomicIntOr(&s, 8);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(s2, 4);
|
|
|
|
testAssertEq(s, 12);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
s2 = virAtomicIntXor(&s, 4);
|
2016-06-15 08:17:05 +00:00
|
|
|
testAssertEq(s2, 12);
|
|
|
|
testAssertEq(s, 8);
|
2012-07-11 13:35:43 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define THREADS 10
|
|
|
|
#define ROUNDS 10000
|
|
|
|
|
|
|
|
volatile int bucket[THREADS];
|
|
|
|
volatile int atomic;
|
|
|
|
|
|
|
|
static void
|
|
|
|
thread_func(void *data)
|
|
|
|
{
|
2012-09-04 10:16:55 +00:00
|
|
|
int idx = (intptr_t)data;
|
Convert 'int i' to 'size_t i' in tests/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2012-07-11 13:35:43 +00:00
|
|
|
int d;
|
|
|
|
|
|
|
|
for (i = 0; i < ROUNDS; i++) {
|
|
|
|
d = virRandomBits(7);
|
|
|
|
bucket[idx] += d;
|
|
|
|
virAtomicIntAdd(&atomic, d);
|
|
|
|
#ifdef WIN32
|
2013-11-19 22:50:56 +00:00
|
|
|
SleepEx(0, 0);
|
2012-07-11 13:35:43 +00:00
|
|
|
#else
|
|
|
|
sched_yield();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testThreads(const void *data G_GNUC_UNUSED)
|
2012-07-11 13:35:43 +00:00
|
|
|
{
|
|
|
|
int sum;
|
Convert 'int i' to 'size_t i' in tests/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2012-07-11 13:35:43 +00:00
|
|
|
virThread threads[THREADS];
|
|
|
|
|
|
|
|
atomic = 0;
|
|
|
|
for (i = 0; i < THREADS; i++)
|
|
|
|
bucket[i] = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < THREADS; i++) {
|
2012-09-04 10:16:55 +00:00
|
|
|
if (virThreadCreate(&(threads[i]), true, thread_func, (void*)(intptr_t)i) < 0)
|
2012-07-11 13:35:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < THREADS; i++)
|
|
|
|
virThreadJoin(&threads[i]);
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
for (i = 0; i < THREADS; i++)
|
|
|
|
sum += bucket[i];
|
|
|
|
|
|
|
|
if (sum != atomic)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("types", testTypes, NULL) < 0)
|
2012-07-11 13:35:43 +00:00
|
|
|
ret = -1;
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("threads", testThreads, NULL) < 0)
|
2012-07-11 13:35:43 +00:00
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|