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"
23 #include "sysemu/cpus.h"
25 static QemuMutex qemu_cpu_list_lock
;
26 static QemuCond qemu_work_cond
;
28 void qemu_init_cpu_list(void)
30 qemu_mutex_init(&qemu_cpu_list_lock
);
31 qemu_cond_init(&qemu_work_cond
);
34 void cpu_list_lock(void)
36 qemu_mutex_lock(&qemu_cpu_list_lock
);
39 void cpu_list_unlock(void)
41 qemu_mutex_unlock(&qemu_cpu_list_lock
);
44 static bool cpu_index_auto_assigned
;
46 static int cpu_get_free_index(void)
51 cpu_index_auto_assigned
= true;
52 CPU_FOREACH(some_cpu
) {
58 void cpu_list_add(CPUState
*cpu
)
60 qemu_mutex_lock(&qemu_cpu_list_lock
);
61 if (cpu
->cpu_index
== UNASSIGNED_CPU_INDEX
) {
62 cpu
->cpu_index
= cpu_get_free_index();
63 assert(cpu
->cpu_index
!= UNASSIGNED_CPU_INDEX
);
65 assert(!cpu_index_auto_assigned
);
67 QTAILQ_INSERT_TAIL(&cpus
, cpu
, node
);
68 qemu_mutex_unlock(&qemu_cpu_list_lock
);
71 void cpu_list_remove(CPUState
*cpu
)
73 qemu_mutex_lock(&qemu_cpu_list_lock
);
74 if (!QTAILQ_IN_USE(cpu
, node
)) {
75 /* there is nothing to undo since cpu_exec_init() hasn't been called */
76 qemu_mutex_unlock(&qemu_cpu_list_lock
);
80 assert(!(cpu_index_auto_assigned
&& cpu
!= QTAILQ_LAST(&cpus
, CPUTailQ
)));
82 QTAILQ_REMOVE(&cpus
, cpu
, node
);
83 cpu
->cpu_index
= UNASSIGNED_CPU_INDEX
;
84 qemu_mutex_unlock(&qemu_cpu_list_lock
);
87 struct qemu_work_item
{
88 struct qemu_work_item
*next
;
95 static void queue_work_on_cpu(CPUState
*cpu
, struct qemu_work_item
*wi
)
97 qemu_mutex_lock(&cpu
->work_mutex
);
98 if (cpu
->queued_work_first
== NULL
) {
99 cpu
->queued_work_first
= wi
;
101 cpu
->queued_work_last
->next
= wi
;
103 cpu
->queued_work_last
= wi
;
106 qemu_mutex_unlock(&cpu
->work_mutex
);
111 void do_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, void *data
,
114 struct qemu_work_item wi
;
116 if (qemu_cpu_is_self(cpu
)) {
125 queue_work_on_cpu(cpu
, &wi
);
126 while (!atomic_mb_read(&wi
.done
)) {
127 CPUState
*self_cpu
= current_cpu
;
129 qemu_cond_wait(&qemu_work_cond
, mutex
);
130 current_cpu
= self_cpu
;
134 void async_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, void *data
)
136 struct qemu_work_item
*wi
;
138 if (qemu_cpu_is_self(cpu
)) {
143 wi
= g_malloc0(sizeof(struct qemu_work_item
));
148 queue_work_on_cpu(cpu
, wi
);
151 void process_queued_cpu_work(CPUState
*cpu
)
153 struct qemu_work_item
*wi
;
155 if (cpu
->queued_work_first
== NULL
) {
159 qemu_mutex_lock(&cpu
->work_mutex
);
160 while (cpu
->queued_work_first
!= NULL
) {
161 wi
= cpu
->queued_work_first
;
162 cpu
->queued_work_first
= wi
->next
;
163 if (!cpu
->queued_work_first
) {
164 cpu
->queued_work_last
= NULL
;
166 qemu_mutex_unlock(&cpu
->work_mutex
);
167 wi
->func(cpu
, wi
->data
);
168 qemu_mutex_lock(&cpu
->work_mutex
);
172 atomic_mb_set(&wi
->done
, true);
175 qemu_mutex_unlock(&cpu
->work_mutex
);
176 qemu_cond_broadcast(&qemu_work_cond
);