From patchwork Thu Jun 20 17:35:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 11007563 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 463496C5 for ; Thu, 20 Jun 2019 17:53:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 37EA2287E7 for ; Thu, 20 Jun 2019 17:53:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2936B1FF62; Thu, 20 Jun 2019 17:53:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B5A211FF62 for ; Thu, 20 Jun 2019 17:53:57 +0000 (UTC) Received: from localhost ([::1]:51936 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1he1G0-0005CD-Q5 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 20 Jun 2019 13:53:56 -0400 Received: from eggs.gnu.org ([209.51.188.92]:34757) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1he19S-00078z-Tr for qemu-devel@nongnu.org; Thu, 20 Jun 2019 13:47:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1he0zy-0004Us-Ts for qemu-devel@nongnu.org; Thu, 20 Jun 2019 13:37:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42606) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1he0zy-0004RF-N4 for qemu-devel@nongnu.org; Thu, 20 Jun 2019 13:37:22 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 39E2C3098576; Thu, 20 Jun 2019 17:37:17 +0000 (UTC) Received: from localhost (ovpn-116-108.ams2.redhat.com [10.36.116.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id A2D5C5D71C; Thu, 20 Jun 2019 17:37:11 +0000 (UTC) From: Stefan Hajnoczi To: qemu-devel@nongnu.org Date: Thu, 20 Jun 2019 18:35:30 +0100 Message-Id: <20190620173532.14468-2-stefanha@redhat.com> In-Reply-To: <20190620173532.14468-1-stefanha@redhat.com> References: <20190620173532.14468-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Thu, 20 Jun 2019 17:37:17 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v5 1/3] vl: add qemu_add_vm_change_state_handler_prio() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Paolo Bonzini , Stefan Hajnoczi , Kevin Wolf , "Michael S. Tsirkin" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add an API for registering vm change state handlers with a well-defined ordering. This is necessary when handlers depend on each other. Small coding style fixes are included to make checkpatch.pl happy. Signed-off-by: Stefan Hajnoczi --- include/sysemu/sysemu.h | 2 ++ vl.c | 59 ++++++++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h index 61579ae71e..984c439ac9 100644 --- a/include/sysemu/sysemu.h +++ b/include/sysemu/sysemu.h @@ -29,6 +29,8 @@ typedef void VMChangeStateHandler(void *opaque, int running, RunState state); VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, void *opaque); +VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( + VMChangeStateHandler *cb, void *opaque, int priority); void qemu_del_vm_change_state_handler(VMChangeStateEntry *e); void vm_state_notify(int running, RunState state); diff --git a/vl.c b/vl.c index 99a56b5556..7fac2ae7ca 100644 --- a/vl.c +++ b/vl.c @@ -1471,28 +1471,57 @@ static int machine_help_func(QemuOpts *opts, MachineState *machine) struct vm_change_state_entry { VMChangeStateHandler *cb; void *opaque; - QLIST_ENTRY (vm_change_state_entry) entries; + QTAILQ_ENTRY(vm_change_state_entry) entries; + int priority; }; -static QLIST_HEAD(, vm_change_state_entry) vm_change_state_head; +static QTAILQ_HEAD(, vm_change_state_entry) vm_change_state_head; -VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, - void *opaque) +/** + * qemu_add_vm_change_state_handler_prio: + * @cb: the callback to invoke + * @opaque: user data passed to the callback + * @priority: low priorities execute first when the vm runs and the reverse is + * true when the vm stops + * + * Register a callback function that is invoked when the vm starts or stops + * running. + * + * Returns: an entry to be freed using qemu_del_vm_change_state_handler() + */ +VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( + VMChangeStateHandler *cb, void *opaque, int priority) { VMChangeStateEntry *e; + VMChangeStateEntry *other; - e = g_malloc0(sizeof (*e)); - + e = g_malloc0(sizeof(*e)); e->cb = cb; e->opaque = opaque; - QLIST_INSERT_HEAD(&vm_change_state_head, e, entries); + e->priority = priority; + + /* Keep list sorted in ascending priority order */ + QTAILQ_FOREACH(other, &vm_change_state_head, entries) { + if (priority < other->priority) { + QTAILQ_INSERT_BEFORE(other, e, entries); + return e; + } + } + + QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries); return e; } +VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, + void *opaque) +{ + return qemu_add_vm_change_state_handler_prio(cb, opaque, 0); +} + void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) { - QLIST_REMOVE (e, entries); - g_free (e); + QTAILQ_REMOVE(&vm_change_state_head, e, entries); + g_free(e); } void vm_state_notify(int running, RunState state) @@ -1501,8 +1530,14 @@ void vm_state_notify(int running, RunState state) trace_vm_state_notify(running, state, RunState_str(state)); - QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { - e->cb(e->opaque, running, state); + if (running) { + QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { + e->cb(e->opaque, running, state); + } + } else { + QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) { + e->cb(e->opaque, running, state); + } } } @@ -3025,7 +3060,7 @@ int main(int argc, char **argv, char **envp) exit(1); } - QLIST_INIT (&vm_change_state_head); + QTAILQ_INIT(&vm_change_state_head); os_setup_early_signal_handling(); cpu_option = NULL;