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