1#ifndef QEMU_RCU_H
2#define QEMU_RCU_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "qemu/thread.h"
28#include "qemu/queue.h"
29#include "qemu/atomic.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35
36
37
38
39
40
41
42
43#ifdef DEBUG_RCU
44#define rcu_assert(args...) assert(args)
45#else
46#define rcu_assert(args...)
47#endif
48
49
50
51
52
53
54extern unsigned long rcu_gp_ctr;
55
56extern QemuEvent rcu_gp_event;
57
58struct rcu_reader_data {
59
60 unsigned long ctr;
61 bool waiting;
62
63
64 unsigned depth;
65
66
67 QLIST_ENTRY(rcu_reader_data) node;
68};
69
70extern __thread struct rcu_reader_data rcu_reader;
71
72static inline void rcu_read_lock(void)
73{
74 struct rcu_reader_data *p_rcu_reader = &rcu_reader;
75 unsigned ctr;
76
77 if (p_rcu_reader->depth++ > 0) {
78 return;
79 }
80
81 ctr = atomic_read(&rcu_gp_ctr);
82 atomic_xchg(&p_rcu_reader->ctr, ctr);
83}
84
85static inline void rcu_read_unlock(void)
86{
87 struct rcu_reader_data *p_rcu_reader = &rcu_reader;
88
89 assert(p_rcu_reader->depth != 0);
90 if (--p_rcu_reader->depth > 0) {
91 return;
92 }
93
94 atomic_xchg(&p_rcu_reader->ctr, 0);
95 if (unlikely(atomic_read(&p_rcu_reader->waiting))) {
96 atomic_set(&p_rcu_reader->waiting, false);
97 qemu_event_set(&rcu_gp_event);
98 }
99}
100
101extern void synchronize_rcu(void);
102
103
104
105
106extern void rcu_register_thread(void);
107extern void rcu_unregister_thread(void);
108
109
110
111
112extern void rcu_enable_atfork(void);
113extern void rcu_disable_atfork(void);
114
115struct rcu_head;
116typedef void RCUCBFunc(struct rcu_head *head);
117
118struct rcu_head {
119 struct rcu_head *next;
120 RCUCBFunc *func;
121};
122
123extern void call_rcu1(struct rcu_head *head, RCUCBFunc *func);
124
125
126
127
128#define call_rcu(head, func, field) \
129 call_rcu1(({ \
130 char __attribute__((unused)) \
131 offset_must_be_zero[-offsetof(typeof(*(head)), field)], \
132 func_type_invalid = (func) - (void (*)(typeof(head)))(func); \
133 &(head)->field; \
134 }), \
135 (RCUCBFunc *)(func))
136
137#define g_free_rcu(obj, field) \
138 call_rcu1(({ \
139 char __attribute__((unused)) \
140 offset_must_be_zero[-offsetof(typeof(*(obj)), field)]; \
141 &(obj)->field; \
142 }), \
143 (RCUCBFunc *)g_free);
144
145#ifdef __cplusplus
146}
147#endif
148
149#endif
150