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;
27 void qemu_init_cpu_list(void)
29 qemu_mutex_init(&qemu_cpu_list_lock);
32 void cpu_list_lock(void)
34 qemu_mutex_lock(&qemu_cpu_list_lock);
37 void cpu_list_unlock(void)
39 qemu_mutex_unlock(&qemu_cpu_list_lock);
42 static bool cpu_index_auto_assigned;
44 static int cpu_get_free_index(void)
49 cpu_index_auto_assigned = true;
50 CPU_FOREACH(some_cpu) {
56 void cpu_list_add(CPUState *cpu)
58 qemu_mutex_lock(&qemu_cpu_list_lock);
59 if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
60 cpu->cpu_index = cpu_get_free_index();
61 assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
63 assert(!cpu_index_auto_assigned);
65 QTAILQ_INSERT_TAIL(&cpus, cpu, node);
66 qemu_mutex_unlock(&qemu_cpu_list_lock);
69 void cpu_list_remove(CPUState *cpu)
71 qemu_mutex_lock(&qemu_cpu_list_lock);
72 if (!QTAILQ_IN_USE(cpu, node)) {
73 /* there is nothing to undo since cpu_exec_init() hasn't been called */
74 qemu_mutex_unlock(&qemu_cpu_list_lock);
78 assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus, CPUTailQ)));
80 QTAILQ_REMOVE(&cpus, cpu, node);
81 cpu->cpu_index = UNASSIGNED_CPU_INDEX;
82 qemu_mutex_unlock(&qemu_cpu_list_lock);