7 * Userspace RCU header with explicit memory barrier.
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.
27 #include "qemu/thread.h"
28 #include "qemu/queue.h"
29 #include "qemu/atomic.h"
38 * Each thread containing read-side critical sections must be registered
39 * with rcu_register_thread() before calling rcu_read_lock().
40 * rcu_unregister_thread() should be called before the thread exits.
44 #define rcu_assert(args...) assert(args)
46 #define rcu_assert(args...)
50 * Global quiescent period counter with low-order bits unused.
51 * Using a int rather than a char to eliminate false register dependencies
52 * causing stalls on some architectures.
54 extern unsigned long rcu_gp_ctr
;
56 extern QemuEvent rcu_gp_event
;
58 struct rcu_reader_data
{
59 /* Data used by both reader and synchronize_rcu() */
63 /* Data used by reader only */
66 /* Data used for registry, protected by rcu_registry_lock */
67 QLIST_ENTRY(rcu_reader_data
) node
;
70 extern __thread
struct rcu_reader_data rcu_reader
;
72 static inline void rcu_read_lock(void)
74 struct rcu_reader_data
*p_rcu_reader
= &rcu_reader
;
77 if (p_rcu_reader
->depth
++ > 0) {
81 ctr
= atomic_read(&rcu_gp_ctr
);
82 atomic_set(&p_rcu_reader
->ctr
, ctr
);
84 /* Write p_rcu_reader->ctr before reading RCU-protected pointers. */
88 static inline void rcu_read_unlock(void)
90 struct rcu_reader_data
*p_rcu_reader
= &rcu_reader
;
92 assert(p_rcu_reader
->depth
!= 0);
93 if (--p_rcu_reader
->depth
> 0) {
97 /* Ensure that the critical section is seen to precede the
98 * store to p_rcu_reader->ctr. Together with the following
99 * smp_mb(), this ensures writes to p_rcu_reader->ctr
100 * are sequentially consistent.
102 atomic_store_release(&p_rcu_reader
->ctr
, 0);
104 /* Write p_rcu_reader->ctr before reading p_rcu_reader->waiting. */
106 if (unlikely(atomic_read(&p_rcu_reader
->waiting
))) {
107 atomic_set(&p_rcu_reader
->waiting
, false);
108 qemu_event_set(&rcu_gp_event
);
112 extern void synchronize_rcu(void);
115 * Reader thread registration.
117 extern void rcu_register_thread(void);
118 extern void rcu_unregister_thread(void);
121 * Support for fork(). fork() support is enabled at startup.
123 extern void rcu_enable_atfork(void);
124 extern void rcu_disable_atfork(void);
127 typedef void RCUCBFunc(struct rcu_head
*head
);
130 struct rcu_head
*next
;
134 extern void call_rcu1(struct rcu_head
*head
, RCUCBFunc
*func
);
136 /* The operands of the minus operator must have the same type,
137 * which must be the one that we specify in the cast.
139 #define call_rcu(head, func, field) \
141 char __attribute__((unused)) \
142 offset_must_be_zero[-offsetof(typeof(*(head)), field)], \
143 func_type_invalid = (func) - (void (*)(typeof(head)))(func); \
148 #define g_free_rcu(obj, field) \
150 char __attribute__((unused)) \
151 offset_must_be_zero[-offsetof(typeof(*(obj)), field)]; \
154 (RCUCBFunc *)g_free);
160 #endif /* QEMU_RCU_H */