|
@@ -55,24 +55,85 @@ typedef struct {
|
|
|
/* Shared monitor I/O thread */
|
|
|
IOThread *mon_iothread;
|
|
|
|
|
|
-/* Bottom half to dispatch the requests received from I/O thread */
|
|
|
-QEMUBH *qmp_dispatcher_bh;
|
|
|
+/* Coroutine to dispatch the requests received from I/O thread */
|
|
|
+Coroutine *qmp_dispatcher_co;
|
|
|
|
|
|
-/* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
|
|
|
+/* Set to true when the dispatcher coroutine should terminate */
|
|
|
+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,
|
|
|
+ * monitor_destroyed.
|
|
|
+ */
|
|
|
QemuMutex monitor_lock;
|
|
|
static GHashTable *monitor_qapi_event_state;
|
|
|
+static GHashTable *coroutine_mon; /* Maps Coroutine* to Monitor* */
|
|
|
|
|
|
MonitorList mon_list;
|
|
|
int mon_refcount;
|
|
|
static bool monitor_destroyed;
|
|
|
|
|
|
-__thread Monitor *cur_mon;
|
|
|
+Monitor *monitor_cur(void)
|
|
|
+{
|
|
|
+ Monitor *mon;
|
|
|
+
|
|
|
+ qemu_mutex_lock(&monitor_lock);
|
|
|
+ mon = g_hash_table_lookup(coroutine_mon, qemu_coroutine_self());
|
|
|
+ qemu_mutex_unlock(&monitor_lock);
|
|
|
+
|
|
|
+ return mon;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Sets a new current monitor and returns the old one.
|
|
|
+ *
|
|
|
+ * If a non-NULL monitor is set for a coroutine, another call
|
|
|
+ * resetting it to NULL is required before the coroutine terminates,
|
|
|
+ * otherwise a stale entry would remain in the hash table.
|
|
|
+ */
|
|
|
+Monitor *monitor_set_cur(Coroutine *co, Monitor *mon)
|
|
|
+{
|
|
|
+ Monitor *old_monitor = monitor_cur();
|
|
|
+
|
|
|
+ qemu_mutex_lock(&monitor_lock);
|
|
|
+ if (mon) {
|
|
|
+ g_hash_table_replace(coroutine_mon, co, mon);
|
|
|
+ } else {
|
|
|
+ g_hash_table_remove(coroutine_mon, co);
|
|
|
+ }
|
|
|
+ qemu_mutex_unlock(&monitor_lock);
|
|
|
+
|
|
|
+ return old_monitor;
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* Is the current monitor, if any, a QMP monitor?
|
|
|
*/
|
|
|
bool monitor_cur_is_qmp(void)
|
|
|
{
|
|
|
+ Monitor *cur_mon = monitor_cur();
|
|
|
+
|
|
|
return cur_mon && monitor_is_qmp(cur_mon);
|
|
|
}
|
|
|
|
|
@@ -209,6 +270,8 @@ int monitor_printf(Monitor *mon, const char *fmt, ...)
|
|
|
*/
|
|
|
int error_vprintf(const char *fmt, va_list ap)
|
|
|
{
|
|
|
+ Monitor *cur_mon = monitor_cur();
|
|
|
+
|
|
|
if (cur_mon && !monitor_cur_is_qmp()) {
|
|
|
return monitor_vprintf(cur_mon, fmt, ap);
|
|
|
}
|
|
@@ -217,6 +280,8 @@ int error_vprintf(const char *fmt, va_list ap)
|
|
|
|
|
|
int error_vprintf_unless_qmp(const char *fmt, va_list ap)
|
|
|
{
|
|
|
+ Monitor *cur_mon = monitor_cur();
|
|
|
+
|
|
|
if (!cur_mon) {
|
|
|
return vfprintf(stderr, fmt, ap);
|
|
|
}
|
|
@@ -582,9 +647,24 @@ void monitor_cleanup(void)
|
|
|
}
|
|
|
qemu_mutex_unlock(&monitor_lock);
|
|
|
|
|
|
- /* QEMUBHs needs to be deleted before destroying the I/O thread */
|
|
|
- qemu_bh_delete(qmp_dispatcher_bh);
|
|
|
- qmp_dispatcher_bh = NULL;
|
|
|
+ /*
|
|
|
+ * The dispatcher needs to stop before destroying the I/O thread.
|
|
|
+ *
|
|
|
+ * We need to poll both qemu_aio_context and iohandler_ctx to make
|
|
|
+ * sure that the dispatcher coroutine keeps making progress and
|
|
|
+ * eventually terminates. qemu_aio_context is automatically
|
|
|
+ * polled by calling AIO_WAIT_WHILE on it, but we must poll
|
|
|
+ * iohandler_ctx manually.
|
|
|
+ */
|
|
|
+ qmp_dispatcher_co_shutdown = true;
|
|
|
+ if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) {
|
|
|
+ aio_co_wake(qmp_dispatcher_co);
|
|
|
+ }
|
|
|
+
|
|
|
+ AIO_WAIT_WHILE(qemu_get_aio_context(),
|
|
|
+ (aio_poll(iohandler_get_aio_context(), false),
|
|
|
+ qatomic_mb_read(&qmp_dispatcher_co_busy)));
|
|
|
+
|
|
|
if (mon_iothread) {
|
|
|
iothread_destroy(mon_iothread);
|
|
|
mon_iothread = NULL;
|
|
@@ -601,15 +681,16 @@ void monitor_init_globals_core(void)
|
|
|
{
|
|
|
monitor_qapi_event_init();
|
|
|
qemu_mutex_init(&monitor_lock);
|
|
|
+ coroutine_mon = g_hash_table_new(NULL, NULL);
|
|
|
|
|
|
/*
|
|
|
* The dispatcher BH must run in the main loop thread, since we
|
|
|
* have commands assuming that context. It would be nice to get
|
|
|
* rid of those assumptions.
|
|
|
*/
|
|
|
- qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
|
|
|
- monitor_qmp_bh_dispatcher,
|
|
|
- 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);
|
|
|
}
|
|
|
|
|
|
int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
|