virtio-input: add live migration support
[qemu.git] / include / qemu / rcu.h
blob56d3a682a934d7a63faf93ea328fa0e9571a04b3
1 #ifndef QEMU_RCU_H
2 #define QEMU_RCU_H
4 /*
5 * urcu-mb.h
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.
26 #include <glib.h>
28 #include "qemu/thread.h"
29 #include "qemu/queue.h"
30 #include "qemu/atomic.h"
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
37 * Important !
39 * Each thread containing read-side critical sections must be registered
40 * with rcu_register_thread() before calling rcu_read_lock().
41 * rcu_unregister_thread() should be called before the thread exits.
44 #ifdef DEBUG_RCU
45 #define rcu_assert(args...) assert(args)
46 #else
47 #define rcu_assert(args...)
48 #endif
51 * Global quiescent period counter with low-order bits unused.
52 * Using a int rather than a char to eliminate false register dependencies
53 * causing stalls on some architectures.
55 extern unsigned long rcu_gp_ctr;
57 extern QemuEvent rcu_gp_event;
59 struct rcu_reader_data {
60 /* Data used by both reader and synchronize_rcu() */
61 unsigned long ctr;
62 bool waiting;
64 /* Data used by reader only */
65 unsigned depth;
67 /* Data used for registry, protected by rcu_registry_lock */
68 QLIST_ENTRY(rcu_reader_data) node;
71 extern __thread struct rcu_reader_data rcu_reader;
73 static inline void rcu_read_lock(void)
75 struct rcu_reader_data *p_rcu_reader = &rcu_reader;
76 unsigned ctr;
78 if (p_rcu_reader->depth++ > 0) {
79 return;
82 ctr = atomic_read(&rcu_gp_ctr);
83 atomic_xchg(&p_rcu_reader->ctr, ctr);
86 static inline void rcu_read_unlock(void)
88 struct rcu_reader_data *p_rcu_reader = &rcu_reader;
90 assert(p_rcu_reader->depth != 0);
91 if (--p_rcu_reader->depth > 0) {
92 return;
95 atomic_xchg(&p_rcu_reader->ctr, 0);
96 if (unlikely(atomic_read(&p_rcu_reader->waiting))) {
97 atomic_set(&p_rcu_reader->waiting, false);
98 qemu_event_set(&rcu_gp_event);
102 extern void synchronize_rcu(void);
105 * Reader thread registration.
107 extern void rcu_register_thread(void);
108 extern void rcu_unregister_thread(void);
109 extern void rcu_after_fork(void);
111 struct rcu_head;
112 typedef void RCUCBFunc(struct rcu_head *head);
114 struct rcu_head {
115 struct rcu_head *next;
116 RCUCBFunc *func;
119 extern void call_rcu1(struct rcu_head *head, RCUCBFunc *func);
121 /* The operands of the minus operator must have the same type,
122 * which must be the one that we specify in the cast.
124 #define call_rcu(head, func, field) \
125 call_rcu1(({ \
126 char __attribute__((unused)) \
127 offset_must_be_zero[-offsetof(typeof(*(head)), field)], \
128 func_type_invalid = (func) - (void (*)(typeof(head)))(func); \
129 &(head)->field; \
130 }), \
131 (RCUCBFunc *)(func))
133 #define g_free_rcu(obj, field) \
134 call_rcu1(({ \
135 char __attribute__((unused)) \
136 offset_must_be_zero[-offsetof(typeof(*(obj)), field)]; \
137 &(obj)->field; \
138 }), \
139 (RCUCBFunc *)g_free);
141 #ifdef __cplusplus
143 #endif
145 #endif /* QEMU_RCU_H */