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 | { | |
cf380c2f | 91 | int qparity; |
41718ff9 MD |
92 | struct test_array *local_ptr; |
93 | ||
cf380c2f | 94 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 95 | "reader", pthread_self(), (unsigned long)gettid()); |
f69f195a MD |
96 | |
97 | urcu_register_thread(); | |
98 | ||
cf380c2f MD |
99 | for (;;) { |
100 | rcu_read_lock(&qparity); | |
101 | local_ptr = rcu_dereference(test_rcu_pointer); | |
102 | if (local_ptr) | |
103 | assert(local_ptr->a == 8); | |
104 | rcu_read_unlock(&qparity); | |
105 | if (!test_duration()) | |
106 | break; | |
41718ff9 | 107 | } |
f69f195a MD |
108 | |
109 | urcu_unregister_thread(); | |
41718ff9 | 110 | |
cf380c2f MD |
111 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
112 | "reader", pthread_self(), (unsigned long)gettid()); | |
f69f195a MD |
113 | return ((void*)1); |
114 | ||
115 | } | |
116 | ||
117 | void *thr_writer(void *arg) | |
118 | { | |
41718ff9 | 119 | struct test_array *new, *old; |
f69f195a | 120 | |
cf380c2f | 121 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 122 | "writer", pthread_self(), (unsigned long)gettid()); |
f69f195a | 123 | |
cf380c2f | 124 | for (;;) { |
41718ff9 | 125 | new = malloc(sizeof(struct test_array)); |
c265818b | 126 | rcu_copy_mutex_lock(); |
41718ff9 | 127 | old = test_rcu_pointer; |
cf380c2f | 128 | if (old) |
41718ff9 | 129 | assert(old->a == 8); |
ad6ce6ae | 130 | new->a = 8; |
cdcb92bb | 131 | old = urcu_publish_content((void **)&test_rcu_pointer, new); |
c265818b | 132 | rcu_copy_mutex_unlock(); |
41718ff9 | 133 | /* can be done after unlock */ |
cf380c2f | 134 | if (old) |
8c8eed97 | 135 | old->a = 0; |
41718ff9 | 136 | free(old); |
cf380c2f MD |
137 | if (!test_duration()) |
138 | break; | |
8c8eed97 | 139 | usleep(1); |
f69f195a MD |
140 | } |
141 | ||
cf380c2f MD |
142 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
143 | "writer", pthread_self(), (unsigned long)gettid()); | |
f69f195a MD |
144 | return ((void*)2); |
145 | } | |
ac260fd9 | 146 | |
cf380c2f | 147 | int main(int argc, char **argv) |
ac260fd9 | 148 | { |
f69f195a MD |
149 | int err; |
150 | pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE]; | |
151 | void *tret; | |
152 | int i; | |
153 | ||
cf380c2f MD |
154 | if (argc < 2) { |
155 | printf("Usage : %s duration (s) [-r] [-w] " | |
156 | "(yield reader and/or writer)\n", argv[0]); | |
157 | return -1; | |
158 | } | |
159 | ||
160 | err = sscanf(argv[1], "%lu", &duration); | |
161 | if (err != 1) { | |
162 | printf("Usage : %s duration (s) [-r] [-w] " | |
163 | "(yield reader and/or writer)\n", argv[0]); | |
164 | return -1; | |
165 | } | |
166 | ||
167 | #ifdef DEBUG_YIELD | |
168 | for (i = 2; i < argc; i++) { | |
169 | if (argv[i][0] != '-') | |
170 | continue; | |
171 | switch (argv[i][1]) { | |
172 | case 'r': | |
173 | yield_active |= YIELD_READ; | |
174 | break; | |
175 | case 'w': | |
176 | yield_active |= YIELD_WRITE; | |
177 | break; | |
178 | } | |
179 | } | |
180 | #endif | |
181 | ||
182 | printf("running test for %lu seconds.\n", duration); | |
183 | start_time = time(NULL); | |
87bd15cd BW |
184 | printf("thread %-6s, thread id : %lx, tid %lu\n", |
185 | "main", pthread_self(), (unsigned long)gettid()); | |
186 | ||
f69f195a MD |
187 | for (i = 0; i < NR_READ; i++) { |
188 | err = pthread_create(&tid_reader[i], NULL, thr_reader, NULL); | |
189 | if (err != 0) | |
190 | exit(1); | |
191 | } | |
192 | for (i = 0; i < NR_WRITE; i++) { | |
193 | err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL); | |
194 | if (err != 0) | |
195 | exit(1); | |
196 | } | |
197 | ||
20bf310a | 198 | for (i = 0; i < NR_READ; i++) { |
f69f195a MD |
199 | err = pthread_join(tid_reader[i], &tret); |
200 | if (err != 0) | |
201 | exit(1); | |
202 | } | |
203 | for (i = 0; i < NR_WRITE; i++) { | |
204 | err = pthread_join(tid_writer[i], &tret); | |
205 | if (err != 0) | |
206 | exit(1); | |
207 | } | |
20bf310a | 208 | free(test_rcu_pointer); |
ac260fd9 | 209 | |
f69f195a | 210 | return 0; |
ac260fd9 | 211 | } |