xref: /third_party/ltp/lib/newlib_tests/test09.c (revision f08c3bdf)
1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2016 Linux Test Project
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * Test that tst_atomic_inc works as expected.
8f08c3bdfSopenharmony_ci */
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#include <pthread.h>
11f08c3bdfSopenharmony_ci#include "tst_test.h"
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#define THREADS 64
14f08c3bdfSopenharmony_ci#define ITERATIONS 100000
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic int atomic;
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic void *worker(void *id)
19f08c3bdfSopenharmony_ci{
20f08c3bdfSopenharmony_ci	int i;
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci	(void) id;
23f08c3bdfSopenharmony_ci	for (i = 0; i < ITERATIONS; i++)
24f08c3bdfSopenharmony_ci		tst_atomic_inc(&atomic);
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	return NULL;
27f08c3bdfSopenharmony_ci}
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic void do_test(void)
30f08c3bdfSopenharmony_ci{
31f08c3bdfSopenharmony_ci	long i;
32f08c3bdfSopenharmony_ci	pthread_t threads[THREADS];
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	for (i = 0; i < THREADS; i++)
35f08c3bdfSopenharmony_ci		pthread_create(threads+i, NULL, worker, (void *)i);
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	for (i = 0; i < THREADS; i++) {
38f08c3bdfSopenharmony_ci		tst_res(TINFO, "Joining thread %li", i);
39f08c3bdfSopenharmony_ci		pthread_join(threads[i], NULL);
40f08c3bdfSopenharmony_ci	}
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	if (atomic == THREADS * ITERATIONS)
43f08c3bdfSopenharmony_ci		tst_res(TPASS, "Atomic working as expected");
44f08c3bdfSopenharmony_ci	else
45f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Atomic does not have expected value");
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic struct tst_test test = {
49f08c3bdfSopenharmony_ci	.test_all = do_test,
50f08c3bdfSopenharmony_ci};
51