Make sure the rwlock and per thread lock could detect races
[urcu.git] / test_perthreadlock.c
1 /*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <stdio.h>
24 #include <pthread.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <sys/syscall.h>
33
34 #include "arch.h"
35
36 #if defined(_syscall0)
37 _syscall0(pid_t, gettid)
38 #elif defined(__NR_gettid)
39 static inline pid_t gettid(void)
40 {
41 return syscall(__NR_gettid);
42 }
43 #else
44 #warning "use pid as tid"
45 static inline pid_t gettid(void)
46 {
47 return getpid();
48 }
49 #endif
50
51 #ifndef DYNAMIC_LINK_TEST
52 #define _LGPL_SOURCE
53 #else
54 #define debug_yield_read()
55 #endif
56 #include "urcu.h"
57
58 struct test_array {
59 int a;
60 };
61
62 struct per_thread_lock {
63 pthread_mutex_t lock;
64 } __attribute__((aligned(128))); /* cache-line aligned */
65
66 static struct per_thread_lock *per_thread_lock;
67
68 static volatile int test_go, test_stop;
69
70 static int wdelay;
71
72 static volatile struct test_array test_array = { 8 };
73
74 static unsigned long duration;
75
76 /*
77 * returns 0 if test should end.
78 */
79 static int test_duration_write(void)
80 {
81 return !test_stop;
82 }
83
84 static int test_duration_read(void)
85 {
86 return !test_stop;
87 }
88
89 static unsigned long long __thread nr_writes;
90 static unsigned long long __thread nr_reads;
91
92 static unsigned long long __attribute__((aligned(128))) *tot_nr_writes;
93 static unsigned long long __attribute__((aligned(128))) *tot_nr_reads;
94
95 static unsigned int nr_readers;
96 static unsigned int nr_writers;
97
98 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
99
100 void rcu_copy_mutex_lock(void)
101 {
102 int ret;
103 ret = pthread_mutex_lock(&rcu_copy_mutex);
104 if (ret) {
105 perror("Error in pthread mutex lock");
106 exit(-1);
107 }
108 }
109
110 void rcu_copy_mutex_unlock(void)
111 {
112 int ret;
113
114 ret = pthread_mutex_unlock(&rcu_copy_mutex);
115 if (ret) {
116 perror("Error in pthread mutex unlock");
117 exit(-1);
118 }
119 }
120
121 void *thr_reader(void *data)
122 {
123 unsigned long tidx = (unsigned long)data;
124
125 printf("thread_begin %s, thread id : %lx, tid %lu\n",
126 "reader", pthread_self(), (unsigned long)gettid());
127
128 while (!test_go)
129 {
130 }
131
132 for (;;) {
133 pthread_mutex_lock(&per_thread_lock[tidx].lock);
134 assert(test_array.a == 8);
135 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
136 nr_reads++;
137 if (!test_duration_read())
138 break;
139 }
140
141 tot_nr_reads[tidx] = nr_reads;
142 printf("thread_end %s, thread id : %lx, tid %lu\n",
143 "reader", pthread_self(), (unsigned long)gettid());
144 return ((void*)1);
145
146 }
147
148 void *thr_writer(void *data)
149 {
150 unsigned long wtidx = (unsigned long)data;
151 long tidx;
152
153 printf("thread_begin %s, thread id : %lx, tid %lu\n",
154 "writer", pthread_self(), (unsigned long)gettid());
155
156 while (!test_go)
157 {
158 }
159 smp_mb();
160
161 for (;;) {
162 for (tidx = 0; tidx < nr_readers; tidx++) {
163 pthread_mutex_lock(&per_thread_lock[tidx].lock);
164 }
165 test_array.a = 0;
166 test_array.a = 8;
167 for (tidx = nr_readers - 1; tidx >= 0; tidx--) {
168 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
169 }
170 nr_writes++;
171 if (!test_duration_write())
172 break;
173 if (wdelay)
174 usleep(wdelay);
175 }
176
177 printf("thread_end %s, thread id : %lx, tid %lu\n",
178 "writer", pthread_self(), (unsigned long)gettid());
179 tot_nr_writes[wtidx] = nr_writes;
180 return ((void*)2);
181 }
182
183 void show_usage(int argc, char **argv)
184 {
185 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
186 #ifdef DEBUG_YIELD
187 printf(" [-r] [-w] (yield reader and/or writer)");
188 #endif
189 printf(" [-d delay] (writer period (us))");
190 printf("\n");
191 }
192
193 int main(int argc, char **argv)
194 {
195 int err;
196 pthread_t *tid_reader, *tid_writer;
197 void *tret;
198 unsigned long long *count_reader, *count_writer;
199 unsigned long long tot_reads = 0, tot_writes = 0;
200 int i;
201
202 if (argc < 4) {
203 show_usage(argc, argv);
204 return -1;
205 }
206 smp_mb();
207
208 err = sscanf(argv[1], "%u", &nr_readers);
209 if (err != 1) {
210 show_usage(argc, argv);
211 return -1;
212 }
213
214 err = sscanf(argv[2], "%u", &nr_writers);
215 if (err != 1) {
216 show_usage(argc, argv);
217 return -1;
218 }
219
220 err = sscanf(argv[3], "%lu", &duration);
221 if (err != 1) {
222 show_usage(argc, argv);
223 return -1;
224 }
225
226 for (i = 4; i < argc; i++) {
227 if (argv[i][0] != '-')
228 continue;
229 switch (argv[i][1]) {
230 #ifdef DEBUG_YIELD
231 case 'r':
232 yield_active |= YIELD_READ;
233 break;
234 case 'w':
235 yield_active |= YIELD_WRITE;
236 break;
237 #endif
238 case 'd':
239 if (argc < i + 2) {
240 show_usage(argc, argv);
241 return -1;
242 }
243 wdelay = atoi(argv[++i]);
244 break;
245 }
246 }
247
248 printf("running test for %lu seconds, %u readers, %u writers.\n",
249 duration, nr_readers, nr_writers);
250 printf("Writer delay : %u us.\n", wdelay);
251 printf("thread %-6s, thread id : %lx, tid %lu\n",
252 "main", pthread_self(), (unsigned long)gettid());
253
254 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
255 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
256 count_reader = malloc(sizeof(*count_reader) * nr_readers);
257 count_writer = malloc(sizeof(*count_writer) * nr_writers);
258 tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers);
259 tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
260 per_thread_lock = malloc(sizeof(*per_thread_lock) * nr_readers);
261
262 for (i = 0; i < nr_readers; i++) {
263 err = pthread_create(&tid_reader[i], NULL, thr_reader,
264 (void *)(long)i);
265 if (err != 0)
266 exit(1);
267 }
268 for (i = 0; i < nr_writers; i++) {
269 err = pthread_create(&tid_writer[i], NULL, thr_writer,
270 (void *)(long)i);
271 if (err != 0)
272 exit(1);
273 }
274
275 smp_mb();
276
277 test_go = 1;
278
279 sleep(duration);
280
281 test_stop = 1;
282
283 for (i = 0; i < nr_readers; i++) {
284 err = pthread_join(tid_reader[i], &tret);
285 if (err != 0)
286 exit(1);
287 tot_reads += tot_nr_reads[i];
288 }
289 for (i = 0; i < nr_writers; i++) {
290 err = pthread_join(tid_writer[i], &tret);
291 if (err != 0)
292 exit(1);
293 tot_writes += tot_nr_writes[i];
294 }
295
296 printf("total number of reads : %llu, writes %llu\n", tot_reads,
297 tot_writes);
298 free(tid_reader);
299 free(tid_writer);
300 free(count_reader);
301 free(count_writer);
302 free(tot_nr_reads);
303 free(tot_nr_writes);
304 free(per_thread_lock);
305 return 0;
306 }
This page took 0.049352 seconds and 4 git commands to generate.