Commit | Line | Data |
---|---|---|
b257a10b 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 | * Distributed under GPLv2 | |
9 | */ | |
10 | ||
ac260fd9 | 11 | #include <stdio.h> |
f69f195a MD |
12 | #include <pthread.h> |
13 | #include <stdlib.h> | |
41718ff9 | 14 | #include <string.h> |
f69f195a MD |
15 | #include <sys/types.h> |
16 | #include <sys/wait.h> | |
17 | #include <unistd.h> | |
18 | #include <stdio.h> | |
41718ff9 | 19 | #include <assert.h> |
87bd15cd BW |
20 | #include <sys/syscall.h> |
21 | ||
22 | #if defined(_syscall0) | |
23 | _syscall0(pid_t, gettid) | |
24 | #elif defined(__NR_gettid) | |
25 | static inline pid_t gettid(void) | |
26 | { | |
27 | return syscall(__NR_gettid); | |
28 | } | |
29 | #else | |
30 | #warning "use pid as tid" | |
31 | static inline pid_t gettid(void) | |
32 | { | |
33 | return getpid(); | |
34 | } | |
35 | #endif | |
36 | ||
ac260fd9 MD |
37 | #include "urcu.h" |
38 | ||
41718ff9 MD |
39 | struct test_array { |
40 | int a; | |
41718ff9 MD |
41 | }; |
42 | ||
43 | static struct test_array *test_rcu_pointer; | |
44 | ||
cf380c2f MD |
45 | static unsigned long duration; |
46 | static time_t start_time; | |
47 | static unsigned long __thread duration_interval; | |
48 | #define DURATION_TEST_DELAY 100 | |
49 | ||
50 | /* | |
51 | * returns 0 if test should end. | |
52 | */ | |
53 | static int test_duration(void) | |
54 | { | |
55 | if (duration_interval++ >= DURATION_TEST_DELAY) { | |
56 | duration_interval = 0; | |
57 | if (time(NULL) - start_time >= duration) | |
58 | return 0; | |
59 | } | |
60 | return 1; | |
61 | } | |
62 | ||
b1b5ce8f | 63 | #define NR_READ 10 |
8c8eed97 | 64 | #define NR_WRITE 9 |
f69f195a | 65 | |
c265818b MD |
66 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
67 | ||
68 | void rcu_copy_mutex_lock(void) | |
69 | { | |
70 | int ret; | |
71 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
72 | if (ret) { | |
73 | perror("Error in pthread mutex lock"); | |
74 | exit(-1); | |
75 | } | |
76 | } | |
77 | ||
78 | void rcu_copy_mutex_unlock(void) | |
79 | { | |
80 | int ret; | |
81 | ||
82 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
83 | if (ret) { | |
84 | perror("Error in pthread mutex unlock"); | |
85 | exit(-1); | |
86 | } | |
87 | } | |
f69f195a MD |
88 | |
89 | void *thr_reader(void *arg) | |
90 | { | |
41718ff9 MD |
91 | struct test_array *local_ptr; |
92 | ||
cf380c2f | 93 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 94 | "reader", pthread_self(), (unsigned long)gettid()); |
f69f195a MD |
95 | |
96 | urcu_register_thread(); | |
97 | ||
cf380c2f | 98 | for (;;) { |
1430ee0b | 99 | rcu_read_lock(); |
cf380c2f MD |
100 | local_ptr = rcu_dereference(test_rcu_pointer); |
101 | if (local_ptr) | |
102 | assert(local_ptr->a == 8); | |
1430ee0b | 103 | rcu_read_unlock(); |
cf380c2f MD |
104 | if (!test_duration()) |
105 | break; | |
41718ff9 | 106 | } |
f69f195a MD |
107 | |
108 | urcu_unregister_thread(); | |
41718ff9 | 109 | |
cf380c2f MD |
110 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
111 | "reader", pthread_self(), (unsigned long)gettid()); | |
f69f195a MD |
112 | return ((void*)1); |
113 | ||
114 | } | |
115 | ||
116 | void *thr_writer(void *arg) | |
117 | { | |
41718ff9 | 118 | struct test_array *new, *old; |
f69f195a | 119 | |
cf380c2f | 120 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 121 | "writer", pthread_self(), (unsigned long)gettid()); |
f69f195a | 122 | |
cf380c2f | 123 | for (;;) { |
41718ff9 | 124 | new = malloc(sizeof(struct test_array)); |
c265818b | 125 | rcu_copy_mutex_lock(); |
41718ff9 | 126 | old = test_rcu_pointer; |
cf380c2f | 127 | if (old) |
41718ff9 | 128 | assert(old->a == 8); |
ad6ce6ae | 129 | new->a = 8; |
f4a486ac | 130 | old = urcu_publish_content(&test_rcu_pointer, new); |
c265818b | 131 | rcu_copy_mutex_unlock(); |
41718ff9 | 132 | /* can be done after unlock */ |
cf380c2f | 133 | if (old) |
8c8eed97 | 134 | old->a = 0; |
41718ff9 | 135 | free(old); |
cf380c2f MD |
136 | if (!test_duration()) |
137 | break; | |
8c8eed97 | 138 | usleep(1); |
f69f195a MD |
139 | } |
140 | ||
cf380c2f MD |
141 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
142 | "writer", pthread_self(), (unsigned long)gettid()); | |
f69f195a MD |
143 | return ((void*)2); |
144 | } | |
ac260fd9 | 145 | |
1430ee0b MD |
146 | void show_usage(int argc, char **argv) |
147 | { | |
148 | printf("Usage : %s duration (s)", argv[0]); | |
149 | #ifdef DEBUG_YIELD | |
150 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
151 | #endif | |
152 | printf("\n"); | |
153 | } | |
154 | ||
cf380c2f | 155 | int main(int argc, char **argv) |
ac260fd9 | 156 | { |
f69f195a MD |
157 | int err; |
158 | pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE]; | |
159 | void *tret; | |
160 | int i; | |
161 | ||
cf380c2f | 162 | if (argc < 2) { |
1430ee0b | 163 | show_usage(argc, argv); |
cf380c2f MD |
164 | return -1; |
165 | } | |
166 | ||
167 | err = sscanf(argv[1], "%lu", &duration); | |
168 | if (err != 1) { | |
1430ee0b | 169 | show_usage(argc, argv); |
cf380c2f MD |
170 | return -1; |
171 | } | |
172 | ||
173 | #ifdef DEBUG_YIELD | |
174 | for (i = 2; i < argc; i++) { | |
175 | if (argv[i][0] != '-') | |
176 | continue; | |
177 | switch (argv[i][1]) { | |
178 | case 'r': | |
179 | yield_active |= YIELD_READ; | |
180 | break; | |
181 | case 'w': | |
182 | yield_active |= YIELD_WRITE; | |
183 | break; | |
184 | } | |
185 | } | |
186 | #endif | |
187 | ||
188 | printf("running test for %lu seconds.\n", duration); | |
189 | start_time = time(NULL); | |
87bd15cd BW |
190 | printf("thread %-6s, thread id : %lx, tid %lu\n", |
191 | "main", pthread_self(), (unsigned long)gettid()); | |
192 | ||
f69f195a MD |
193 | for (i = 0; i < NR_READ; i++) { |
194 | err = pthread_create(&tid_reader[i], NULL, thr_reader, NULL); | |
195 | if (err != 0) | |
196 | exit(1); | |
197 | } | |
198 | for (i = 0; i < NR_WRITE; i++) { | |
199 | err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL); | |
200 | if (err != 0) | |
201 | exit(1); | |
202 | } | |
203 | ||
20bf310a | 204 | for (i = 0; i < NR_READ; i++) { |
f69f195a MD |
205 | err = pthread_join(tid_reader[i], &tret); |
206 | if (err != 0) | |
207 | exit(1); | |
208 | } | |
209 | for (i = 0; i < NR_WRITE; i++) { | |
210 | err = pthread_join(tid_writer[i], &tret); | |
211 | if (err != 0) | |
212 | exit(1); | |
213 | } | |
20bf310a | 214 | free(test_rcu_pointer); |
ac260fd9 | 215 | |
f69f195a | 216 | return 0; |
ac260fd9 | 217 | } |