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