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 MD |
79 | #ifdef DEBUG_FULL_MB |
80 | static void force_mb_all_threads(void) | |
81 | { | |
b715b99e | 82 | smp_mb(); |
bb488185 MD |
83 | } |
84 | #else | |
27b012e2 MD |
85 | static void force_mb_all_threads(void) |
86 | { | |
f69f195a | 87 | struct reader_data *index; |
27b012e2 | 88 | /* |
b715b99e | 89 | * Ask for each threads to execute a smp_mb() so we can consider the |
27b012e2 MD |
90 | * compiler barriers around rcu read lock as real memory barriers. |
91 | */ | |
92 | if (!reader_data) | |
93 | return; | |
cf380c2f | 94 | debug_yield_write(); |
27b012e2 | 95 | sig_done = 0; |
cf380c2f | 96 | debug_yield_write(); |
b715b99e | 97 | smp_mb(); /* write sig_done before sending the signals */ |
cf380c2f MD |
98 | debug_yield_write(); |
99 | for (index = reader_data; index < reader_data + num_readers; index++) { | |
f69f195a | 100 | pthread_kill(index->tid, SIGURCU); |
cf380c2f MD |
101 | debug_yield_write(); |
102 | } | |
27b012e2 MD |
103 | /* |
104 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
105 | * BUSY-LOOP. | |
106 | */ | |
107 | while (sig_done < num_readers) | |
108 | barrier(); | |
cf380c2f | 109 | debug_yield_write(); |
b715b99e | 110 | smp_mb(); /* read sig_done before ending the barrier */ |
cf380c2f | 111 | debug_yield_write(); |
27b012e2 | 112 | } |
bb488185 | 113 | #endif |
27b012e2 | 114 | |
1430ee0b | 115 | void wait_for_quiescent_state(void) |
27b012e2 | 116 | { |
f69f195a | 117 | struct reader_data *index; |
27b012e2 MD |
118 | |
119 | if (!reader_data) | |
120 | return; | |
121 | /* Wait for each thread urcu_active_readers count to become 0. | |
122 | */ | |
f69f195a | 123 | for (index = reader_data; index < reader_data + num_readers; index++) { |
27b012e2 MD |
124 | /* |
125 | * BUSY-LOOP. | |
126 | */ | |
1430ee0b | 127 | while (rcu_old_gp_ongoing(index->urcu_active_readers)) |
27b012e2 MD |
128 | barrier(); |
129 | } | |
27b012e2 MD |
130 | } |
131 | ||
9598a481 | 132 | void synchronize_rcu(void) |
2bc59bd7 | 133 | { |
9598a481 MD |
134 | /* All threads should read qparity before accessing data structure |
135 | * where new ptr points to. */ | |
136 | /* Write new ptr before changing the qparity */ | |
2bc59bd7 | 137 | force_mb_all_threads(); |
cf380c2f | 138 | debug_yield_write(); |
9598a481 MD |
139 | |
140 | internal_urcu_lock(); | |
141 | debug_yield_write(); | |
142 | ||
143 | switch_next_urcu_qparity(); /* 0 -> 1 */ | |
cf380c2f | 144 | debug_yield_write(); |
2bc59bd7 PM |
145 | |
146 | /* | |
9598a481 MD |
147 | * Must commit qparity update to memory before waiting for parity |
148 | * 0 quiescent state. Failure to do so could result in the writer | |
149 | * waiting forever while new readers are always accessing data (no | |
150 | * progress). | |
2bc59bd7 | 151 | */ |
b715b99e | 152 | smp_mb(); |
2bc59bd7 | 153 | |
9598a481 MD |
154 | /* |
155 | * Wait for previous parity to be empty of readers. | |
156 | */ | |
157 | wait_for_quiescent_state(); /* Wait readers in parity 0 */ | |
cf380c2f | 158 | debug_yield_write(); |
9598a481 MD |
159 | |
160 | /* | |
161 | * Must finish waiting for quiescent state for parity 0 before | |
162 | * committing qparity update to memory. Failure to do so could result in | |
163 | * the writer waiting forever while new readers are always accessing | |
164 | * data (no progress). | |
165 | */ | |
b715b99e | 166 | smp_mb(); |
9598a481 MD |
167 | |
168 | switch_next_urcu_qparity(); /* 1 -> 0 */ | |
cf380c2f | 169 | debug_yield_write(); |
9598a481 MD |
170 | |
171 | /* | |
172 | * Must commit qparity update to memory before waiting for parity | |
173 | * 1 quiescent state. Failure to do so could result in the writer | |
174 | * waiting forever while new readers are always accessing data (no | |
175 | * progress). | |
176 | */ | |
b715b99e | 177 | smp_mb(); |
9598a481 MD |
178 | |
179 | /* | |
180 | * Wait for previous parity to be empty of readers. | |
181 | */ | |
182 | wait_for_quiescent_state(); /* Wait readers in parity 1 */ | |
cf380c2f | 183 | debug_yield_write(); |
9598a481 | 184 | |
e7b43771 | 185 | internal_urcu_unlock(); |
cf380c2f | 186 | debug_yield_write(); |
9598a481 MD |
187 | |
188 | /* All threads should finish using the data referred to by old ptr | |
189 | * before decrementing their urcu_active_readers count */ | |
190 | /* Finish waiting for reader threads before letting the old ptr being | |
191 | * freed. */ | |
192 | force_mb_all_threads(); | |
193 | debug_yield_write(); | |
2bc59bd7 PM |
194 | } |
195 | ||
27b012e2 MD |
196 | void urcu_add_reader(pthread_t id) |
197 | { | |
f69f195a MD |
198 | struct reader_data *oldarray; |
199 | ||
27b012e2 MD |
200 | if (!reader_data) { |
201 | alloc_readers = INIT_NUM_THREADS; | |
f69f195a | 202 | num_readers = 0; |
27b012e2 MD |
203 | reader_data = |
204 | malloc(sizeof(struct reader_data) * alloc_readers); | |
27b012e2 MD |
205 | } |
206 | if (alloc_readers < num_readers + 1) { | |
27b012e2 MD |
207 | oldarray = reader_data; |
208 | reader_data = malloc(sizeof(struct reader_data) | |
209 | * (alloc_readers << 1)); | |
210 | memcpy(reader_data, oldarray, | |
211 | sizeof(struct reader_data) * alloc_readers); | |
212 | alloc_readers <<= 1; | |
213 | free(oldarray); | |
214 | } | |
215 | reader_data[num_readers].tid = id; | |
216 | /* reference to the TLS of _this_ reader thread. */ | |
1430ee0b | 217 | reader_data[num_readers].urcu_active_readers = &urcu_active_readers; |
27b012e2 MD |
218 | num_readers++; |
219 | } | |
220 | ||
221 | /* | |
222 | * Never shrink (implementation limitation). | |
223 | * This is O(nb threads). Eventually use a hash table. | |
224 | */ | |
225 | void urcu_remove_reader(pthread_t id) | |
226 | { | |
227 | struct reader_data *index; | |
228 | ||
229 | assert(reader_data != NULL); | |
230 | for (index = reader_data; index < reader_data + num_readers; index++) { | |
e6d6e2dc | 231 | if (pthread_equal(index->tid, id)) { |
27b012e2 MD |
232 | memcpy(index, &reader_data[num_readers - 1], |
233 | sizeof(struct reader_data)); | |
234 | reader_data[num_readers - 1].tid = 0; | |
235 | reader_data[num_readers - 1].urcu_active_readers = NULL; | |
236 | num_readers--; | |
237 | return; | |
238 | } | |
239 | } | |
240 | /* Hrm not found, forgot to register ? */ | |
241 | assert(0); | |
242 | } | |
243 | ||
244 | void urcu_register_thread(void) | |
245 | { | |
c265818b | 246 | internal_urcu_lock(); |
41718ff9 | 247 | urcu_add_reader(pthread_self()); |
c265818b | 248 | internal_urcu_unlock(); |
27b012e2 MD |
249 | } |
250 | ||
f69f195a | 251 | void urcu_unregister_thread(void) |
27b012e2 | 252 | { |
c265818b | 253 | internal_urcu_lock(); |
41718ff9 | 254 | urcu_remove_reader(pthread_self()); |
c265818b | 255 | internal_urcu_unlock(); |
27b012e2 MD |
256 | } |
257 | ||
bb488185 | 258 | #ifndef DEBUG_FULL_MB |
f69f195a | 259 | void sigurcu_handler(int signo, siginfo_t *siginfo, void *context) |
27b012e2 | 260 | { |
b715b99e | 261 | smp_mb(); |
27b012e2 MD |
262 | atomic_inc(&sig_done); |
263 | } | |
264 | ||
265 | void __attribute__((constructor)) urcu_init(void) | |
266 | { | |
267 | struct sigaction act; | |
268 | int ret; | |
269 | ||
270 | act.sa_sigaction = sigurcu_handler; | |
271 | ret = sigaction(SIGURCU, &act, NULL); | |
f69f195a MD |
272 | if (ret) { |
273 | perror("Error in sigaction"); | |
27b012e2 MD |
274 | exit(-1); |
275 | } | |
276 | } | |
277 | ||
278 | void __attribute__((destructor)) urcu_exit(void) | |
279 | { | |
280 | struct sigaction act; | |
281 | int ret; | |
282 | ||
283 | ret = sigaction(SIGURCU, NULL, &act); | |
f69f195a MD |
284 | if (ret) { |
285 | perror("Error in sigaction"); | |
27b012e2 MD |
286 | exit(-1); |
287 | } | |
288 | assert(act.sa_sigaction == sigurcu_handler); | |
289 | free(reader_data); | |
290 | } | |
bb488185 | 291 | #endif |