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