]> git.proxmox.com Git - mirror_qemu.git/blob - cpus-common.c
cpus-common: simplify locking for start_exclusive/end_exclusive
[mirror_qemu.git] / cpus-common.c
1 /*
2 * CPU thread main loop - common bits for user and system mode emulation
3 *
4 * Copyright (c) 2003-2005 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "exec/cpu-common.h"
22 #include "qom/cpu.h"
23 #include "sysemu/cpus.h"
24
25 static QemuMutex qemu_cpu_list_lock;
26 static QemuCond exclusive_cond;
27 static QemuCond exclusive_resume;
28 static QemuCond qemu_work_cond;
29
30 static int pending_cpus;
31
32 void qemu_init_cpu_list(void)
33 {
34 /* This is needed because qemu_init_cpu_list is also called by the
35 * child process in a fork. */
36 pending_cpus = 0;
37
38 qemu_mutex_init(&qemu_cpu_list_lock);
39 qemu_cond_init(&exclusive_cond);
40 qemu_cond_init(&exclusive_resume);
41 qemu_cond_init(&qemu_work_cond);
42 }
43
44 void cpu_list_lock(void)
45 {
46 qemu_mutex_lock(&qemu_cpu_list_lock);
47 }
48
49 void cpu_list_unlock(void)
50 {
51 qemu_mutex_unlock(&qemu_cpu_list_lock);
52 }
53
54 static bool cpu_index_auto_assigned;
55
56 static int cpu_get_free_index(void)
57 {
58 CPUState *some_cpu;
59 int cpu_index = 0;
60
61 cpu_index_auto_assigned = true;
62 CPU_FOREACH(some_cpu) {
63 cpu_index++;
64 }
65 return cpu_index;
66 }
67
68 static void finish_safe_work(CPUState *cpu)
69 {
70 cpu_exec_start(cpu);
71 cpu_exec_end(cpu);
72 }
73
74 void cpu_list_add(CPUState *cpu)
75 {
76 qemu_mutex_lock(&qemu_cpu_list_lock);
77 if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
78 cpu->cpu_index = cpu_get_free_index();
79 assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
80 } else {
81 assert(!cpu_index_auto_assigned);
82 }
83 QTAILQ_INSERT_TAIL(&cpus, cpu, node);
84 qemu_mutex_unlock(&qemu_cpu_list_lock);
85
86 finish_safe_work(cpu);
87 }
88
89 void cpu_list_remove(CPUState *cpu)
90 {
91 qemu_mutex_lock(&qemu_cpu_list_lock);
92 if (!QTAILQ_IN_USE(cpu, node)) {
93 /* there is nothing to undo since cpu_exec_init() hasn't been called */
94 qemu_mutex_unlock(&qemu_cpu_list_lock);
95 return;
96 }
97
98 assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus, CPUTailQ)));
99
100 QTAILQ_REMOVE(&cpus, cpu, node);
101 cpu->cpu_index = UNASSIGNED_CPU_INDEX;
102 qemu_mutex_unlock(&qemu_cpu_list_lock);
103 }
104
105 struct qemu_work_item {
106 struct qemu_work_item *next;
107 run_on_cpu_func func;
108 void *data;
109 bool free, done;
110 };
111
112 static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
113 {
114 qemu_mutex_lock(&cpu->work_mutex);
115 if (cpu->queued_work_first == NULL) {
116 cpu->queued_work_first = wi;
117 } else {
118 cpu->queued_work_last->next = wi;
119 }
120 cpu->queued_work_last = wi;
121 wi->next = NULL;
122 wi->done = false;
123 qemu_mutex_unlock(&cpu->work_mutex);
124
125 qemu_cpu_kick(cpu);
126 }
127
128 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data,
129 QemuMutex *mutex)
130 {
131 struct qemu_work_item wi;
132
133 if (qemu_cpu_is_self(cpu)) {
134 func(cpu, data);
135 return;
136 }
137
138 wi.func = func;
139 wi.data = data;
140 wi.done = false;
141 wi.free = false;
142
143 queue_work_on_cpu(cpu, &wi);
144 while (!atomic_mb_read(&wi.done)) {
145 CPUState *self_cpu = current_cpu;
146
147 qemu_cond_wait(&qemu_work_cond, mutex);
148 current_cpu = self_cpu;
149 }
150 }
151
152 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data)
153 {
154 struct qemu_work_item *wi;
155
156 wi = g_malloc0(sizeof(struct qemu_work_item));
157 wi->func = func;
158 wi->data = data;
159 wi->free = true;
160
161 queue_work_on_cpu(cpu, wi);
162 }
163
164 /* Wait for pending exclusive operations to complete. The CPU list lock
165 must be held. */
166 static inline void exclusive_idle(void)
167 {
168 while (pending_cpus) {
169 qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock);
170 }
171 }
172
173 /* Start an exclusive operation.
174 Must only be called from outside cpu_exec. */
175 void start_exclusive(void)
176 {
177 CPUState *other_cpu;
178
179 qemu_mutex_lock(&qemu_cpu_list_lock);
180 exclusive_idle();
181
182 /* Make all other cpus stop executing. */
183 pending_cpus = 1;
184 CPU_FOREACH(other_cpu) {
185 if (other_cpu->running) {
186 pending_cpus++;
187 qemu_cpu_kick(other_cpu);
188 }
189 }
190 while (pending_cpus > 1) {
191 qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
192 }
193
194 /* Can release mutex, no one will enter another exclusive
195 * section until end_exclusive resets pending_cpus to 0.
196 */
197 qemu_mutex_unlock(&qemu_cpu_list_lock);
198 }
199
200 /* Finish an exclusive operation. */
201 void end_exclusive(void)
202 {
203 qemu_mutex_lock(&qemu_cpu_list_lock);
204 pending_cpus = 0;
205 qemu_cond_broadcast(&exclusive_resume);
206 qemu_mutex_unlock(&qemu_cpu_list_lock);
207 }
208
209 /* Wait for exclusive ops to finish, and begin cpu execution. */
210 void cpu_exec_start(CPUState *cpu)
211 {
212 qemu_mutex_lock(&qemu_cpu_list_lock);
213 exclusive_idle();
214 cpu->running = true;
215 qemu_mutex_unlock(&qemu_cpu_list_lock);
216 }
217
218 /* Mark cpu as not executing, and release pending exclusive ops. */
219 void cpu_exec_end(CPUState *cpu)
220 {
221 qemu_mutex_lock(&qemu_cpu_list_lock);
222 cpu->running = false;
223 if (pending_cpus > 1) {
224 pending_cpus--;
225 if (pending_cpus == 1) {
226 qemu_cond_signal(&exclusive_cond);
227 }
228 }
229 qemu_mutex_unlock(&qemu_cpu_list_lock);
230 }
231
232 void process_queued_cpu_work(CPUState *cpu)
233 {
234 struct qemu_work_item *wi;
235
236 if (cpu->queued_work_first == NULL) {
237 return;
238 }
239
240 qemu_mutex_lock(&cpu->work_mutex);
241 while (cpu->queued_work_first != NULL) {
242 wi = cpu->queued_work_first;
243 cpu->queued_work_first = wi->next;
244 if (!cpu->queued_work_first) {
245 cpu->queued_work_last = NULL;
246 }
247 qemu_mutex_unlock(&cpu->work_mutex);
248 wi->func(cpu, wi->data);
249 qemu_mutex_lock(&cpu->work_mutex);
250 if (wi->free) {
251 g_free(wi);
252 } else {
253 atomic_mb_set(&wi->done, true);
254 }
255 }
256 qemu_mutex_unlock(&cpu->work_mutex);
257 qemu_cond_broadcast(&qemu_work_cond);
258 }