stdatomic: simple counter test
This commit is contained in:
parent
aa7727964b
commit
4bb3b3cec7
3 changed files with 83 additions and 0 deletions
75
tests/tests2/124_atomic_counter.c
Normal file
75
tests/tests2/124_atomic_counter.c
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
#define NR_THREADS 16
|
||||
#define NR_STEPS ((uint32_t)UINT16_MAX * 4)
|
||||
|
||||
#define BUG_ON(COND) \
|
||||
do { \
|
||||
if (!!(COND)) \
|
||||
abort(); \
|
||||
} while (0)
|
||||
|
||||
static
|
||||
void *adder_simple(void *arg)
|
||||
{
|
||||
size_t step;
|
||||
atomic_size_t *counter = arg;
|
||||
|
||||
for (step = 0; step < NR_STEPS; ++step)
|
||||
atomic_fetch_add_explicit(counter, 1, memory_order_relaxed);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static
|
||||
void *adder_cmpxchg(void *arg)
|
||||
{
|
||||
size_t step;
|
||||
atomic_size_t *counter = arg;
|
||||
|
||||
for (step = 0; step < NR_STEPS; ++step) {
|
||||
size_t xchg;
|
||||
size_t cmp = atomic_load_explicit(counter, memory_order_relaxed);
|
||||
|
||||
do {
|
||||
xchg = (cmp + 1);
|
||||
} while (!atomic_compare_exchange_strong_explicit(counter,
|
||||
&cmp, xchg, memory_order_relaxed, memory_order_relaxed));
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static
|
||||
void atomic_counter_test(void *(*adder)(void *arg))
|
||||
{
|
||||
size_t index;
|
||||
atomic_size_t counter;
|
||||
pthread_t thread[NR_THREADS];
|
||||
|
||||
atomic_init(&counter, 0);
|
||||
|
||||
for (index = 0; index < NR_THREADS; ++index)
|
||||
BUG_ON(pthread_create(&thread[index], NULL, adder, (void *)&counter));
|
||||
|
||||
for (index = 0; index < NR_THREADS; ++index)
|
||||
BUG_ON(pthread_join(thread[index], NULL));
|
||||
|
||||
if (atomic_load(&counter) == (NR_THREADS * NR_STEPS))
|
||||
printf("SUCCESS\n");
|
||||
else
|
||||
printf("FAILURE\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
atomic_counter_test(adder_simple);
|
||||
atomic_counter_test(adder_cmpxchg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
2
tests/tests2/124_atomic_counter.expect
Normal file
2
tests/tests2/124_atomic_counter.expect
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
SUCCESS
|
||||
SUCCESS
|
||||
|
|
@ -116,6 +116,12 @@ ifneq ($(CONFIG_bcheck),no)
|
|||
122_vla_reuse.test: FLAGS += -b
|
||||
endif
|
||||
|
||||
# this test needs pthread and is currently supported on x86 platforms
|
||||
ifneq (,$(filter i386 x86_64,$(ARCH)))
|
||||
SKIP += 124_atomic_counter.test
|
||||
endif
|
||||
124_atomic_counter.test: FLAGS += -pthread
|
||||
|
||||
# Filter source directory in warnings/errors (out-of-tree builds)
|
||||
FILTER = 2>&1 | sed -e 's,$(SRC)/,,g'
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue