4 * Userspace RCU QSBR library
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
38 #include "urcu/wfcqueue.h"
39 #include "urcu/map/urcu-qsbr.h"
40 #define BUILD_QSBR_LIB
41 #include "urcu/static/urcu-qsbr.h"
42 #include "urcu-pointer.h"
43 #include "urcu/tls-compat.h"
46 #include "urcu-wait.h"
48 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
50 #include "urcu-qsbr.h"
53 void __attribute__((destructor
)) rcu_exit(void);
55 static pthread_mutex_t rcu_gp_lock
= PTHREAD_MUTEX_INITIALIZER
;
60 * Global grace period counter.
62 unsigned long rcu_gp_ctr
= RCU_GP_ONLINE
;
65 * Active attempts to check for reader Q.S. before calling futex().
67 #define RCU_QS_ACTIVE_ATTEMPTS 100
70 * Written to only by each individual reader. Read by both the reader and the
73 DEFINE_URCU_TLS(struct rcu_reader
, rcu_reader
);
76 unsigned int rcu_yield_active
;
77 DEFINE_URCU_TLS(unsigned int, rcu_rand_yield
);
80 static CDS_LIST_HEAD(registry
);
83 * Queue keeping threads awaiting to wait for a grace period. Contains
84 * struct gp_waiters_thread objects.
86 static DEFINE_URCU_WAIT_QUEUE(gp_waiters
);
88 static void mutex_lock(pthread_mutex_t
*mutex
)
92 #ifndef DISTRUST_SIGNALS_EXTREME
93 ret
= pthread_mutex_lock(mutex
);
96 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
97 while ((ret
= pthread_mutex_trylock(mutex
)) != 0) {
98 if (ret
!= EBUSY
&& ret
!= EINTR
)
102 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
105 static void mutex_unlock(pthread_mutex_t
*mutex
)
109 ret
= pthread_mutex_unlock(mutex
);
115 * synchronize_rcu() waiting. Single thread.
117 static void wait_gp(void)
119 /* Read reader_gp before read futex */
121 if (uatomic_read(&rcu_gp_futex
) == -1)
122 futex_noasync(&rcu_gp_futex
, FUTEX_WAIT
, -1,
126 static void wait_for_readers(struct cds_list_head
*input_readers
,
127 struct cds_list_head
*cur_snap_readers
,
128 struct cds_list_head
*qsreaders
)
131 struct rcu_reader
*index
, *tmp
;
134 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
135 * indicate quiescence (offline), or for them to observe the
136 * current rcu_gp_ctr value.
140 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
141 uatomic_set(&rcu_gp_futex
, -1);
143 * Write futex before write waiting (the other side
144 * reads them in the opposite order).
147 cds_list_for_each_entry(index
, input_readers
, node
) {
148 _CMM_STORE_SHARED(index
->waiting
, 1);
150 /* Write futex before read reader_gp */
153 cds_list_for_each_entry_safe(index
, tmp
, input_readers
, node
) {
154 switch (rcu_reader_state(&index
->ctr
)) {
155 case RCU_READER_ACTIVE_CURRENT
:
156 if (cur_snap_readers
) {
157 cds_list_move(&index
->node
,
162 case RCU_READER_INACTIVE
:
163 cds_list_move(&index
->node
, qsreaders
);
165 case RCU_READER_ACTIVE_OLD
:
167 * Old snapshot. Leaving node in
168 * input_readers will make us busy-loop
169 * until the snapshot becomes current or
170 * the reader becomes inactive.
176 if (cds_list_empty(input_readers
)) {
177 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
178 /* Read reader_gp before write futex */
180 uatomic_set(&rcu_gp_futex
, 0);
184 if (wait_loops
>= RCU_QS_ACTIVE_ATTEMPTS
) {
187 #ifndef HAS_INCOHERENT_CACHES
189 #else /* #ifndef HAS_INCOHERENT_CACHES */
191 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
198 * Using a two-subphases algorithm for architectures with smaller than 64-bit
199 * long-size to ensure we do not encounter an overflow bug.
202 #if (CAA_BITS_PER_LONG < 64)
203 void synchronize_rcu(void)
205 CDS_LIST_HEAD(cur_snap_readers
);
206 CDS_LIST_HEAD(qsreaders
);
207 unsigned long was_online
;
208 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
209 struct urcu_waiters waiters
;
211 was_online
= URCU_TLS(rcu_reader
).ctr
;
213 /* All threads should read qparity before accessing data structure
214 * where new ptr points to. In the "then" case, rcu_thread_offline
215 * includes a memory barrier.
217 * Mark the writer thread offline to make sure we don't wait for
218 * our own quiescent state. This allows using synchronize_rcu()
219 * in threads registered as readers.
222 rcu_thread_offline();
227 * Add ourself to gp_waiters queue of threads awaiting to wait
228 * for a grace period. Proceed to perform the grace period only
229 * if we are the first thread added into the queue.
231 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
232 /* Not first in queue: will be awakened by another thread. */
233 urcu_adaptative_busy_wait(&wait
);
236 /* We won't need to wake ourself up */
237 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
239 mutex_lock(&rcu_gp_lock
);
242 * Move all waiters into our local queue.
244 urcu_move_waiters(&waiters
, &gp_waiters
);
246 if (cds_list_empty(®istry
))
250 * Wait for readers to observe original parity or be quiescent.
252 wait_for_readers(®istry
, &cur_snap_readers
, &qsreaders
);
255 * Must finish waiting for quiescent state for original parity
256 * before committing next rcu_gp_ctr update to memory. Failure
257 * to do so could result in the writer waiting forever while new
258 * readers are always accessing data (no progress). Enforce
259 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
265 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
266 * model easier to understand. It does not have a big performance impact
267 * anyway, given this is the write-side.
271 /* Switch parity: 0 -> 1, 1 -> 0 */
272 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
275 * Must commit rcu_gp_ctr update to memory before waiting for
276 * quiescent state. Failure to do so could result in the writer
277 * waiting forever while new readers are always accessing data
278 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
279 * before load URCU_TLS(rcu_reader).ctr.
284 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
285 * model easier to understand. It does not have a big performance impact
286 * anyway, given this is the write-side.
291 * Wait for readers to observe new parity or be quiescent.
293 wait_for_readers(&cur_snap_readers
, NULL
, &qsreaders
);
296 * Put quiescent reader list back into registry.
298 cds_list_splice(&qsreaders
, ®istry
);
300 mutex_unlock(&rcu_gp_lock
);
301 urcu_wake_all_waiters(&waiters
);
304 * Finish waiting for reader threads before letting the old ptr being
312 #else /* !(CAA_BITS_PER_LONG < 64) */
313 void synchronize_rcu(void)
315 CDS_LIST_HEAD(qsreaders
);
316 unsigned long was_online
;
317 DEFINE_URCU_WAIT_NODE(wait
, URCU_WAIT_WAITING
);
318 struct urcu_waiters waiters
;
320 was_online
= URCU_TLS(rcu_reader
).ctr
;
323 * Mark the writer thread offline to make sure we don't wait for
324 * our own quiescent state. This allows using synchronize_rcu()
325 * in threads registered as readers.
328 rcu_thread_offline();
333 * Add ourself to gp_waiters queue of threads awaiting to wait
334 * for a grace period. Proceed to perform the grace period only
335 * if we are the first thread added into the queue.
337 if (urcu_wait_add(&gp_waiters
, &wait
) != 0) {
338 /* Not first in queue: will be awakened by another thread. */
339 urcu_adaptative_busy_wait(&wait
);
342 /* We won't need to wake ourself up */
343 urcu_wait_set_state(&wait
, URCU_WAIT_RUNNING
);
345 mutex_lock(&rcu_gp_lock
);
348 * Move all waiters into our local queue.
350 urcu_move_waiters(&waiters
, &gp_waiters
);
352 if (cds_list_empty(®istry
))
355 /* Increment current G.P. */
356 CMM_STORE_SHARED(rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
359 * Must commit rcu_gp_ctr update to memory before waiting for
360 * quiescent state. Failure to do so could result in the writer
361 * waiting forever while new readers are always accessing data
362 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
363 * before load URCU_TLS(rcu_reader).ctr.
368 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
369 * model easier to understand. It does not have a big performance impact
370 * anyway, given this is the write-side.
375 * Wait for readers to observe new count of be quiescent.
377 wait_for_readers(®istry
, NULL
, &qsreaders
);
380 * Put quiescent reader list back into registry.
382 cds_list_splice(&qsreaders
, ®istry
);
384 mutex_unlock(&rcu_gp_lock
);
385 urcu_wake_all_waiters(&waiters
);
392 #endif /* !(CAA_BITS_PER_LONG < 64) */
395 * library wrappers to be used by non-LGPL compatible source code.
398 void rcu_read_lock(void)
403 void rcu_read_unlock(void)
408 void rcu_quiescent_state(void)
410 _rcu_quiescent_state();
413 void rcu_thread_offline(void)
415 _rcu_thread_offline();
418 void rcu_thread_online(void)
420 _rcu_thread_online();
423 void rcu_register_thread(void)
425 URCU_TLS(rcu_reader
).tid
= pthread_self();
426 assert(URCU_TLS(rcu_reader
).ctr
== 0);
428 mutex_lock(&rcu_gp_lock
);
429 cds_list_add(&URCU_TLS(rcu_reader
).node
, ®istry
);
430 mutex_unlock(&rcu_gp_lock
);
431 _rcu_thread_online();
434 void rcu_unregister_thread(void)
437 * We have to make the thread offline otherwise we end up dealocking
438 * with a waiting writer.
440 _rcu_thread_offline();
441 mutex_lock(&rcu_gp_lock
);
442 cds_list_del(&URCU_TLS(rcu_reader
).node
);
443 mutex_unlock(&rcu_gp_lock
);
449 * Assertion disabled because call_rcu threads are now rcu
450 * readers, and left running at exit.
451 * assert(cds_list_empty(®istry));
455 DEFINE_RCU_FLAVOR(rcu_flavor
);
457 #include "urcu-call-rcu-impl.h"
458 #include "urcu-defer-impl.h"