cpus-common: fix uninitialized variable use in run_on_cpu
[qemu/ar7.git] / cpus-common.c
blobd6cd4262353e8397305037fb1ae11fe34ce10880
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 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)
48 CPUState *some_cpu;
49 int cpu_index = 0;
51 cpu_index_auto_assigned = true;
52 CPU_FOREACH(some_cpu) {
53 cpu_index++;
55 return cpu_index;
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);
64 } else {
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);
77 return;
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;
89 run_on_cpu_func func;
90 void *data;
91 bool free, done;
94 static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
96 qemu_mutex_lock(&cpu->work_mutex);
97 if (cpu->queued_work_first == NULL) {
98 cpu->queued_work_first = wi;
99 } else {
100 cpu->queued_work_last->next = wi;
102 cpu->queued_work_last = wi;
103 wi->next = NULL;
104 wi->done = false;
105 qemu_mutex_unlock(&cpu->work_mutex);
107 qemu_cpu_kick(cpu);
110 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data,
111 QemuMutex *mutex)
113 struct qemu_work_item wi;
115 if (qemu_cpu_is_self(cpu)) {
116 func(cpu, data);
117 return;
120 wi.func = func;
121 wi.data = data;
122 wi.done = false;
123 wi.free = false;
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)) {
139 func(cpu, data);
140 return;
143 wi = g_malloc0(sizeof(struct qemu_work_item));
144 wi->func = func;
145 wi->data = data;
146 wi->free = true;
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) {
156 return;
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);
169 if (wi->free) {
170 g_free(wi);
171 } else {
172 atomic_mb_set(&wi->done, true);
175 qemu_mutex_unlock(&cpu->work_mutex);
176 qemu_cond_broadcast(&qemu_work_cond);