Paolo Bonzini | 267f685 | 2016-08-28 03:45:14 +0200 | [diff] [blame^] | 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 | |
| 27 | void qemu_init_cpu_list(void) |
| 28 | { |
| 29 | qemu_mutex_init(&qemu_cpu_list_lock); |
| 30 | } |
| 31 | |
| 32 | void cpu_list_lock(void) |
| 33 | { |
| 34 | qemu_mutex_lock(&qemu_cpu_list_lock); |
| 35 | } |
| 36 | |
| 37 | void cpu_list_unlock(void) |
| 38 | { |
| 39 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
| 40 | } |
| 41 | |
| 42 | static bool cpu_index_auto_assigned; |
| 43 | |
| 44 | static int cpu_get_free_index(void) |
| 45 | { |
| 46 | CPUState *some_cpu; |
| 47 | int cpu_index = 0; |
| 48 | |
| 49 | cpu_index_auto_assigned = true; |
| 50 | CPU_FOREACH(some_cpu) { |
| 51 | cpu_index++; |
| 52 | } |
| 53 | return cpu_index; |
| 54 | } |
| 55 | |
| 56 | void cpu_list_add(CPUState *cpu) |
| 57 | { |
| 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); |
| 62 | } else { |
| 63 | assert(!cpu_index_auto_assigned); |
| 64 | } |
| 65 | QTAILQ_INSERT_TAIL(&cpus, cpu, node); |
| 66 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
| 67 | } |
| 68 | |
| 69 | void cpu_list_remove(CPUState *cpu) |
| 70 | { |
| 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); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus, CPUTailQ))); |
| 79 | |
| 80 | QTAILQ_REMOVE(&cpus, cpu, node); |
| 81 | cpu->cpu_index = UNASSIGNED_CPU_INDEX; |
| 82 | qemu_mutex_unlock(&qemu_cpu_list_lock); |
| 83 | } |