diff options
Diffstat (limited to 'vl.c')
-rw-r--r-- | vl.c | 59 |
1 files changed, 47 insertions, 12 deletions
@@ -1365,28 +1365,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) @@ -1395,8 +1424,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); + } } } @@ -2911,7 +2946,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; |