Fix RCU_GP_CTR_BIT
[urcu.git] / urcu.c
1 /*
2 * urcu.c
3 *
4 * Userspace RCU library
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 <signal.h>
14 #include <assert.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "urcu.h"
19
20 pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
21
22 /* Global grace period counter */
23 long urcu_gp_ctr;
24
25 long __thread urcu_active_readers;
26
27 /* Thread IDs of registered readers */
28 #define INIT_NUM_THREADS 4
29
30 struct reader_data {
31 pthread_t tid;
32 int *urcu_active_readers;
33 };
34
35 #ifdef DEBUG_YIELD
36 unsigned int yield_active;
37 unsigned int __thread rand_yield;
38 #endif
39
40 static struct reader_data *reader_data;
41 static int num_readers, alloc_readers;
42 static int sig_done;
43
44 void internal_urcu_lock(void)
45 {
46 int ret;
47 ret = pthread_mutex_lock(&urcu_mutex);
48 if (ret) {
49 perror("Error in pthread mutex lock");
50 exit(-1);
51 }
52 }
53
54 void internal_urcu_unlock(void)
55 {
56 int ret;
57
58 ret = pthread_mutex_unlock(&urcu_mutex);
59 if (ret) {
60 perror("Error in pthread mutex unlock");
61 exit(-1);
62 }
63 }
64
65 /*
66 * called with urcu_mutex held.
67 */
68 static void switch_next_urcu_qparity(void)
69 {
70 urcu_gp_ctr ^= RCU_GP_CTR_BIT;
71 }
72
73 static void force_mb_all_threads(void)
74 {
75 struct reader_data *index;
76 /*
77 * Ask for each threads to execute a mb() so we can consider the
78 * compiler barriers around rcu read lock as real memory barriers.
79 */
80 if (!reader_data)
81 return;
82 debug_yield_write();
83 sig_done = 0;
84 debug_yield_write();
85 mb(); /* write sig_done before sending the signals */
86 debug_yield_write();
87 for (index = reader_data; index < reader_data + num_readers; index++) {
88 pthread_kill(index->tid, SIGURCU);
89 debug_yield_write();
90 }
91 /*
92 * Wait for sighandler (and thus mb()) to execute on every thread.
93 * BUSY-LOOP.
94 */
95 while (sig_done < num_readers)
96 barrier();
97 debug_yield_write();
98 mb(); /* read sig_done before ending the barrier */
99 debug_yield_write();
100 }
101
102 void wait_for_quiescent_state(void)
103 {
104 struct reader_data *index;
105
106 if (!reader_data)
107 return;
108 /* Wait for each thread urcu_active_readers count to become 0.
109 */
110 for (index = reader_data; index < reader_data + num_readers; index++) {
111 /*
112 * BUSY-LOOP.
113 */
114 while (rcu_old_gp_ongoing(index->urcu_active_readers))
115 barrier();
116 }
117 /*
118 * Locally : read *index->urcu_active_readers before freeing old
119 * pointer.
120 * Remote (reader threads) : Order urcu_qparity update and other
121 * thread's quiescent state counter read.
122 */
123 force_mb_all_threads();
124 }
125
126 static void switch_qparity(void)
127 {
128 /* All threads should read qparity before accessing data structure. */
129 /* Write ptr before changing the qparity */
130 force_mb_all_threads();
131 debug_yield_write();
132 switch_next_urcu_qparity();
133 debug_yield_write();
134
135 /*
136 * Wait for previous parity to be empty of readers.
137 */
138 wait_for_quiescent_state();
139 }
140
141 void synchronize_rcu(void)
142 {
143 debug_yield_write();
144 internal_urcu_lock();
145 debug_yield_write();
146 switch_qparity();
147 debug_yield_write();
148 switch_qparity();
149 debug_yield_write();
150 internal_urcu_unlock();
151 debug_yield_write();
152 }
153
154 void urcu_add_reader(pthread_t id)
155 {
156 struct reader_data *oldarray;
157
158 if (!reader_data) {
159 alloc_readers = INIT_NUM_THREADS;
160 num_readers = 0;
161 reader_data =
162 malloc(sizeof(struct reader_data) * alloc_readers);
163 }
164 if (alloc_readers < num_readers + 1) {
165 oldarray = reader_data;
166 reader_data = malloc(sizeof(struct reader_data)
167 * (alloc_readers << 1));
168 memcpy(reader_data, oldarray,
169 sizeof(struct reader_data) * alloc_readers);
170 alloc_readers <<= 1;
171 free(oldarray);
172 }
173 reader_data[num_readers].tid = id;
174 /* reference to the TLS of _this_ reader thread. */
175 reader_data[num_readers].urcu_active_readers = &urcu_active_readers;
176 num_readers++;
177 }
178
179 /*
180 * Never shrink (implementation limitation).
181 * This is O(nb threads). Eventually use a hash table.
182 */
183 void urcu_remove_reader(pthread_t id)
184 {
185 struct reader_data *index;
186
187 assert(reader_data != NULL);
188 for (index = reader_data; index < reader_data + num_readers; index++) {
189 if (pthread_equal(index->tid, id)) {
190 memcpy(index, &reader_data[num_readers - 1],
191 sizeof(struct reader_data));
192 reader_data[num_readers - 1].tid = 0;
193 reader_data[num_readers - 1].urcu_active_readers = NULL;
194 num_readers--;
195 return;
196 }
197 }
198 /* Hrm not found, forgot to register ? */
199 assert(0);
200 }
201
202 void urcu_register_thread(void)
203 {
204 internal_urcu_lock();
205 urcu_add_reader(pthread_self());
206 internal_urcu_unlock();
207 }
208
209 void urcu_unregister_thread(void)
210 {
211 internal_urcu_lock();
212 urcu_remove_reader(pthread_self());
213 internal_urcu_unlock();
214 }
215
216 void sigurcu_handler(int signo, siginfo_t *siginfo, void *context)
217 {
218 mb();
219 atomic_inc(&sig_done);
220 }
221
222 void __attribute__((constructor)) urcu_init(void)
223 {
224 struct sigaction act;
225 int ret;
226
227 act.sa_sigaction = sigurcu_handler;
228 ret = sigaction(SIGURCU, &act, NULL);
229 if (ret) {
230 perror("Error in sigaction");
231 exit(-1);
232 }
233 }
234
235 void __attribute__((destructor)) urcu_exit(void)
236 {
237 struct sigaction act;
238 int ret;
239
240 ret = sigaction(SIGURCU, NULL, &act);
241 if (ret) {
242 perror("Error in sigaction");
243 exit(-1);
244 }
245 assert(act.sa_sigaction == sigurcu_handler);
246 free(reader_data);
247 }
This page took 0.034165 seconds and 5 git commands to generate.