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