esp: convert ti_buf from array to Fifo8
[qemu/ar7.git] / util / yank.c
blobfc08f65209a3c7ef7eecc7406d1e9c47eaf81e47
1 /*
2 * QEMU yank feature
4 * Copyright (c) Lukas Straub <lukasstraub2@web.de>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "qemu/thread.h"
13 #include "qemu/queue.h"
14 #include "qemu/lockable.h"
15 #include "qapi/qapi-commands-yank.h"
16 #include "qapi/qapi-visit-yank.h"
17 #include "qapi/clone-visitor.h"
18 #include "io/channel.h"
19 #include "qemu/yank.h"
21 struct YankFuncAndParam {
22 YankFn *func;
23 void *opaque;
24 QLIST_ENTRY(YankFuncAndParam) next;
27 struct YankInstanceEntry {
28 YankInstance *instance;
29 QLIST_HEAD(, YankFuncAndParam) yankfns;
30 QLIST_ENTRY(YankInstanceEntry) next;
33 typedef struct YankFuncAndParam YankFuncAndParam;
34 typedef struct YankInstanceEntry YankInstanceEntry;
37 * This lock protects the yank_instance_list below. Because it's taken by
38 * OOB-capable commands, it must be "fast", i.e. it may only be held for a
39 * bounded, short time. See docs/devel/qapi-code-gen.txt for additional
40 * information.
42 static QemuMutex yank_lock;
44 static QLIST_HEAD(, YankInstanceEntry) yank_instance_list
45 = QLIST_HEAD_INITIALIZER(yank_instance_list);
47 static bool yank_instance_equal(const YankInstance *a, const YankInstance *b)
49 if (a->type != b->type) {
50 return false;
53 switch (a->type) {
54 case YANK_INSTANCE_TYPE_BLOCK_NODE:
55 return g_str_equal(a->u.block_node.node_name,
56 b->u.block_node.node_name);
58 case YANK_INSTANCE_TYPE_CHARDEV:
59 return g_str_equal(a->u.chardev.id, b->u.chardev.id);
61 case YANK_INSTANCE_TYPE_MIGRATION:
62 return true;
64 default:
65 abort();
69 static YankInstanceEntry *yank_find_entry(const YankInstance *instance)
71 YankInstanceEntry *entry;
73 QLIST_FOREACH(entry, &yank_instance_list, next) {
74 if (yank_instance_equal(entry->instance, instance)) {
75 return entry;
78 return NULL;
81 bool yank_register_instance(const YankInstance *instance, Error **errp)
83 YankInstanceEntry *entry;
85 QEMU_LOCK_GUARD(&yank_lock);
87 if (yank_find_entry(instance)) {
88 error_setg(errp, "duplicate yank instance");
89 return false;
92 entry = g_new0(YankInstanceEntry, 1);
93 entry->instance = QAPI_CLONE(YankInstance, instance);
94 QLIST_INIT(&entry->yankfns);
95 QLIST_INSERT_HEAD(&yank_instance_list, entry, next);
97 return true;
100 void yank_unregister_instance(const YankInstance *instance)
102 YankInstanceEntry *entry;
104 QEMU_LOCK_GUARD(&yank_lock);
105 entry = yank_find_entry(instance);
106 assert(entry);
108 assert(QLIST_EMPTY(&entry->yankfns));
109 QLIST_REMOVE(entry, next);
110 qapi_free_YankInstance(entry->instance);
111 g_free(entry);
114 void yank_register_function(const YankInstance *instance,
115 YankFn *func,
116 void *opaque)
118 YankInstanceEntry *entry;
119 YankFuncAndParam *func_entry;
121 QEMU_LOCK_GUARD(&yank_lock);
122 entry = yank_find_entry(instance);
123 assert(entry);
125 func_entry = g_new0(YankFuncAndParam, 1);
126 func_entry->func = func;
127 func_entry->opaque = opaque;
129 QLIST_INSERT_HEAD(&entry->yankfns, func_entry, next);
132 void yank_unregister_function(const YankInstance *instance,
133 YankFn *func,
134 void *opaque)
136 YankInstanceEntry *entry;
137 YankFuncAndParam *func_entry;
139 QEMU_LOCK_GUARD(&yank_lock);
140 entry = yank_find_entry(instance);
141 assert(entry);
143 QLIST_FOREACH(func_entry, &entry->yankfns, next) {
144 if (func_entry->func == func && func_entry->opaque == opaque) {
145 QLIST_REMOVE(func_entry, next);
146 g_free(func_entry);
147 return;
151 abort();
154 void yank_generic_iochannel(void *opaque)
156 QIOChannel *ioc = QIO_CHANNEL(opaque);
158 qio_channel_shutdown(ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
161 void qmp_yank(YankInstanceList *instances,
162 Error **errp)
164 YankInstanceList *tail;
165 YankInstanceEntry *entry;
166 YankFuncAndParam *func_entry;
168 QEMU_LOCK_GUARD(&yank_lock);
169 for (tail = instances; tail; tail = tail->next) {
170 entry = yank_find_entry(tail->value);
171 if (!entry) {
172 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, "Instance not found");
173 return;
176 for (tail = instances; tail; tail = tail->next) {
177 entry = yank_find_entry(tail->value);
178 assert(entry);
179 QLIST_FOREACH(func_entry, &entry->yankfns, next) {
180 func_entry->func(func_entry->opaque);
185 YankInstanceList *qmp_query_yank(Error **errp)
187 YankInstanceEntry *entry;
188 YankInstanceList *ret;
190 ret = NULL;
192 QEMU_LOCK_GUARD(&yank_lock);
193 QLIST_FOREACH(entry, &yank_instance_list, next) {
194 YankInstanceList *new_entry;
195 new_entry = g_new0(YankInstanceList, 1);
196 new_entry->value = QAPI_CLONE(YankInstance, entry->instance);
197 new_entry->next = ret;
198 ret = new_entry;
201 return ret;
204 static void __attribute__((__constructor__)) yank_init(void)
206 qemu_mutex_init(&yank_lock);