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 | ||
22 | /* Global quiescent period parity */ | |
23 | int urcu_qparity; | |
24 | ||
25 | int __thread urcu_active_readers[2]; | |
26 | ||
27 | /* Thread IDs of registered readers */ | |
28 | #define INIT_NUM_THREADS 4 | |
29 | ||
30 | struct reader_data { | |
31 | pthread_t tid; | |
f69f195a | 32 | int *urcu_active_readers; |
27b012e2 MD |
33 | }; |
34 | ||
cf380c2f | 35 | #ifdef DEBUG_YIELD |
9d335088 MD |
36 | unsigned int yield_active; |
37 | unsigned int __thread rand_yield; | |
cf380c2f MD |
38 | #endif |
39 | ||
27b012e2 MD |
40 | static struct reader_data *reader_data; |
41 | static int num_readers, alloc_readers; | |
42 | static int sig_done; | |
43 | ||
c265818b | 44 | void internal_urcu_lock(void) |
41718ff9 MD |
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 | ||
c265818b | 54 | void internal_urcu_unlock(void) |
41718ff9 MD |
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 | ||
27b012e2 MD |
65 | /* |
66 | * called with urcu_mutex held. | |
67 | */ | |
68 | static int switch_next_urcu_qparity(void) | |
69 | { | |
70 | int old_parity = urcu_qparity; | |
71 | urcu_qparity = 1 - old_parity; | |
72 | return old_parity; | |
73 | } | |
74 | ||
75 | static void force_mb_all_threads(void) | |
76 | { | |
f69f195a | 77 | struct reader_data *index; |
27b012e2 MD |
78 | /* |
79 | * Ask for each threads to execute a mb() so we can consider the | |
80 | * compiler barriers around rcu read lock as real memory barriers. | |
81 | */ | |
82 | if (!reader_data) | |
83 | return; | |
cf380c2f | 84 | debug_yield_write(); |
27b012e2 | 85 | sig_done = 0; |
cf380c2f | 86 | debug_yield_write(); |
f69f195a | 87 | mb(); /* write sig_done before sending the signals */ |
cf380c2f MD |
88 | debug_yield_write(); |
89 | for (index = reader_data; index < reader_data + num_readers; index++) { | |
f69f195a | 90 | pthread_kill(index->tid, SIGURCU); |
cf380c2f MD |
91 | debug_yield_write(); |
92 | } | |
27b012e2 MD |
93 | /* |
94 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
95 | * BUSY-LOOP. | |
96 | */ | |
97 | while (sig_done < num_readers) | |
98 | barrier(); | |
cf380c2f | 99 | debug_yield_write(); |
f69f195a | 100 | mb(); /* read sig_done before ending the barrier */ |
cf380c2f | 101 | debug_yield_write(); |
27b012e2 MD |
102 | } |
103 | ||
104 | void wait_for_quiescent_state(int parity) | |
105 | { | |
f69f195a | 106 | struct reader_data *index; |
27b012e2 MD |
107 | |
108 | if (!reader_data) | |
109 | return; | |
110 | /* Wait for each thread urcu_active_readers count to become 0. | |
111 | */ | |
f69f195a | 112 | for (index = reader_data; index < reader_data + num_readers; index++) { |
27b012e2 MD |
113 | /* |
114 | * BUSY-LOOP. | |
115 | */ | |
f858d07a | 116 | while (index->urcu_active_readers[parity] != 0) |
27b012e2 MD |
117 | barrier(); |
118 | } | |
119 | /* | |
120 | * Locally : read *index->urcu_active_readers before freeing old | |
121 | * pointer. | |
122 | * Remote (reader threads) : Order urcu_qparity update and other | |
123 | * thread's quiescent state counter read. | |
124 | */ | |
125 | force_mb_all_threads(); | |
126 | } | |
127 | ||
2bc59bd7 PM |
128 | static void switch_qparity(void) |
129 | { | |
130 | int prev_parity; | |
131 | ||
132 | /* All threads should read qparity before accessing data structure. */ | |
133 | /* Write ptr before changing the qparity */ | |
134 | force_mb_all_threads(); | |
cf380c2f | 135 | debug_yield_write(); |
2bc59bd7 | 136 | prev_parity = switch_next_urcu_qparity(); |
cf380c2f | 137 | debug_yield_write(); |
2bc59bd7 PM |
138 | |
139 | /* | |
140 | * Wait for previous parity to be empty of readers. | |
141 | */ | |
142 | wait_for_quiescent_state(prev_parity); | |
143 | } | |
144 | ||
145 | void synchronize_rcu(void) | |
146 | { | |
cf380c2f | 147 | debug_yield_write(); |
c265818b | 148 | internal_urcu_lock(); |
cf380c2f | 149 | debug_yield_write(); |
2bc59bd7 | 150 | switch_qparity(); |
cf380c2f | 151 | debug_yield_write(); |
2bc59bd7 | 152 | switch_qparity(); |
cf380c2f | 153 | debug_yield_write(); |
c265818b | 154 | internal_urcu_lock(); |
cf380c2f | 155 | debug_yield_write(); |
2bc59bd7 PM |
156 | } |
157 | ||
27b012e2 MD |
158 | /* |
159 | * Return old pointer, OK to free, no more reference exist. | |
41718ff9 | 160 | * Called under rcu_write_lock. |
27b012e2 | 161 | */ |
cdcb92bb | 162 | void *urcu_publish_content(void **ptr, void *new) |
27b012e2 | 163 | { |
27b012e2 MD |
164 | void *oldptr; |
165 | ||
cf380c2f | 166 | debug_yield_write(); |
c265818b | 167 | internal_urcu_lock(); |
cf380c2f | 168 | debug_yield_write(); |
27b012e2 MD |
169 | /* |
170 | * We can publish the new pointer before we change the current qparity. | |
171 | * Readers seeing the new pointer while being in the previous qparity | |
172 | * window will make us wait until the end of the quiescent state before | |
173 | * we release the unrelated memory area. However, given we hold the | |
174 | * urcu_mutex, we are making sure that no further garbage collection can | |
175 | * occur until we release the mutex, therefore we guarantee that this | |
176 | * given reader will have completed its execution using the new pointer | |
177 | * when the next quiescent state window will be over. | |
178 | */ | |
179 | oldptr = *ptr; | |
cf380c2f | 180 | debug_yield_write(); |
27b012e2 | 181 | *ptr = new; |
27b012e2 | 182 | |
cf380c2f | 183 | debug_yield_write(); |
2bc59bd7 | 184 | switch_qparity(); |
cf380c2f | 185 | debug_yield_write(); |
2bc59bd7 | 186 | switch_qparity(); |
cf380c2f | 187 | debug_yield_write(); |
c265818b | 188 | internal_urcu_unlock(); |
cf380c2f | 189 | debug_yield_write(); |
2bc59bd7 | 190 | |
27b012e2 MD |
191 | return oldptr; |
192 | } | |
193 | ||
194 | void urcu_add_reader(pthread_t id) | |
195 | { | |
f69f195a MD |
196 | struct reader_data *oldarray; |
197 | ||
27b012e2 MD |
198 | if (!reader_data) { |
199 | alloc_readers = INIT_NUM_THREADS; | |
f69f195a | 200 | num_readers = 0; |
27b012e2 MD |
201 | reader_data = |
202 | malloc(sizeof(struct reader_data) * alloc_readers); | |
27b012e2 MD |
203 | } |
204 | if (alloc_readers < num_readers + 1) { | |
27b012e2 MD |
205 | oldarray = reader_data; |
206 | reader_data = malloc(sizeof(struct reader_data) | |
207 | * (alloc_readers << 1)); | |
208 | memcpy(reader_data, oldarray, | |
209 | sizeof(struct reader_data) * alloc_readers); | |
210 | alloc_readers <<= 1; | |
211 | free(oldarray); | |
212 | } | |
213 | reader_data[num_readers].tid = id; | |
214 | /* reference to the TLS of _this_ reader thread. */ | |
215 | reader_data[num_readers].urcu_active_readers = urcu_active_readers; | |
216 | num_readers++; | |
217 | } | |
218 | ||
219 | /* | |
220 | * Never shrink (implementation limitation). | |
221 | * This is O(nb threads). Eventually use a hash table. | |
222 | */ | |
223 | void urcu_remove_reader(pthread_t id) | |
224 | { | |
225 | struct reader_data *index; | |
226 | ||
227 | assert(reader_data != NULL); | |
228 | for (index = reader_data; index < reader_data + num_readers; index++) { | |
e6d6e2dc | 229 | if (pthread_equal(index->tid, id)) { |
27b012e2 MD |
230 | memcpy(index, &reader_data[num_readers - 1], |
231 | sizeof(struct reader_data)); | |
232 | reader_data[num_readers - 1].tid = 0; | |
233 | reader_data[num_readers - 1].urcu_active_readers = NULL; | |
234 | num_readers--; | |
235 | return; | |
236 | } | |
237 | } | |
238 | /* Hrm not found, forgot to register ? */ | |
239 | assert(0); | |
240 | } | |
241 | ||
242 | void urcu_register_thread(void) | |
243 | { | |
c265818b | 244 | internal_urcu_lock(); |
41718ff9 | 245 | urcu_add_reader(pthread_self()); |
c265818b | 246 | internal_urcu_unlock(); |
27b012e2 MD |
247 | } |
248 | ||
f69f195a | 249 | void urcu_unregister_thread(void) |
27b012e2 | 250 | { |
c265818b | 251 | internal_urcu_lock(); |
41718ff9 | 252 | urcu_remove_reader(pthread_self()); |
c265818b | 253 | internal_urcu_unlock(); |
27b012e2 MD |
254 | } |
255 | ||
f69f195a | 256 | void sigurcu_handler(int signo, siginfo_t *siginfo, void *context) |
27b012e2 MD |
257 | { |
258 | mb(); | |
259 | atomic_inc(&sig_done); | |
260 | } | |
261 | ||
262 | void __attribute__((constructor)) urcu_init(void) | |
263 | { | |
264 | struct sigaction act; | |
265 | int ret; | |
266 | ||
267 | act.sa_sigaction = sigurcu_handler; | |
268 | ret = sigaction(SIGURCU, &act, NULL); | |
f69f195a MD |
269 | if (ret) { |
270 | perror("Error in sigaction"); | |
27b012e2 MD |
271 | exit(-1); |
272 | } | |
273 | } | |
274 | ||
275 | void __attribute__((destructor)) urcu_exit(void) | |
276 | { | |
277 | struct sigaction act; | |
278 | int ret; | |
279 | ||
280 | ret = sigaction(SIGURCU, NULL, &act); | |
f69f195a MD |
281 | if (ret) { |
282 | perror("Error in sigaction"); | |
27b012e2 MD |
283 | exit(-1); |
284 | } | |
285 | assert(act.sa_sigaction == sigurcu_handler); | |
286 | free(reader_data); | |
287 | } |