• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Richard Palethorpe <rpalethorpe@suse.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 /* Basic functionality test for tst_fuzzy_sync.h similar to the atomic tests
18  * (test15.c). One thread writes to the odd indexes of an array while the
19  * other writes to the even. If the threads are not synchronised then they
20  * will probably write to the wrong indexes as they share an index variable
21  * which they should take it in turns to update.
22  */
23 
24 #include <stdlib.h>
25 #include "tst_test.h"
26 #include "tst_safe_pthread.h"
27 #include "tst_fuzzy_sync.h"
28 
29 /* LOOPS * 2 + 1 must be less than INT_MAX */
30 #define LOOPS 0xFFFFULL
31 
32 static volatile char seq[LOOPS * 2 + 1];
33 static struct tst_fzsync_pair pair;
34 static volatile int seq_n;
35 static volatile char last_wins;
36 
setup(void)37 static void setup(void)
38 {
39 	pair.exec_loops = LOOPS;
40 	tst_fzsync_pair_init(&pair);
41 }
42 
worker(void * v LTP_ATTRIBUTE_UNUSED)43 static void *worker(void *v LTP_ATTRIBUTE_UNUSED)
44 {
45 	unsigned long long i;
46 
47 	for (i = 0; tst_fzsync_run_b(&pair); i++) {
48 		tst_fzsync_start_race_b(&pair);
49 		usleep(1);
50 		last_wins = 'B';
51 		tst_fzsync_end_race_b(&pair);
52 		seq[seq_n] = 'B';
53 		seq_n = (i + 1) * 2 % (int)LOOPS * 2;
54 	}
55 
56 	if (i != LOOPS) {
57 		tst_res(TFAIL,
58 			"Worker performed wrong number of iterations: %lld != %lld",
59 			i, LOOPS);
60 	}
61 
62 	return NULL;
63 }
64 
run(void)65 static void run(void)
66 {
67 	unsigned int i, j, fail = 0, lost_race = 0;
68 
69 	tst_fzsync_pair_reset(&pair, worker);
70 	for (i = 0; tst_fzsync_run_a(&pair); i++) {
71 		tst_fzsync_start_race_a(&pair);
72 		seq[seq_n] = 'A';
73 		seq_n = i * 2 + 1;
74 		last_wins = 'A';
75 		tst_fzsync_end_race_a(&pair);
76 		if (last_wins == 'B')
77 			lost_race++;
78 	}
79 
80 	tst_res(TINFO, "Checking sequence...");
81 	for (i = 0; i < LOOPS; i++) {
82 		j = i * 2;
83 		if (seq[j] != 'A') {
84 			tst_res(TFAIL, "Expected A, but found %c at %d",
85 				seq[j], j);
86 			fail = 1;
87 		}
88 		j = i * 2 + 1;
89 		if (seq[j] != 'B') {
90 			tst_res(TFAIL, "Expected A, but found %c at %d",
91 				seq[j], j);
92 			fail = 1;
93 		}
94 	}
95 
96 	if (!fail)
97 		tst_res(TPASS, "Sequence is correct");
98 
99 	if (lost_race < 100)
100 		tst_res(TFAIL, "A only lost the race %d times", lost_race);
101 	else
102 		tst_res(TPASS, "A lost the race %d times", lost_race);
103 }
104 
cleanup(void)105 static void cleanup(void)
106 {
107 	tst_fzsync_pair_cleanup(&pair);
108 }
109 
110 static struct tst_test test = {
111 	.setup = setup,
112 	.cleanup = cleanup,
113 	.test_all = run,
114 };
115