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