net: Make qmp_query_rx_filter() with name argument more obvious
[qemu/ar7.git] / iothread.c
blob1fbf9f1c49e99570daa40cfda541a4bafe475ce3
1 /*
2 * Event loop thread
4 * Copyright Red Hat Inc., 2013
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qom/object.h"
15 #include "qom/object_interfaces.h"
16 #include "qemu/module.h"
17 #include "block/aio.h"
18 #include "sysemu/iothread.h"
19 #include "qmp-commands.h"
21 #define IOTHREADS_PATH "/objects"
23 typedef ObjectClass IOThreadClass;
25 #define IOTHREAD_GET_CLASS(obj) \
26 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
27 #define IOTHREAD_CLASS(klass) \
28 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
30 static void *iothread_run(void *opaque)
32 IOThread *iothread = opaque;
34 qemu_mutex_lock(&iothread->init_done_lock);
35 iothread->thread_id = qemu_get_thread_id();
36 qemu_cond_signal(&iothread->init_done_cond);
37 qemu_mutex_unlock(&iothread->init_done_lock);
39 while (!iothread->stopping) {
40 aio_context_acquire(iothread->ctx);
41 while (!iothread->stopping && aio_poll(iothread->ctx, true)) {
42 /* Progress was made, keep going */
44 aio_context_release(iothread->ctx);
46 return NULL;
49 static void iothread_instance_finalize(Object *obj)
51 IOThread *iothread = IOTHREAD(obj);
53 iothread->stopping = true;
54 aio_notify(iothread->ctx);
55 qemu_thread_join(&iothread->thread);
56 qemu_cond_destroy(&iothread->init_done_cond);
57 qemu_mutex_destroy(&iothread->init_done_lock);
58 aio_context_unref(iothread->ctx);
61 static void iothread_complete(UserCreatable *obj, Error **errp)
63 IOThread *iothread = IOTHREAD(obj);
65 iothread->stopping = false;
66 iothread->ctx = aio_context_new();
67 iothread->thread_id = -1;
69 qemu_mutex_init(&iothread->init_done_lock);
70 qemu_cond_init(&iothread->init_done_cond);
72 /* This assumes we are called from a thread with useful CPU affinity for us
73 * to inherit.
75 qemu_thread_create(&iothread->thread, "iothread", iothread_run,
76 iothread, QEMU_THREAD_JOINABLE);
78 /* Wait for initialization to complete */
79 qemu_mutex_lock(&iothread->init_done_lock);
80 while (iothread->thread_id == -1) {
81 qemu_cond_wait(&iothread->init_done_cond,
82 &iothread->init_done_lock);
84 qemu_mutex_unlock(&iothread->init_done_lock);
87 static void iothread_class_init(ObjectClass *klass, void *class_data)
89 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
90 ucc->complete = iothread_complete;
93 static const TypeInfo iothread_info = {
94 .name = TYPE_IOTHREAD,
95 .parent = TYPE_OBJECT,
96 .class_init = iothread_class_init,
97 .instance_size = sizeof(IOThread),
98 .instance_finalize = iothread_instance_finalize,
99 .interfaces = (InterfaceInfo[]) {
100 {TYPE_USER_CREATABLE},
105 static void iothread_register_types(void)
107 type_register_static(&iothread_info);
110 type_init(iothread_register_types)
112 IOThread *iothread_find(const char *id)
114 Object *container = container_get(object_get_root(), IOTHREADS_PATH);
115 Object *child;
117 child = object_property_get_link(container, id, NULL);
118 if (!child) {
119 return NULL;
121 return (IOThread *)object_dynamic_cast(child, TYPE_IOTHREAD);
124 char *iothread_get_id(IOThread *iothread)
126 return object_get_canonical_path_component(OBJECT(iothread));
129 AioContext *iothread_get_aio_context(IOThread *iothread)
131 return iothread->ctx;
134 static int query_one_iothread(Object *object, void *opaque)
136 IOThreadInfoList ***prev = opaque;
137 IOThreadInfoList *elem;
138 IOThreadInfo *info;
139 IOThread *iothread;
141 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
142 if (!iothread) {
143 return 0;
146 info = g_new0(IOThreadInfo, 1);
147 info->id = iothread_get_id(iothread);
148 info->thread_id = iothread->thread_id;
150 elem = g_new0(IOThreadInfoList, 1);
151 elem->value = info;
152 elem->next = NULL;
154 **prev = elem;
155 *prev = &elem->next;
156 return 0;
159 IOThreadInfoList *qmp_query_iothreads(Error **errp)
161 IOThreadInfoList *head = NULL;
162 IOThreadInfoList **prev = &head;
163 Object *container = container_get(object_get_root(), IOTHREADS_PATH);
165 object_child_foreach(container, query_one_iothread, &prev);
166 return head;