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