mirror of
https://gitlab.rtems.org/rtems/rtos/rtems.git
synced 2026-09-24 04:16:05 +08:00
smptests/smpatomic08: Avoid copy and paste
This commit is contained in:
@@ -104,6 +104,45 @@ static bool is_master_worker(size_t worker_index)
|
||||
return worker_index == 0;
|
||||
}
|
||||
|
||||
static void test_fini(
|
||||
test_context *ctx,
|
||||
const char *test,
|
||||
bool atomic
|
||||
)
|
||||
{
|
||||
uint_fast32_t expected_value = 0;
|
||||
uint_fast32_t actual_value;
|
||||
size_t worker_index;
|
||||
|
||||
printf("=== atomic %s test case ==\n", test);
|
||||
|
||||
for (worker_index = 0; worker_index < ctx->worker_count; ++worker_index) {
|
||||
uint_fast32_t worker_value = ctx->per_worker_value[worker_index];
|
||||
|
||||
expected_value += worker_value;
|
||||
|
||||
printf(
|
||||
"worker %zu value: %" PRIuFAST32 "\n",
|
||||
worker_index,
|
||||
worker_value
|
||||
);
|
||||
}
|
||||
|
||||
if (atomic) {
|
||||
actual_value = _Atomic_Load_uint(&ctx->atomic_value, ATOMIC_ORDER_RELAXED);
|
||||
} else {
|
||||
actual_value = ctx->normal_value;
|
||||
}
|
||||
|
||||
printf(
|
||||
"atomic value: expected = %" PRIuFAST32 ", actual = %" PRIuFAST32 "\n",
|
||||
expected_value,
|
||||
actual_value
|
||||
);
|
||||
|
||||
rtems_test_assert(expected_value == actual_value);
|
||||
}
|
||||
|
||||
static void test_atomic_add_init(test_context *ctx)
|
||||
{
|
||||
_Atomic_Init_uint(&ctx->atomic_value, 0);
|
||||
@@ -123,33 +162,7 @@ static void test_atomic_add_body(test_context *ctx, size_t worker_index)
|
||||
|
||||
static void test_atomic_add_fini(test_context *ctx)
|
||||
{
|
||||
uint_fast32_t expected_counter = 0;
|
||||
uint_fast32_t actual_counter;
|
||||
size_t worker_index;
|
||||
|
||||
printf("=== atomic add test case ==\n");
|
||||
|
||||
for (worker_index = 0; worker_index < ctx->worker_count; ++worker_index) {
|
||||
uint_fast32_t worker_counter = ctx->per_worker_value[worker_index];
|
||||
|
||||
expected_counter += worker_counter;
|
||||
|
||||
printf(
|
||||
"atomic add worker %zu counter: %" PRIuFAST32 "\n",
|
||||
worker_index,
|
||||
worker_counter
|
||||
);
|
||||
}
|
||||
|
||||
actual_counter = _Atomic_Load_uint(&ctx->atomic_value, ATOMIC_ORDER_RELAXED);
|
||||
|
||||
printf(
|
||||
"global counter: expected = %" PRIuFAST32 ", actual = %" PRIuFAST32 "\n",
|
||||
expected_counter,
|
||||
actual_counter
|
||||
);
|
||||
|
||||
rtems_test_assert(expected_counter == actual_counter);
|
||||
test_fini(ctx, "add", true);
|
||||
}
|
||||
|
||||
static void test_atomic_flag_init(test_context *ctx)
|
||||
@@ -178,82 +191,29 @@ static void test_atomic_flag_body(test_context *ctx, size_t worker_index)
|
||||
|
||||
static void test_atomic_flag_fini(test_context *ctx)
|
||||
{
|
||||
uint_fast32_t expected_counter = 0;
|
||||
uint_fast32_t actual_counter;
|
||||
size_t worker_index;
|
||||
|
||||
printf("=== atomic flag test case ===\n");
|
||||
|
||||
for (worker_index = 0; worker_index < ctx->worker_count; ++worker_index) {
|
||||
uint_fast32_t worker_counter = ctx->per_worker_value[worker_index];
|
||||
|
||||
expected_counter += worker_counter;
|
||||
|
||||
printf(
|
||||
"atomic flag worker %zu counter: %" PRIuFAST32 "\n",
|
||||
worker_index,
|
||||
worker_counter
|
||||
);
|
||||
}
|
||||
|
||||
actual_counter = ctx->normal_value;
|
||||
|
||||
printf(
|
||||
"global flag counter: expected = %" PRIuFAST32 ", actual = %" PRIuFAST32 "\n",
|
||||
expected_counter,
|
||||
actual_counter
|
||||
);
|
||||
|
||||
rtems_test_assert(expected_counter == actual_counter);
|
||||
test_fini(ctx, "flag", false);
|
||||
}
|
||||
|
||||
static void test_atomic_sub_init(test_context *ctx)
|
||||
{
|
||||
_Atomic_Init_uint(&ctx->atomic_value, 0xffffffff);
|
||||
_Atomic_Init_uint(&ctx->atomic_value, 0);
|
||||
}
|
||||
|
||||
static void test_atomic_sub_body(test_context *ctx, size_t worker_index)
|
||||
{
|
||||
uint_fast32_t counter = 0xffffffff;
|
||||
uint_fast32_t counter = 0;
|
||||
|
||||
while (!stop(ctx)) {
|
||||
--counter;
|
||||
_Atomic_Fetch_sub_uint(&ctx->atomic_value, 1, ATOMIC_ORDER_RELAXED);
|
||||
}
|
||||
|
||||
ctx->per_worker_value[worker_index] = 0xffffffff - counter;
|
||||
ctx->per_worker_value[worker_index] = counter;
|
||||
}
|
||||
|
||||
static void test_atomic_sub_fini(test_context *ctx)
|
||||
{
|
||||
uint_fast32_t expected_counter = 0;
|
||||
uint_fast32_t actual_counter;
|
||||
size_t worker_index;
|
||||
|
||||
printf("=== atomic sub test case ==\n");
|
||||
|
||||
for (worker_index = 0; worker_index < ctx->worker_count; ++worker_index) {
|
||||
uint_fast32_t worker_counter = ctx->per_worker_value[worker_index];
|
||||
|
||||
expected_counter += worker_counter;
|
||||
|
||||
printf(
|
||||
"atomic sub worker %zu counter: %" PRIuFAST32 "\n",
|
||||
worker_index,
|
||||
worker_counter
|
||||
);
|
||||
}
|
||||
|
||||
actual_counter = _Atomic_Load_uint(&ctx->atomic_value, ATOMIC_ORDER_RELAXED);
|
||||
actual_counter = 0xffffffff - actual_counter;
|
||||
|
||||
printf(
|
||||
"global counter: expected = %" PRIuFAST32 ", actual = %" PRIuFAST32 "\n",
|
||||
expected_counter,
|
||||
actual_counter
|
||||
);
|
||||
|
||||
rtems_test_assert(expected_counter == actual_counter);
|
||||
test_fini(ctx, "sub", true);
|
||||
}
|
||||
|
||||
static void test_atomic_compare_exchange_init(test_context *ctx)
|
||||
@@ -292,33 +252,7 @@ static void test_atomic_compare_exchange_body(test_context *ctx, size_t worker_i
|
||||
|
||||
static void test_atomic_compare_exchange_fini(test_context *ctx)
|
||||
{
|
||||
uint_fast32_t expected_counter = 0;
|
||||
uint_fast32_t actual_counter;
|
||||
size_t worker_index;
|
||||
|
||||
printf("=== atomic compare_exchange test case ==\n");
|
||||
|
||||
for (worker_index = 0; worker_index < ctx->worker_count; ++worker_index) {
|
||||
uint_fast32_t worker_counter = ctx->per_worker_value[worker_index];
|
||||
|
||||
expected_counter += worker_counter;
|
||||
|
||||
printf(
|
||||
"atomic compare_exchange worker %zu counter: %" PRIuFAST32 "\n",
|
||||
worker_index,
|
||||
worker_counter
|
||||
);
|
||||
}
|
||||
|
||||
actual_counter = ctx->normal_value;
|
||||
|
||||
printf(
|
||||
"global counter: expected = %" PRIuFAST32 ", actual = %" PRIuFAST32 "\n",
|
||||
expected_counter,
|
||||
actual_counter
|
||||
);
|
||||
|
||||
rtems_test_assert(expected_counter == actual_counter);
|
||||
test_fini(ctx, "compare exchange", false);
|
||||
}
|
||||
|
||||
static void test_atomic_or_and_init(test_context *ctx)
|
||||
@@ -358,42 +292,31 @@ static void test_atomic_or_and_body(test_context *ctx, size_t worker_index)
|
||||
|
||||
static void test_atomic_or_and_fini(test_context *ctx)
|
||||
{
|
||||
uint_fast32_t expected_counter = 0;
|
||||
uint_fast32_t actual_counter;
|
||||
size_t worker_index;
|
||||
|
||||
printf("=== atomic or_and test case ==\n");
|
||||
|
||||
for (worker_index = 0; worker_index < ctx->worker_count; ++worker_index) {
|
||||
uint_fast32_t worker_counter = ctx->per_worker_value[worker_index];
|
||||
|
||||
expected_counter += worker_counter;
|
||||
|
||||
printf(
|
||||
"atomic or_and worker %zu counter: %" PRIuFAST32 "\n",
|
||||
worker_index,
|
||||
worker_counter
|
||||
);
|
||||
}
|
||||
|
||||
actual_counter = _Atomic_Load_uint(&ctx->atomic_value, ATOMIC_ORDER_RELAXED);
|
||||
|
||||
printf(
|
||||
"global counter: expected = %" PRIuFAST32 ", actual = %" PRIuFAST32 "\n",
|
||||
expected_counter,
|
||||
actual_counter
|
||||
);
|
||||
|
||||
rtems_test_assert(expected_counter == actual_counter);
|
||||
test_fini(ctx, "or/and", true);
|
||||
}
|
||||
|
||||
static const test_case test_cases[] = {
|
||||
{ test_atomic_add_init, test_atomic_add_body, test_atomic_add_fini },
|
||||
{ test_atomic_flag_init, test_atomic_flag_body, test_atomic_flag_fini },
|
||||
{ test_atomic_sub_init, test_atomic_sub_body, test_atomic_sub_fini },
|
||||
{ test_atomic_compare_exchange_init, test_atomic_compare_exchange_body,
|
||||
test_atomic_compare_exchange_fini },
|
||||
{ test_atomic_or_and_init, test_atomic_or_and_body, test_atomic_or_and_fini },
|
||||
{
|
||||
test_atomic_add_init,
|
||||
test_atomic_add_body,
|
||||
test_atomic_add_fini
|
||||
}, {
|
||||
test_atomic_flag_init,
|
||||
test_atomic_flag_body,
|
||||
test_atomic_flag_fini
|
||||
}, {
|
||||
test_atomic_sub_init,
|
||||
test_atomic_sub_body,
|
||||
test_atomic_sub_fini
|
||||
}, {
|
||||
test_atomic_compare_exchange_init,
|
||||
test_atomic_compare_exchange_body,
|
||||
test_atomic_compare_exchange_fini
|
||||
}, {
|
||||
test_atomic_or_and_init,
|
||||
test_atomic_or_and_body,
|
||||
test_atomic_or_and_fini
|
||||
},
|
||||
};
|
||||
|
||||
#define TEST_COUNT RTEMS_ARRAY_SIZE(test_cases)
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
*** TEST SMPATOMIC 8 ***
|
||||
=== atomic add test case ==
|
||||
atomic add worker 0 counter: 18583
|
||||
atomic add worker 1 counter: 36324
|
||||
global counter: expected = 54907, actual = 54907
|
||||
=== atomic flag test case ===
|
||||
atomic flag worker 0 counter: 7388
|
||||
atomic flag worker 1 counter: 17280
|
||||
global flag counter: expected = 24668, actual = 24668
|
||||
worker 0 value: 16686
|
||||
worker 1 value: 36405
|
||||
atomic value: expected = 53091, actual = 53091
|
||||
=== atomic flag test case ==
|
||||
worker 0 value: 5588
|
||||
worker 1 value: 16019
|
||||
atomic value: expected = 21607, actual = 21607
|
||||
=== atomic sub test case ==
|
||||
atomic sub worker 0 counter: 18583
|
||||
atomic sub worker 1 counter: 36324
|
||||
global counter: expected = 54907, actual = 54907
|
||||
=== atomic compare_exchange test case ==
|
||||
atomic compare_exchange worker 0 counter: 3467
|
||||
atomic compare_exchange worker 1 counter: 19635
|
||||
global counter: expected = 23102, actual = 23102
|
||||
=== atomic or_and test case ==
|
||||
atomic or_and worker 0 counter: 1
|
||||
atomic or_and worker 1 counter: 1
|
||||
global counter: expected = 3, actual = 3
|
||||
worker 0 value: 4294950967
|
||||
worker 1 value: 4294930886
|
||||
atomic value: expected = 4294914557, actual = 4294914557
|
||||
=== atomic compare exchange test case ==
|
||||
worker 0 value: 2950
|
||||
worker 1 value: 22456
|
||||
atomic value: expected = 25406, actual = 25406
|
||||
=== atomic or/and test case ==
|
||||
worker 0 value: 1
|
||||
worker 1 value: 0
|
||||
atomic value: expected = 1, actual = 1
|
||||
*** END OF TEST SMPATOMIC 8 ***
|
||||
|
||||
Reference in New Issue
Block a user