cpus-common: move exclusive work infrastructure from linux-user
[qemu/ar7.git] / cpus-common.c
blob7d935fd3dd9f93f9491421c6b50ede8e495bbb8b
1 /*
2 * CPU thread main loop - common bits for user and system mode emulation
4 * Copyright (c) 2003-2005 Fabrice Bellard
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "exec/cpu-common.h"
22 #include "qom/cpu.h"
23 #include "sysemu/cpus.h"
25 static QemuMutex qemu_cpu_list_lock;
26 static QemuCond exclusive_cond;
27 static QemuCond exclusive_resume;
28 static QemuCond qemu_work_cond;
30 static int pending_cpus;
32 void qemu_init_cpu_list(void)
34 /* This is needed because qemu_init_cpu_list is also called by the
35 * child process in a fork. */
36 pending_cpus = 0;
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);
44 void cpu_list_lock(void)
46 qemu_mutex_lock(&qemu_cpu_list_lock);
49 void cpu_list_unlock(void)
51 qemu_mutex_unlock(&qemu_cpu_list_lock);
54 static bool cpu_index_auto_assigned;
56 static int cpu_get_free_index(void)
58 CPUState *some_cpu;
59 int cpu_index = 0;
61 cpu_index_auto_assigned = true;
62 CPU_FOREACH(some_cpu) {
63 cpu_index++;
65 return cpu_index;
68 static void finish_safe_work(CPUState *cpu)
70 cpu_exec_start(cpu);
71 cpu_exec_end(cpu);
74 void cpu_list_add(CPUState *cpu)
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);
83 QTAILQ_INSERT_TAIL(&cpus, cpu, node);
84 qemu_mutex_unlock(&qemu_cpu_list_lock);
86 finish_safe_work(cpu);
89 void cpu_list_remove(CPUState *cpu)
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;
98 assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus, CPUTailQ)));
100 QTAILQ_REMOVE(&cpus, cpu, node);
101 cpu->cpu_index = UNASSIGNED_CPU_INDEX;
102 qemu_mutex_unlock(&qemu_cpu_list_lock);
105 struct qemu_work_item {
106 struct qemu_work_item *next;
107 run_on_cpu_func func;
108 void *data;
109 bool free, done;
112 static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
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;
120 cpu->queued_work_last = wi;
121 wi->next = NULL;
122 wi->done = false;
123 qemu_mutex_unlock(&cpu->work_mutex);
125 qemu_cpu_kick(cpu);
128 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data,
129 QemuMutex *mutex)
131 struct qemu_work_item wi;
133 if (qemu_cpu_is_self(cpu)) {
134 func(cpu, data);
135 return;
138 wi.func = func;
139 wi.data = data;
140 wi.done = false;
141 wi.free = false;
143 queue_work_on_cpu(cpu, &wi);
144 while (!atomic_mb_read(&wi.done)) {
145 CPUState *self_cpu = current_cpu;
147 qemu_cond_wait(&qemu_work_cond, mutex);
148 current_cpu = self_cpu;
152 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data)
154 struct qemu_work_item *wi;
156 if (qemu_cpu_is_self(cpu)) {
157 func(cpu, data);
158 return;
161 wi = g_malloc0(sizeof(struct qemu_work_item));
162 wi->func = func;
163 wi->data = data;
164 wi->free = true;
166 queue_work_on_cpu(cpu, wi);
169 /* Wait for pending exclusive operations to complete. The CPU list lock
170 must be held. */
171 static inline void exclusive_idle(void)
173 while (pending_cpus) {
174 qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock);
178 /* Start an exclusive operation.
179 Must only be called from outside cpu_exec, takes
180 qemu_cpu_list_lock. */
181 void start_exclusive(void)
183 CPUState *other_cpu;
185 qemu_mutex_lock(&qemu_cpu_list_lock);
186 exclusive_idle();
188 /* Make all other cpus stop executing. */
189 pending_cpus = 1;
190 CPU_FOREACH(other_cpu) {
191 if (other_cpu->running) {
192 pending_cpus++;
193 qemu_cpu_kick(other_cpu);
196 while (pending_cpus > 1) {
197 qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
201 /* Finish an exclusive operation. Releases qemu_cpu_list_lock. */
202 void end_exclusive(void)
204 pending_cpus = 0;
205 qemu_cond_broadcast(&exclusive_resume);
206 qemu_mutex_unlock(&qemu_cpu_list_lock);
209 /* Wait for exclusive ops to finish, and begin cpu execution. */
210 void cpu_exec_start(CPUState *cpu)
212 qemu_mutex_lock(&qemu_cpu_list_lock);
213 exclusive_idle();
214 cpu->running = true;
215 qemu_mutex_unlock(&qemu_cpu_list_lock);
218 /* Mark cpu as not executing, and release pending exclusive ops. */
219 void cpu_exec_end(CPUState *cpu)
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);
229 exclusive_idle();
230 qemu_mutex_unlock(&qemu_cpu_list_lock);
233 void process_queued_cpu_work(CPUState *cpu)
235 struct qemu_work_item *wi;
237 if (cpu->queued_work_first == NULL) {
238 return;
241 qemu_mutex_lock(&cpu->work_mutex);
242 while (cpu->queued_work_first != NULL) {
243 wi = cpu->queued_work_first;
244 cpu->queued_work_first = wi->next;
245 if (!cpu->queued_work_first) {
246 cpu->queued_work_last = NULL;
248 qemu_mutex_unlock(&cpu->work_mutex);
249 wi->func(cpu, wi->data);
250 qemu_mutex_lock(&cpu->work_mutex);
251 if (wi->free) {
252 g_free(wi);
253 } else {
254 atomic_mb_set(&wi->done, true);
257 qemu_mutex_unlock(&cpu->work_mutex);
258 qemu_cond_broadcast(&qemu_work_cond);