[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 21/27] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu
From: |
Paolo Bonzini |
Subject: |
[PULL 21/27] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next |
Date: |
Thu, 12 May 2022 19:24:59 +0200 |
qemu_co_queue_next is basically the same as qemu_co_enter_next but
without a QemuLockable argument. That's perfectly fine, but only
as long as the function is marked coroutine_fn. If used outside
coroutine context, qemu_co_queue_wait will attempt to take the lock
and that is just broken: if you are calling qemu_co_queue_next outside
coroutine context, the lock is going to be a QemuMutex which cannot be
taken twice by the same thread.
The patch adds the marker and reimplements qemu_co_queue_next in terms of
qemu_co_enter_next_impl, to remove duplicated code and to clarify that the
latter also works in coroutine context.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20220427130830.150180-2-pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/qemu/coroutine.h | 7 ++++---
util/qemu-coroutine-lock.c | 21 +++++++--------------
2 files changed, 11 insertions(+), 17 deletions(-)
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index 284571badb..c23d41e1ff 100644
--- a/include/qemu/coroutine.h
+++ b/include/qemu/coroutine.h
@@ -208,11 +208,12 @@ void qemu_co_queue_init(CoQueue *queue);
void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock);
/**
- * Removes the next coroutine from the CoQueue, and wake it up.
+ * Removes the next coroutine from the CoQueue, and queue it to run after
+ * the currently-running coroutine yields.
* Returns true if a coroutine was removed, false if the queue is empty.
- * OK to run from coroutine and non-coroutine context.
+ * Used from coroutine context, use qemu_co_enter_next outside.
*/
-bool qemu_co_queue_next(CoQueue *queue);
+bool coroutine_fn qemu_co_queue_next(CoQueue *queue);
/**
* Empties the CoQueue; all coroutines are woken up.
diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c
index 2669403839..5705cfea2e 100644
--- a/util/qemu-coroutine-lock.c
+++ b/util/qemu-coroutine-lock.c
@@ -67,7 +67,7 @@ void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue,
QemuLockable *lock)
}
}
-static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
+void qemu_co_queue_restart_all(CoQueue *queue)
{
Coroutine *next;
@@ -78,23 +78,10 @@ static bool qemu_co_queue_do_restart(CoQueue *queue, bool
single)
while ((next = QSIMPLEQ_FIRST(&queue->entries)) != NULL) {
QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
aio_co_wake(next);
- if (single) {
- break;
- }
}
return true;
}
-bool qemu_co_queue_next(CoQueue *queue)
-{
- return qemu_co_queue_do_restart(queue, true);
-}
-
-void qemu_co_queue_restart_all(CoQueue *queue)
-{
- qemu_co_queue_do_restart(queue, false);
-}
-
bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock)
{
Coroutine *next;
@@ -115,6 +102,12 @@ bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable
*lock)
return true;
}
+bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
+{
+ /* No unlock/lock needed in coroutine context. */
+ return qemu_co_enter_next_impl(queue, NULL);
+}
+
bool qemu_co_queue_empty(CoQueue *queue)
{
return QSIMPLEQ_FIRST(&queue->entries) == NULL;
--
2.36.0
- [PULL 01/27] pc-bios/optionrom: detect -fno-pie, (continued)
- [PULL 01/27] pc-bios/optionrom: detect -fno-pie, Paolo Bonzini, 2022/05/12
- [PULL 05/27] meson: Make mremap() detecting works correctly, Paolo Bonzini, 2022/05/12
- [PULL 11/27] machine: use QAPI struct for boot configuration, Paolo Bonzini, 2022/05/12
- [PULL 12/27] machine: add boot compound property, Paolo Bonzini, 2022/05/12
- [PULL 16/27] slirp: bump submodule past 4.7 release, Paolo Bonzini, 2022/05/12
- [PULL 06/27] hw/xen/xen_pt: Confine igd-passthrough-isa-bridge to XEN, Paolo Bonzini, 2022/05/12
- [PULL 14/27] machine: make memory-backend a link property, Paolo Bonzini, 2022/05/12
- [PULL 17/27] net: slirp: introduce a wrapper struct for QemuTimer, Paolo Bonzini, 2022/05/12
- [PULL 18/27] net: slirp: switch to slirp_new, Paolo Bonzini, 2022/05/12
- [PULL 20/27] net: slirp: allow CFI with libslirp >= 4.7, Paolo Bonzini, 2022/05/12
- [PULL 21/27] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next,
Paolo Bonzini <=
- [PULL 22/27] coroutine-lock: introduce qemu_co_queue_enter_all, Paolo Bonzini, 2022/05/12
- [PULL 08/27] tests/qtest/libqos/pci: Introduce pio_limit, Paolo Bonzini, 2022/05/12
- [PULL 04/27] checkpatch: fix g_malloc check, Paolo Bonzini, 2022/05/12
- [PULL 07/27] hw/xen/xen_pt: Resolve igd_passthrough_isa_bridge_create() indirection, Paolo Bonzini, 2022/05/12
- [PULL 09/27] tests/qtest/libqos: Skip hotplug tests if pci root bus is not hotpluggable, Paolo Bonzini, 2022/05/12
- [PULL 10/27] tests/qtest/libqos: Add generic pci host bridge in arm-virt machine, Paolo Bonzini, 2022/05/12
- [PULL 13/27] machine: add mem compound property, Paolo Bonzini, 2022/05/12
- [PULL 15/27] machine: move more memory validation to Machine object, Paolo Bonzini, 2022/05/12
- [PULL 19/27] net: slirp: add support for CFI-friendly timer API, Paolo Bonzini, 2022/05/12
- [PULL 23/27] coroutine-lock: qemu_co_queue_restart_all is a coroutine-only qemu_co_enter_all, Paolo Bonzini, 2022/05/12