4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * Distributed under GPLv2
15 #include <sys/types.h>
20 #include <sys/syscall.h>
22 #if defined(_syscall0)
23 _syscall0(pid_t
, gettid
)
24 #elif defined(__NR_gettid)
25 static inline pid_t
gettid(void)
27 return syscall(__NR_gettid
);
30 #warning "use pid as tid"
31 static inline pid_t
gettid(void)
43 static int no_writer_delay
;
45 static struct test_array
*test_rcu_pointer
;
47 static unsigned long duration
;
48 static time_t start_time
;
49 static unsigned long __thread duration_interval
;
50 #define DURATION_TEST_DELAY 100
53 * returns 0 if test should end.
55 static int test_duration(void)
57 if (duration_interval
++ >= DURATION_TEST_DELAY
) {
58 duration_interval
= 0;
59 if (time(NULL
) - start_time
>= duration
)
68 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
70 void rcu_copy_mutex_lock(void)
73 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
75 perror("Error in pthread mutex lock");
80 void rcu_copy_mutex_unlock(void)
84 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
86 perror("Error in pthread mutex unlock");
92 * malloc/free are reusing memory areas too quickly, which does not let us
93 * test races appropriately. Use a large circular array for allocations.
94 * ARRAY_SIZE is larger than NR_WRITE, which insures we never run over our tail.
96 #define ARRAY_SIZE (1048576 * NR_WRITE)
97 #define ARRAY_POISON 0xDEADBEEF
98 static int array_index
;
99 static struct test_array test_array
[ARRAY_SIZE
];
101 static struct test_array
*test_array_alloc(void)
103 struct test_array
*ret
;
106 rcu_copy_mutex_lock();
107 index
= array_index
% ARRAY_SIZE
;
108 assert(test_array
[index
].a
== ARRAY_POISON
||
109 test_array
[index
].a
== 0);
110 ret
= &test_array
[index
];
112 if (array_index
== ARRAY_SIZE
)
114 rcu_copy_mutex_unlock();
118 static void test_array_free(struct test_array
*ptr
)
122 rcu_copy_mutex_lock();
123 ptr
->a
= ARRAY_POISON
;
124 rcu_copy_mutex_unlock();
127 void *thr_reader(void *arg
)
129 struct test_array
*local_ptr
;
131 printf("thread_begin %s, thread id : %lx, tid %lu\n",
132 "reader", pthread_self(), (unsigned long)gettid());
134 urcu_register_thread();
138 local_ptr
= rcu_dereference(test_rcu_pointer
);
141 assert(local_ptr
->a
== 8);
143 if (!test_duration())
147 urcu_unregister_thread();
149 printf("thread_end %s, thread id : %lx, tid %lu\n",
150 "reader", pthread_self(), (unsigned long)gettid());
155 void *thr_writer(void *arg
)
157 struct test_array
*new, *old
;
159 printf("thread_begin %s, thread id : %lx, tid %lu\n",
160 "writer", pthread_self(), (unsigned long)gettid());
163 new = test_array_alloc();
164 rcu_copy_mutex_lock();
165 old
= test_rcu_pointer
;
169 old
= urcu_publish_content(&test_rcu_pointer
, new);
170 rcu_copy_mutex_unlock();
171 /* can be done after unlock */
174 test_array_free(old
);
175 if (!test_duration())
177 if (!no_writer_delay
)
181 printf("thread_end %s, thread id : %lx, tid %lu\n",
182 "writer", pthread_self(), (unsigned long)gettid());
186 void show_usage(int argc
, char **argv
)
188 printf("Usage : %s duration (s)", argv
[0]);
190 printf(" [-r] [-w] (yield reader and/or writer)");
192 printf(" [-n] (disable writer delay)");
196 int main(int argc
, char **argv
)
199 pthread_t tid_reader
[NR_READ
], tid_writer
[NR_WRITE
];
204 show_usage(argc
, argv
);
208 err
= sscanf(argv
[1], "%lu", &duration
);
210 show_usage(argc
, argv
);
214 for (i
= 2; i
< argc
; i
++) {
215 if (argv
[i
][0] != '-')
217 switch (argv
[i
][1]) {
220 yield_active
|= YIELD_READ
;
223 yield_active
|= YIELD_WRITE
;
232 printf("running test for %lu seconds.\n", duration
);
233 start_time
= time(NULL
);
234 printf("thread %-6s, thread id : %lx, tid %lu\n",
235 "main", pthread_self(), (unsigned long)gettid());
237 for (i
= 0; i
< NR_READ
; i
++) {
238 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
, NULL
);
242 for (i
= 0; i
< NR_WRITE
; i
++) {
243 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
, NULL
);
248 for (i
= 0; i
< NR_READ
; i
++) {
249 err
= pthread_join(tid_reader
[i
], &tret
);
253 for (i
= 0; i
< NR_WRITE
; i
++) {
254 err
= pthread_join(tid_writer
[i
], &tret
);
258 test_array_free(test_rcu_pointer
);