Fix int->long and keep a reader count of 1 in the global GP variable
[urcu.git] / test_urcu.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 * Distributed under GPLv2
9 */
10
11 #include <stdio.h>
12 #include <pthread.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <assert.h>
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
37 #include "urcu.h"
38
39 struct test_array {
40 int a;
41 };
42
43 static struct test_array *test_rcu_pointer;
44
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
63 #define NR_READ 10
64 #define NR_WRITE 9
65
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 }
88
89 void *thr_reader(void *arg)
90 {
91 struct test_array *local_ptr;
92
93 printf("thread_begin %s, thread id : %lx, tid %lu\n",
94 "reader", pthread_self(), (unsigned long)gettid());
95
96 urcu_register_thread();
97
98 for (;;) {
99 rcu_read_lock();
100 local_ptr = rcu_dereference(test_rcu_pointer);
101 if (local_ptr)
102 assert(local_ptr->a == 8);
103 rcu_read_unlock();
104 if (!test_duration())
105 break;
106 }
107
108 urcu_unregister_thread();
109
110 printf("thread_end %s, thread id : %lx, tid %lu\n",
111 "reader", pthread_self(), (unsigned long)gettid());
112 return ((void*)1);
113
114 }
115
116 void *thr_writer(void *arg)
117 {
118 struct test_array *new, *old;
119
120 printf("thread_begin %s, thread id : %lx, tid %lu\n",
121 "writer", pthread_self(), (unsigned long)gettid());
122
123 for (;;) {
124 new = malloc(sizeof(struct test_array));
125 rcu_copy_mutex_lock();
126 old = test_rcu_pointer;
127 if (old)
128 assert(old->a == 8);
129 new->a = 8;
130 old = urcu_publish_content(&test_rcu_pointer, new);
131 rcu_copy_mutex_unlock();
132 /* can be done after unlock */
133 if (old)
134 old->a = 0;
135 free(old);
136 if (!test_duration())
137 break;
138 usleep(1);
139 }
140
141 printf("thread_end %s, thread id : %lx, tid %lu\n",
142 "writer", pthread_self(), (unsigned long)gettid());
143 return ((void*)2);
144 }
145
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
155 int main(int argc, char **argv)
156 {
157 int err;
158 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
159 void *tret;
160 int i;
161
162 if (argc < 2) {
163 show_usage(argc, argv);
164 return -1;
165 }
166
167 err = sscanf(argv[1], "%lu", &duration);
168 if (err != 1) {
169 show_usage(argc, argv);
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);
190 printf("thread %-6s, thread id : %lx, tid %lu\n",
191 "main", pthread_self(), (unsigned long)gettid());
192
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
204 for (i = 0; i < NR_READ; i++) {
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 }
214 free(test_rcu_pointer);
215
216 return 0;
217 }
This page took 0.037658 seconds and 4 git commands to generate.