Commit | Line | Data |
---|---|---|
b257a10b MD |
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 | ||
27b012e2 MD |
11 | #include <stdio.h> |
12 | #include <pthread.h> | |
13 | #include <signal.h> | |
14 | #include <assert.h> | |
f69f195a MD |
15 | #include <stdlib.h> |
16 | #include <string.h> | |
27b012e2 MD |
17 | |
18 | #include "urcu.h" | |
19 | ||
20 | pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER; | |
21 | ||
128166c9 MD |
22 | /* |
23 | * Global grace period counter. | |
24 | * Contains the current RCU_GP_CTR_BIT. | |
25 | * Also has a RCU_GP_CTR_BIT of 1, to accelerate the reader fast path. | |
26 | */ | |
27 | long urcu_gp_ctr = RCU_GP_COUNT; | |
27b012e2 | 28 | |
6e8b8429 | 29 | long __thread urcu_active_readers; |
27b012e2 MD |
30 | |
31 | /* Thread IDs of registered readers */ | |
32 | #define INIT_NUM_THREADS 4 | |
33 | ||
34 | struct reader_data { | |
35 | pthread_t tid; | |
128166c9 | 36 | long *urcu_active_readers; |
27b012e2 MD |
37 | }; |
38 | ||
cf380c2f | 39 | #ifdef DEBUG_YIELD |
9d335088 MD |
40 | unsigned int yield_active; |
41 | unsigned int __thread rand_yield; | |
cf380c2f MD |
42 | #endif |
43 | ||
27b012e2 MD |
44 | static struct reader_data *reader_data; |
45 | static int num_readers, alloc_readers; | |
b715b99e | 46 | #ifndef DEBUG_FULL_MB |
27b012e2 | 47 | static int sig_done; |
b715b99e | 48 | #endif |
27b012e2 | 49 | |
c265818b | 50 | void internal_urcu_lock(void) |
41718ff9 MD |
51 | { |
52 | int ret; | |
53 | ret = pthread_mutex_lock(&urcu_mutex); | |
54 | if (ret) { | |
55 | perror("Error in pthread mutex lock"); | |
56 | exit(-1); | |
57 | } | |
58 | } | |
59 | ||
c265818b | 60 | void internal_urcu_unlock(void) |
41718ff9 MD |
61 | { |
62 | int ret; | |
63 | ||
64 | ret = pthread_mutex_unlock(&urcu_mutex); | |
65 | if (ret) { | |
66 | perror("Error in pthread mutex unlock"); | |
67 | exit(-1); | |
68 | } | |
69 | } | |
70 | ||
27b012e2 MD |
71 | /* |
72 | * called with urcu_mutex held. | |
73 | */ | |
1430ee0b | 74 | static void switch_next_urcu_qparity(void) |
27b012e2 | 75 | { |
1430ee0b | 76 | urcu_gp_ctr ^= RCU_GP_CTR_BIT; |
27b012e2 MD |
77 | } |
78 | ||
bb488185 | 79 | #ifdef DEBUG_FULL_MB |
40e140c9 MD |
80 | static void force_mb_single_thread(pthread_t tid) |
81 | { | |
82 | smp_mb(); | |
83 | } | |
84 | ||
bb488185 MD |
85 | static void force_mb_all_threads(void) |
86 | { | |
b715b99e | 87 | smp_mb(); |
bb488185 MD |
88 | } |
89 | #else | |
40e140c9 MD |
90 | |
91 | static void force_mb_single_thread(pthread_t tid) | |
92 | { | |
93 | assert(reader_data); | |
94 | sig_done = 0; | |
95 | smp_mb(); /* write sig_done before sending the signals */ | |
96 | pthread_kill(tid, SIGURCU); | |
97 | /* | |
98 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
99 | * BUSY-LOOP. | |
100 | */ | |
101 | while (sig_done < 1) | |
102 | smp_rmb(); /* ensure we re-read sig-done */ | |
103 | smp_mb(); /* read sig_done before ending the barrier */ | |
104 | } | |
105 | ||
27b012e2 MD |
106 | static void force_mb_all_threads(void) |
107 | { | |
f69f195a | 108 | struct reader_data *index; |
27b012e2 | 109 | /* |
b715b99e | 110 | * Ask for each threads to execute a smp_mb() so we can consider the |
27b012e2 MD |
111 | * compiler barriers around rcu read lock as real memory barriers. |
112 | */ | |
113 | if (!reader_data) | |
114 | return; | |
27b012e2 | 115 | sig_done = 0; |
b715b99e | 116 | smp_mb(); /* write sig_done before sending the signals */ |
ae878d0d | 117 | for (index = reader_data; index < reader_data + num_readers; index++) |
f69f195a | 118 | pthread_kill(index->tid, SIGURCU); |
27b012e2 MD |
119 | /* |
120 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
121 | * BUSY-LOOP. | |
122 | */ | |
123 | while (sig_done < num_readers) | |
40e140c9 | 124 | smp_rmb(); /* ensure we re-read sig-done */ |
b715b99e | 125 | smp_mb(); /* read sig_done before ending the barrier */ |
27b012e2 | 126 | } |
bb488185 | 127 | #endif |
27b012e2 | 128 | |
1430ee0b | 129 | void wait_for_quiescent_state(void) |
27b012e2 | 130 | { |
f69f195a | 131 | struct reader_data *index; |
27b012e2 MD |
132 | |
133 | if (!reader_data) | |
134 | return; | |
40e140c9 MD |
135 | /* |
136 | * Wait for each thread urcu_active_readers count to become 0. | |
27b012e2 | 137 | */ |
f69f195a | 138 | for (index = reader_data; index < reader_data + num_readers; index++) { |
40e140c9 | 139 | int wait_loops = 0; |
27b012e2 | 140 | /* |
40e140c9 MD |
141 | * BUSY-LOOP. Force the reader thread to commit its |
142 | * urcu_active_readers update to memory if we wait for too long. | |
27b012e2 | 143 | */ |
40e140c9 MD |
144 | while (rcu_old_gp_ongoing(index->urcu_active_readers)) { |
145 | if (wait_loops++ == KICK_READER_LOOPS) { | |
146 | force_mb_single_thread(index->tid); | |
147 | wait_loops = 0; | |
148 | } | |
149 | } | |
27b012e2 | 150 | } |
27b012e2 MD |
151 | } |
152 | ||
9598a481 | 153 | void synchronize_rcu(void) |
2bc59bd7 | 154 | { |
9598a481 MD |
155 | /* All threads should read qparity before accessing data structure |
156 | * where new ptr points to. */ | |
157 | /* Write new ptr before changing the qparity */ | |
2bc59bd7 | 158 | force_mb_all_threads(); |
9598a481 MD |
159 | |
160 | internal_urcu_lock(); | |
9598a481 MD |
161 | |
162 | switch_next_urcu_qparity(); /* 0 -> 1 */ | |
2bc59bd7 PM |
163 | |
164 | /* | |
9598a481 MD |
165 | * Must commit qparity update to memory before waiting for parity |
166 | * 0 quiescent state. Failure to do so could result in the writer | |
167 | * waiting forever while new readers are always accessing data (no | |
168 | * progress). | |
2bc59bd7 | 169 | */ |
b715b99e | 170 | smp_mb(); |
2bc59bd7 | 171 | |
9598a481 MD |
172 | /* |
173 | * Wait for previous parity to be empty of readers. | |
174 | */ | |
175 | wait_for_quiescent_state(); /* Wait readers in parity 0 */ | |
9598a481 MD |
176 | |
177 | /* | |
178 | * Must finish waiting for quiescent state for parity 0 before | |
179 | * committing qparity update to memory. Failure to do so could result in | |
180 | * the writer waiting forever while new readers are always accessing | |
181 | * data (no progress). | |
182 | */ | |
b715b99e | 183 | smp_mb(); |
9598a481 MD |
184 | |
185 | switch_next_urcu_qparity(); /* 1 -> 0 */ | |
9598a481 MD |
186 | |
187 | /* | |
188 | * Must commit qparity update to memory before waiting for parity | |
189 | * 1 quiescent state. Failure to do so could result in the writer | |
190 | * waiting forever while new readers are always accessing data (no | |
191 | * progress). | |
192 | */ | |
b715b99e | 193 | smp_mb(); |
9598a481 MD |
194 | |
195 | /* | |
196 | * Wait for previous parity to be empty of readers. | |
197 | */ | |
198 | wait_for_quiescent_state(); /* Wait readers in parity 1 */ | |
9598a481 | 199 | |
e7b43771 | 200 | internal_urcu_unlock(); |
9598a481 MD |
201 | |
202 | /* All threads should finish using the data referred to by old ptr | |
203 | * before decrementing their urcu_active_readers count */ | |
204 | /* Finish waiting for reader threads before letting the old ptr being | |
205 | * freed. */ | |
206 | force_mb_all_threads(); | |
2bc59bd7 PM |
207 | } |
208 | ||
27b012e2 MD |
209 | void urcu_add_reader(pthread_t id) |
210 | { | |
f69f195a MD |
211 | struct reader_data *oldarray; |
212 | ||
27b012e2 MD |
213 | if (!reader_data) { |
214 | alloc_readers = INIT_NUM_THREADS; | |
f69f195a | 215 | num_readers = 0; |
27b012e2 MD |
216 | reader_data = |
217 | malloc(sizeof(struct reader_data) * alloc_readers); | |
27b012e2 MD |
218 | } |
219 | if (alloc_readers < num_readers + 1) { | |
27b012e2 MD |
220 | oldarray = reader_data; |
221 | reader_data = malloc(sizeof(struct reader_data) | |
222 | * (alloc_readers << 1)); | |
223 | memcpy(reader_data, oldarray, | |
224 | sizeof(struct reader_data) * alloc_readers); | |
225 | alloc_readers <<= 1; | |
226 | free(oldarray); | |
227 | } | |
228 | reader_data[num_readers].tid = id; | |
229 | /* reference to the TLS of _this_ reader thread. */ | |
1430ee0b | 230 | reader_data[num_readers].urcu_active_readers = &urcu_active_readers; |
27b012e2 MD |
231 | num_readers++; |
232 | } | |
233 | ||
234 | /* | |
235 | * Never shrink (implementation limitation). | |
236 | * This is O(nb threads). Eventually use a hash table. | |
237 | */ | |
238 | void urcu_remove_reader(pthread_t id) | |
239 | { | |
240 | struct reader_data *index; | |
241 | ||
242 | assert(reader_data != NULL); | |
243 | for (index = reader_data; index < reader_data + num_readers; index++) { | |
e6d6e2dc | 244 | if (pthread_equal(index->tid, id)) { |
27b012e2 MD |
245 | memcpy(index, &reader_data[num_readers - 1], |
246 | sizeof(struct reader_data)); | |
247 | reader_data[num_readers - 1].tid = 0; | |
248 | reader_data[num_readers - 1].urcu_active_readers = NULL; | |
249 | num_readers--; | |
250 | return; | |
251 | } | |
252 | } | |
253 | /* Hrm not found, forgot to register ? */ | |
254 | assert(0); | |
255 | } | |
256 | ||
257 | void urcu_register_thread(void) | |
258 | { | |
c265818b | 259 | internal_urcu_lock(); |
41718ff9 | 260 | urcu_add_reader(pthread_self()); |
c265818b | 261 | internal_urcu_unlock(); |
27b012e2 MD |
262 | } |
263 | ||
f69f195a | 264 | void urcu_unregister_thread(void) |
27b012e2 | 265 | { |
c265818b | 266 | internal_urcu_lock(); |
41718ff9 | 267 | urcu_remove_reader(pthread_self()); |
c265818b | 268 | internal_urcu_unlock(); |
27b012e2 MD |
269 | } |
270 | ||
bb488185 | 271 | #ifndef DEBUG_FULL_MB |
f69f195a | 272 | void sigurcu_handler(int signo, siginfo_t *siginfo, void *context) |
27b012e2 | 273 | { |
40e140c9 MD |
274 | /* |
275 | * Executing this smp_mb() is the only purpose of this signal handler. | |
276 | * It punctually promotes barrier() into smp_mb() on every thread it is | |
277 | * executed on. | |
278 | */ | |
b715b99e | 279 | smp_mb(); |
27b012e2 MD |
280 | atomic_inc(&sig_done); |
281 | } | |
282 | ||
283 | void __attribute__((constructor)) urcu_init(void) | |
284 | { | |
285 | struct sigaction act; | |
286 | int ret; | |
287 | ||
288 | act.sa_sigaction = sigurcu_handler; | |
289 | ret = sigaction(SIGURCU, &act, NULL); | |
f69f195a MD |
290 | if (ret) { |
291 | perror("Error in sigaction"); | |
27b012e2 MD |
292 | exit(-1); |
293 | } | |
294 | } | |
295 | ||
296 | void __attribute__((destructor)) urcu_exit(void) | |
297 | { | |
298 | struct sigaction act; | |
299 | int ret; | |
300 | ||
301 | ret = sigaction(SIGURCU, NULL, &act); | |
f69f195a MD |
302 | if (ret) { |
303 | perror("Error in sigaction"); | |
27b012e2 MD |
304 | exit(-1); |
305 | } | |
306 | assert(act.sa_sigaction == sigurcu_handler); | |
307 | free(reader_data); | |
308 | } | |
bb488185 | 309 | #endif |