]> git.proxmox.com Git - mirror_qemu.git/blob - cpus-common.c
cpus-common: move CPU work item management to common code
[mirror_qemu.git] / cpus-common.c
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 static QemuCond qemu_work_cond;
27
28 void qemu_init_cpu_list(void)
29 {
30 qemu_mutex_init(&qemu_cpu_list_lock);
31 qemu_cond_init(&qemu_work_cond);
32 }
33
34 void cpu_list_lock(void)
35 {
36 qemu_mutex_lock(&qemu_cpu_list_lock);
37 }
38
39 void cpu_list_unlock(void)
40 {
41 qemu_mutex_unlock(&qemu_cpu_list_lock);
42 }
43
44 static bool cpu_index_auto_assigned;
45
46 static int cpu_get_free_index(void)
47 {
48 CPUState *some_cpu;
49 int cpu_index = 0;
50
51 cpu_index_auto_assigned = true;
52 CPU_FOREACH(some_cpu) {
53 cpu_index++;
54 }
55 return cpu_index;
56 }
57
58 void cpu_list_add(CPUState *cpu)
59 {
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);
66 }
67 QTAILQ_INSERT_TAIL(&cpus, cpu, node);
68 qemu_mutex_unlock(&qemu_cpu_list_lock);
69 }
70
71 void cpu_list_remove(CPUState *cpu)
72 {
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;
78 }
79
80 assert(!(cpu_index_auto_assigned && cpu != QTAILQ_LAST(&cpus, CPUTailQ)));
81
82 QTAILQ_REMOVE(&cpus, cpu, node);
83 cpu->cpu_index = UNASSIGNED_CPU_INDEX;
84 qemu_mutex_unlock(&qemu_cpu_list_lock);
85 }
86
87 struct qemu_work_item {
88 struct qemu_work_item *next;
89 run_on_cpu_func func;
90 void *data;
91 int done;
92 bool free;
93 };
94
95 static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
96 {
97 qemu_mutex_lock(&cpu->work_mutex);
98 if (cpu->queued_work_first == NULL) {
99 cpu->queued_work_first = wi;
100 } else {
101 cpu->queued_work_last->next = wi;
102 }
103 cpu->queued_work_last = wi;
104 wi->next = NULL;
105 wi->done = false;
106 qemu_mutex_unlock(&cpu->work_mutex);
107
108 qemu_cpu_kick(cpu);
109 }
110
111 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data,
112 QemuMutex *mutex)
113 {
114 struct qemu_work_item wi;
115
116 if (qemu_cpu_is_self(cpu)) {
117 func(cpu, data);
118 return;
119 }
120
121 wi.func = func;
122 wi.data = data;
123 wi.free = false;
124
125 queue_work_on_cpu(cpu, &wi);
126 while (!atomic_mb_read(&wi.done)) {
127 CPUState *self_cpu = current_cpu;
128
129 qemu_cond_wait(&qemu_work_cond, mutex);
130 current_cpu = self_cpu;
131 }
132 }
133
134 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data)
135 {
136 struct qemu_work_item *wi;
137
138 if (qemu_cpu_is_self(cpu)) {
139 func(cpu, data);
140 return;
141 }
142
143 wi = g_malloc0(sizeof(struct qemu_work_item));
144 wi->func = func;
145 wi->data = data;
146 wi->free = true;
147
148 queue_work_on_cpu(cpu, wi);
149 }
150
151 void process_queued_cpu_work(CPUState *cpu)
152 {
153 struct qemu_work_item *wi;
154
155 if (cpu->queued_work_first == NULL) {
156 return;
157 }
158
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;
165 }
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);
173 }
174 }
175 qemu_mutex_unlock(&cpu->work_mutex);
176 qemu_cond_broadcast(&qemu_work_cond);
177 }