Fix signal handler installation
[urcu.git] / test_perthreadlock.c
CommitLineData
0ee8bb0a
MD
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)
39static inline pid_t gettid(void)
40{
41 return syscall(__NR_gettid);
42}
43#else
44#warning "use pid as tid"
45static 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
58struct test_array {
59 int a;
60};
61
62struct per_thread_lock {
63 pthread_mutex_t lock;
64} __attribute__((aligned(128))); /* cache-line aligned */
65
66static struct per_thread_lock *per_thread_lock;
67
68static volatile int test_go;
69
70static int wdelay;
71
72static struct test_array test_array = { 8 };
73
74static unsigned long duration;
75static time_t start_time;
76static unsigned long __thread duration_interval;
77#define DURATION_TEST_DELAY_WRITE 4
78#define DURATION_TEST_DELAY_READ 100
79
80/*
81 * returns 0 if test should end.
82 */
83static int test_duration_write(void)
84{
85 if (duration_interval++ >= DURATION_TEST_DELAY_WRITE) {
86 duration_interval = 0;
87 if (time(NULL) - start_time >= duration)
88 return 0;
89 }
90 return 1;
91}
92
93static int test_duration_read(void)
94{
95 if (duration_interval++ >= DURATION_TEST_DELAY_READ) {
96 duration_interval = 0;
97 if (time(NULL) - start_time >= duration)
98 return 0;
99 }
100 return 1;
101}
102
103static unsigned long long __thread nr_writes;
104static unsigned long long __thread nr_reads;
105
106static unsigned long long __attribute__((aligned(128))) *tot_nr_writes;
107static unsigned long long __attribute__((aligned(128))) *tot_nr_reads;
108
109static unsigned int nr_readers;
110static unsigned int nr_writers;
111
112pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
113
114void rcu_copy_mutex_lock(void)
115{
116 int ret;
117 ret = pthread_mutex_lock(&rcu_copy_mutex);
118 if (ret) {
119 perror("Error in pthread mutex lock");
120 exit(-1);
121 }
122}
123
124void rcu_copy_mutex_unlock(void)
125{
126 int ret;
127
128 ret = pthread_mutex_unlock(&rcu_copy_mutex);
129 if (ret) {
130 perror("Error in pthread mutex unlock");
131 exit(-1);
132 }
133}
134
135void *thr_reader(void *data)
136{
137 unsigned long tidx = (unsigned long)data;
138
139 printf("thread_begin %s, thread id : %lx, tid %lu\n",
140 "reader", pthread_self(), (unsigned long)gettid());
141
142 while (!test_go)
143 {
144 }
145
146 for (;;) {
147 pthread_mutex_lock(&per_thread_lock[tidx].lock);
148 assert(test_array.a == 8);
149 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
150 nr_reads++;
151 if (!test_duration_read())
152 break;
153 }
154
155 tot_nr_reads[tidx] = nr_reads;
156 printf("thread_end %s, thread id : %lx, tid %lu\n",
157 "reader", pthread_self(), (unsigned long)gettid());
158 return ((void*)1);
159
160}
161
162void *thr_writer(void *data)
163{
164 unsigned long wtidx = (unsigned long)data;
165 long tidx;
166
167 printf("thread_begin %s, thread id : %lx, tid %lu\n",
168 "writer", pthread_self(), (unsigned long)gettid());
169
170 while (!test_go)
171 {
172 }
173 smp_mb();
174
175 for (;;) {
176 for (tidx = 0; tidx < nr_readers; tidx++) {
177 pthread_mutex_lock(&per_thread_lock[tidx].lock);
178 }
179 test_array.a = 8;
180 for (tidx = nr_readers - 1; tidx >= 0; tidx--) {
181 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
182 }
183 nr_writes++;
184 if (!test_duration_write())
185 break;
186 if (wdelay)
187 usleep(wdelay);
188 }
189
190 printf("thread_end %s, thread id : %lx, tid %lu\n",
191 "writer", pthread_self(), (unsigned long)gettid());
192 tot_nr_writes[wtidx] = nr_writes;
193 return ((void*)2);
194}
195
196void show_usage(int argc, char **argv)
197{
198 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
199#ifdef DEBUG_YIELD
200 printf(" [-r] [-w] (yield reader and/or writer)");
201#endif
202 printf(" [-d delay] (writer period (us))");
203 printf("\n");
204}
205
206int main(int argc, char **argv)
207{
208 int err;
209 pthread_t *tid_reader, *tid_writer;
210 void *tret;
211 unsigned long long *count_reader, *count_writer;
212 unsigned long long tot_reads = 0, tot_writes = 0;
213 int i;
214
215 if (argc < 4) {
216 show_usage(argc, argv);
217 return -1;
218 }
219 smp_mb();
220
221 err = sscanf(argv[1], "%u", &nr_readers);
222 if (err != 1) {
223 show_usage(argc, argv);
224 return -1;
225 }
226
227 err = sscanf(argv[2], "%u", &nr_writers);
228 if (err != 1) {
229 show_usage(argc, argv);
230 return -1;
231 }
232
233 err = sscanf(argv[3], "%lu", &duration);
234 if (err != 1) {
235 show_usage(argc, argv);
236 return -1;
237 }
238
239 for (i = 4; i < argc; i++) {
240 if (argv[i][0] != '-')
241 continue;
242 switch (argv[i][1]) {
243#ifdef DEBUG_YIELD
244 case 'r':
245 yield_active |= YIELD_READ;
246 break;
247 case 'w':
248 yield_active |= YIELD_WRITE;
249 break;
250#endif
251 case 'd':
252 if (argc < i + 2) {
253 show_usage(argc, argv);
254 return -1;
255 }
256 wdelay = atoi(argv[++i]);
257 break;
258 }
259 }
260
261 printf("running test for %lu seconds, %u readers, %u writers.\n",
262 duration, nr_readers, nr_writers);
263 printf("Writer delay : %u us.\n", wdelay);
264 printf("thread %-6s, thread id : %lx, tid %lu\n",
265 "main", pthread_self(), (unsigned long)gettid());
266
267 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
268 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
269 count_reader = malloc(sizeof(*count_reader) * nr_readers);
270 count_writer = malloc(sizeof(*count_writer) * nr_writers);
271 tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers);
272 tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
273 per_thread_lock = malloc(sizeof(*per_thread_lock) * nr_readers);
274
275 for (i = 0; i < nr_readers; i++) {
276 err = pthread_create(&tid_reader[i], NULL, thr_reader,
277 (void *)(long)i);
278 if (err != 0)
279 exit(1);
280 }
281 for (i = 0; i < nr_writers; i++) {
282 err = pthread_create(&tid_writer[i], NULL, thr_writer,
283 (void *)(long)i);
284 if (err != 0)
285 exit(1);
286 }
287
288 start_time = time(NULL);
289 test_go = 1;
290 smp_mb();
291
292 for (i = 0; i < nr_readers; i++) {
293 err = pthread_join(tid_reader[i], &tret);
294 if (err != 0)
295 exit(1);
296 tot_reads += tot_nr_reads[i];
297 }
298 for (i = 0; i < nr_writers; i++) {
299 err = pthread_join(tid_writer[i], &tret);
300 if (err != 0)
301 exit(1);
302 tot_writes += tot_nr_writes[i];
303 }
304
305 printf("total number of reads : %llu, writes %llu\n", tot_reads,
306 tot_writes);
307 free(tid_reader);
308 free(tid_writer);
309 free(count_reader);
310 free(count_writer);
311 free(tot_nr_reads);
312 free(tot_nr_writes);
313 free(per_thread_lock);
314 return 0;
315}
This page took 0.032137 seconds and 4 git commands to generate.