monitor: introduce qmp_dispatcher_co_wake

This makes it possible to turn qmp_dispatcher_co_busy into a static
variable.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2023-03-03 13:51:44 +01:00
parent 0ff2553701
commit 9f2d58546e
3 changed files with 31 additions and 29 deletions

View File

@ -165,7 +165,6 @@ typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList;
extern IOThread *mon_iothread; extern IOThread *mon_iothread;
extern Coroutine *qmp_dispatcher_co; extern Coroutine *qmp_dispatcher_co;
extern bool qmp_dispatcher_co_shutdown; extern bool qmp_dispatcher_co_shutdown;
extern bool qmp_dispatcher_co_busy;
extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands; extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
extern QemuMutex monitor_lock; extern QemuMutex monitor_lock;
extern MonitorList mon_list; extern MonitorList mon_list;
@ -183,6 +182,7 @@ void monitor_fdsets_cleanup(void);
void qmp_send_response(MonitorQMP *mon, const QDict *rsp); void qmp_send_response(MonitorQMP *mon, const QDict *rsp);
void monitor_data_destroy_qmp(MonitorQMP *mon); void monitor_data_destroy_qmp(MonitorQMP *mon);
void coroutine_fn monitor_qmp_dispatcher_co(void *data); void coroutine_fn monitor_qmp_dispatcher_co(void *data);
void qmp_dispatcher_co_wake(void);
int get_monitor_def(Monitor *mon, int64_t *pval, const char *name); int get_monitor_def(Monitor *mon, int64_t *pval, const char *name);
void handle_hmp_command(MonitorHMP *mon, const char *cmdline); void handle_hmp_command(MonitorHMP *mon, const char *cmdline);

View File

@ -62,27 +62,6 @@ Coroutine *qmp_dispatcher_co;
*/ */
bool qmp_dispatcher_co_shutdown; bool qmp_dispatcher_co_shutdown;
/*
* qmp_dispatcher_co_busy is used for synchronisation between the
* monitor thread and the main thread to ensure that the dispatcher
* coroutine never gets scheduled a second time when it's already
* scheduled (scheduling the same coroutine twice is forbidden).
*
* It is true if the coroutine is active and processing requests.
* Additional requests may then be pushed onto mon->qmp_requests,
* and @qmp_dispatcher_co_shutdown may be set without further ado.
* @qmp_dispatcher_co_busy must not be woken up in this case.
*
* If false, you also have to set @qmp_dispatcher_co_busy to true and
* wake up @qmp_dispatcher_co after pushing the new requests.
*
* The coroutine will automatically change this variable back to false
* before it yields. Nobody else may set the variable to false.
*
* Access must be atomic for thread safety.
*/
bool qmp_dispatcher_co_busy;
/* /*
* Protects mon_list, monitor_qapi_event_state, coroutine_mon, * Protects mon_list, monitor_qapi_event_state, coroutine_mon,
* monitor_destroyed. * monitor_destroyed.
@ -685,9 +664,7 @@ void monitor_cleanup(void)
WITH_QEMU_LOCK_GUARD(&monitor_lock) { WITH_QEMU_LOCK_GUARD(&monitor_lock) {
qmp_dispatcher_co_shutdown = true; qmp_dispatcher_co_shutdown = true;
} }
if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) { qmp_dispatcher_co_wake();
aio_co_wake(qmp_dispatcher_co);
}
AIO_WAIT_WHILE_UNLOCKED(NULL, AIO_WAIT_WHILE_UNLOCKED(NULL,
(aio_poll(iohandler_get_aio_context(), false), (aio_poll(iohandler_get_aio_context(), false),
@ -742,7 +719,6 @@ void monitor_init_globals(void)
* rid of those assumptions. * rid of those assumptions.
*/ */
qmp_dispatcher_co = qemu_coroutine_create(monitor_qmp_dispatcher_co, NULL); qmp_dispatcher_co = qemu_coroutine_create(monitor_qmp_dispatcher_co, NULL);
qatomic_mb_set(&qmp_dispatcher_co_busy, true);
aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co); aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co);
} }

View File

@ -33,6 +33,27 @@
#include "qapi/qmp/qlist.h" #include "qapi/qmp/qlist.h"
#include "trace.h" #include "trace.h"
/*
* qmp_dispatcher_co_busy is used for synchronisation between the
* monitor thread and the main thread to ensure that the dispatcher
* coroutine never gets scheduled a second time when it's already
* scheduled (scheduling the same coroutine twice is forbidden).
*
* It is true if the coroutine is active and processing requests.
* Additional requests may then be pushed onto mon->qmp_requests,
* and @qmp_dispatcher_co_shutdown may be set without further ado.
* @qmp_dispatcher_co_busy must not be woken up in this case.
*
* If false, you also have to set @qmp_dispatcher_co_busy to true and
* wake up @qmp_dispatcher_co after pushing the new requests.
*
* The coroutine will automatically change this variable back to false
* before it yields. Nobody else may set the variable to false.
*
* Access must be atomic for thread safety.
*/
static bool qmp_dispatcher_co_busy = true;
struct QMPRequest { struct QMPRequest {
/* Owner of the request */ /* Owner of the request */
MonitorQMP *mon; MonitorQMP *mon;
@ -334,6 +355,13 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *data)
qatomic_set(&qmp_dispatcher_co, NULL); qatomic_set(&qmp_dispatcher_co, NULL);
} }
void qmp_dispatcher_co_wake(void)
{
if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) {
aio_co_wake(qmp_dispatcher_co);
}
}
static void handle_qmp_command(void *opaque, QObject *req, Error *err) static void handle_qmp_command(void *opaque, QObject *req, Error *err)
{ {
MonitorQMP *mon = opaque; MonitorQMP *mon = opaque;
@ -395,9 +423,7 @@ static void handle_qmp_command(void *opaque, QObject *req, Error *err)
} }
/* Kick the dispatcher routine */ /* Kick the dispatcher routine */
if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) { qmp_dispatcher_co_wake();
aio_co_wake(qmp_dispatcher_co);
}
} }
static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)