Skip to content

Commit a4c2735

Browse files
stsquadphilmd
authored andcommitted
cpu: move Qemu[Thread|Cond] setup into common code
Aside from the round robin threads this is all common code. By moving the halt_cond setup we also no longer need hacks to work around the race between QOM object creation and thread creation. It is a little ugly to free stuff up for the round robin thread but better it deal with its own specialises than making the other accelerators jump through hoops. Signed-off-by: Alex Bennée <[email protected]> Reviewed-by: Pierrick Bouvier <[email protected]> Message-ID: <[email protected]> Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
1 parent b8a208c commit a4c2735

File tree

9 files changed

+16
-27
lines changed

9 files changed

+16
-27
lines changed

accel/dummy-cpus.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ void dummy_start_vcpu_thread(CPUState *cpu)
6868
{
6969
char thread_name[VCPU_THREAD_NAME_SIZE];
7070

71-
cpu->thread = g_malloc0(sizeof(QemuThread));
72-
cpu->halt_cond = g_malloc0(sizeof(QemuCond));
73-
qemu_cond_init(cpu->halt_cond);
7471
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
7572
cpu->cpu_index);
7673
qemu_thread_create(cpu->thread, thread_name, dummy_cpu_thread_fn, cpu,

accel/hvf/hvf-accel-ops.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,6 @@ static void hvf_start_vcpu_thread(CPUState *cpu)
463463
*/
464464
assert(hvf_enabled());
465465

466-
cpu->thread = g_malloc0(sizeof(QemuThread));
467-
cpu->halt_cond = g_malloc0(sizeof(QemuCond));
468-
qemu_cond_init(cpu->halt_cond);
469-
470466
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
471467
cpu->cpu_index);
472468
qemu_thread_create(cpu->thread, thread_name, hvf_cpu_thread_fn,

accel/kvm/kvm-accel-ops.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ static void kvm_start_vcpu_thread(CPUState *cpu)
6666
{
6767
char thread_name[VCPU_THREAD_NAME_SIZE];
6868

69-
cpu->thread = g_malloc0(sizeof(QemuThread));
70-
cpu->halt_cond = g_malloc0(sizeof(QemuCond));
71-
qemu_cond_init(cpu->halt_cond);
7269
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
7370
cpu->cpu_index);
7471
qemu_thread_create(cpu->thread, thread_name, kvm_vcpu_thread_fn,

accel/tcg/tcg-accel-ops-mttcg.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ void mttcg_start_vcpu_thread(CPUState *cpu)
137137
g_assert(tcg_enabled());
138138
tcg_cpu_init_cflags(cpu, current_machine->smp.max_cpus > 1);
139139

140-
cpu->thread = g_new0(QemuThread, 1);
141-
cpu->halt_cond = g_malloc0(sizeof(QemuCond));
142-
qemu_cond_init(cpu->halt_cond);
143-
144140
/* create a thread per vCPU with TCG (MTTCG) */
145141
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
146142
cpu->cpu_index);

accel/tcg/tcg-accel-ops-rr.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,22 +317,22 @@ void rr_start_vcpu_thread(CPUState *cpu)
317317
tcg_cpu_init_cflags(cpu, false);
318318

319319
if (!single_tcg_cpu_thread) {
320-
cpu->thread = g_new0(QemuThread, 1);
321-
cpu->halt_cond = g_new0(QemuCond, 1);
322-
qemu_cond_init(cpu->halt_cond);
320+
single_tcg_halt_cond = cpu->halt_cond;
321+
single_tcg_cpu_thread = cpu->thread;
323322

324323
/* share a single thread for all cpus with TCG */
325324
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
326325
qemu_thread_create(cpu->thread, thread_name,
327326
rr_cpu_thread_fn,
328327
cpu, QEMU_THREAD_JOINABLE);
329-
330-
single_tcg_halt_cond = cpu->halt_cond;
331-
single_tcg_cpu_thread = cpu->thread;
332328
} else {
333-
/* we share the thread */
329+
/* we share the thread, dump spare data */
330+
g_free(cpu->thread);
331+
qemu_cond_destroy(cpu->halt_cond);
334332
cpu->thread = single_tcg_cpu_thread;
335333
cpu->halt_cond = single_tcg_halt_cond;
334+
335+
/* copy the stuff done at start of rr_cpu_thread_fn */
336336
cpu->thread_id = first_cpu->thread_id;
337337
cpu->neg.can_do_io = 1;
338338
cpu->created = true;

hw/core/cpu-common.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ static void cpu_common_initfn(Object *obj)
261261
cpu->nr_threads = 1;
262262
cpu->cflags_next_tb = -1;
263263

264+
/* allocate storage for thread info, initialise condition variables */
265+
cpu->thread = g_new0(QemuThread, 1);
266+
cpu->halt_cond = g_new0(QemuCond, 1);
267+
qemu_cond_init(cpu->halt_cond);
268+
264269
qemu_mutex_init(&cpu->work_mutex);
265270
qemu_lockcnt_init(&cpu->in_ioctl_lock);
266271
QSIMPLEQ_INIT(&cpu->work_list);

include/hw/core/cpu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,14 @@ struct qemu_work_item;
404404
* @tcg_cflags: Pre-computed cflags for this cpu.
405405
* @nr_cores: Number of cores within this CPU package.
406406
* @nr_threads: Number of threads within this CPU core.
407+
* @thread: Host thread details, only live once @created is #true
408+
* @sem: WIN32 only semaphore used only for qtest
409+
* @thread_id: native thread id of vCPU, only live once @created is #true
407410
* @running: #true if CPU is currently running (lockless).
408411
* @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
409412
* valid under cpu_list_lock.
410413
* @created: Indicates whether the CPU thread has been successfully created.
414+
* @halt_cond: condition variable sleeping threads can wait on.
411415
* @interrupt_request: Indicates a pending interrupt request.
412416
* @halted: Nonzero if the CPU is in suspended state.
413417
* @stop: Indicates a pending stop request.

target/i386/nvmm/nvmm-accel-ops.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ static void nvmm_start_vcpu_thread(CPUState *cpu)
6464
{
6565
char thread_name[VCPU_THREAD_NAME_SIZE];
6666

67-
cpu->thread = g_new0(QemuThread, 1);
68-
cpu->halt_cond = g_new0(QemuCond, 1);
69-
qemu_cond_init(cpu->halt_cond);
7067
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/NVMM",
7168
cpu->cpu_index);
7269
qemu_thread_create(cpu->thread, thread_name, qemu_nvmm_cpu_thread_fn,

target/i386/whpx/whpx-accel-ops.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ static void whpx_start_vcpu_thread(CPUState *cpu)
6464
{
6565
char thread_name[VCPU_THREAD_NAME_SIZE];
6666

67-
cpu->thread = g_new0(QemuThread, 1);
68-
cpu->halt_cond = g_new0(QemuCond, 1);
69-
qemu_cond_init(cpu->halt_cond);
7067
snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
7168
cpu->cpu_index);
7269
qemu_thread_create(cpu->thread, thread_name, whpx_cpu_thread_fn,

0 commit comments

Comments
 (0)