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.
34 #include "qemu/compiler.h"
35 #include "qemu/thread.h"
36 #include "qemu/queue.h"
37 #include "qemu/atomic.h"
46 * Each thread containing read-side critical sections must be registered
47 * with rcu_register_thread() before calling rcu_read_lock().
48 * rcu_unregister_thread() should be called before the thread exits.
52 #define rcu_assert(args...) assert(args)
54 #define rcu_assert(args...)
58 * Global quiescent period counter with low-order bits unused.
59 * Using a int rather than a char to eliminate false register dependencies
60 * causing stalls on some architectures.
62 extern unsigned long rcu_gp_ctr
;
64 extern QemuEvent rcu_gp_event
;
66 struct rcu_reader_data
{
67 /* Data used by both reader and synchronize_rcu() */
71 /* Data used by reader only */
74 /* Data used for registry, protected by rcu_registry_lock */
75 QLIST_ENTRY(rcu_reader_data
) node
;
78 extern __thread
struct rcu_reader_data rcu_reader
;
80 static inline void rcu_read_lock(void)
82 struct rcu_reader_data
*p_rcu_reader
= &rcu_reader
;
85 if (p_rcu_reader
->depth
++ > 0) {
89 ctr
= atomic_read(&rcu_gp_ctr
);
90 atomic_xchg(&p_rcu_reader
->ctr
, ctr
);
93 static inline void rcu_read_unlock(void)
95 struct rcu_reader_data
*p_rcu_reader
= &rcu_reader
;
97 assert(p_rcu_reader
->depth
!= 0);
98 if (--p_rcu_reader
->depth
> 0) {
102 atomic_xchg(&p_rcu_reader
->ctr
, 0);
103 if (unlikely(atomic_read(&p_rcu_reader
->waiting
))) {
104 atomic_set(&p_rcu_reader
->waiting
, false);
105 qemu_event_set(&rcu_gp_event
);
109 extern void synchronize_rcu(void);
112 * Reader thread registration.
114 extern void rcu_register_thread(void);
115 extern void rcu_unregister_thread(void);
116 extern void rcu_after_fork(void);
119 typedef void RCUCBFunc(struct rcu_head
*head
);
122 struct rcu_head
*next
;
126 extern void call_rcu1(struct rcu_head
*head
, RCUCBFunc
*func
);
128 /* The operands of the minus operator must have the same type,
129 * which must be the one that we specify in the cast.
131 #define call_rcu(head, func, field) \
133 char __attribute__((unused)) \
134 offset_must_be_zero[-offsetof(typeof(*(head)), field)], \
135 func_type_invalid = (func) - (void (*)(typeof(head)))(func); \
140 #define g_free_rcu(obj, field) \
142 char __attribute__((unused)) \
143 offset_must_be_zero[-offsetof(typeof(*(obj)), field)]; \
146 (RCUCBFunc *)g_free);
152 #endif /* QEMU_RCU_H */