Change API
[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 int b;
42 char c[200];
43 };
44
45 static struct test_array *test_rcu_pointer;
46
47 #define NR_READ 10
48 #define NR_WRITE 9
49
50 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
51
52 void rcu_copy_mutex_lock(void)
53 {
54 int ret;
55 ret = pthread_mutex_lock(&rcu_copy_mutex);
56 if (ret) {
57 perror("Error in pthread mutex lock");
58 exit(-1);
59 }
60 }
61
62 void rcu_copy_mutex_unlock(void)
63 {
64 int ret;
65
66 ret = pthread_mutex_unlock(&rcu_copy_mutex);
67 if (ret) {
68 perror("Error in pthread mutex unlock");
69 exit(-1);
70 }
71 }
72
73 void *thr_reader(void *arg)
74 {
75 int qparity, i, j;
76 struct test_array *local_ptr;
77
78 printf("thread %s, thread id : %lx, tid %lu\n",
79 "reader", pthread_self(), (unsigned long)gettid());
80 sleep(2);
81
82 urcu_register_thread();
83
84 for (i = 0; i < 100000; i++) {
85 for (j = 0; j < 100000000; j++) {
86 rcu_read_lock(&qparity);
87 local_ptr = rcu_dereference(test_rcu_pointer);
88 if (local_ptr) {
89 assert(local_ptr->a == 8);
90 assert(local_ptr->b == 12);
91 assert(local_ptr->c[55] == 2);
92 }
93 rcu_read_unlock(&qparity);
94 }
95 }
96
97 urcu_unregister_thread();
98
99 return ((void*)1);
100
101 }
102
103 void *thr_writer(void *arg)
104 {
105 int i;
106 struct test_array *new, *old;
107
108 printf("thread %s, thread id : %lx, tid %lu\n",
109 "writer", pthread_self(), (unsigned long)gettid());
110 sleep(2);
111
112 for (i = 0; i < 10000000; i++) {
113 new = malloc(sizeof(struct test_array));
114 rcu_copy_mutex_lock();
115 old = test_rcu_pointer;
116 if (old) {
117 assert(old->a == 8);
118 assert(old->b == 12);
119 assert(old->c[55] == 2);
120 }
121 new->c[55] = 2;
122 new->b = 12;
123 new->a = 8;
124 old = urcu_publish_content((void **)&test_rcu_pointer, new);
125 rcu_copy_mutex_unlock();
126 /* can be done after unlock */
127 if (old) {
128 old->a = 0;
129 old->b = 0;
130 old->c[55] = 0;
131 }
132 free(old);
133 usleep(1);
134 }
135
136 return ((void*)2);
137 }
138
139 int main()
140 {
141 int err;
142 pthread_t tid_reader[NR_READ], tid_writer[NR_WRITE];
143 void *tret;
144 int i;
145
146 printf("thread %-6s, thread id : %lx, tid %lu\n",
147 "main", pthread_self(), (unsigned long)gettid());
148
149 for (i = 0; i < NR_READ; i++) {
150 err = pthread_create(&tid_reader[i], NULL, thr_reader, NULL);
151 if (err != 0)
152 exit(1);
153 }
154 for (i = 0; i < NR_WRITE; i++) {
155 err = pthread_create(&tid_writer[i], NULL, thr_writer, NULL);
156 if (err != 0)
157 exit(1);
158 }
159
160 sleep(10);
161
162 for (i = 0; i < NR_READ; i++) {
163 err = pthread_join(tid_reader[i], &tret);
164 if (err != 0)
165 exit(1);
166 }
167 for (i = 0; i < NR_WRITE; i++) {
168 err = pthread_join(tid_writer[i], &tret);
169 if (err != 0)
170 exit(1);
171 }
172 free(test_rcu_pointer);
173
174 return 0;
175 }
This page took 0.032518 seconds and 4 git commands to generate.