block/iscsi: bump year in copyright notice
[qemu/kevin.git] / iothread.c
bloba1f91099bc69900627b99d89caebcee013d9df8f
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"
20 #include "qemu/error-report.h"
22 #define IOTHREADS_PATH "/objects"
24 typedef ObjectClass IOThreadClass;
26 #define IOTHREAD_GET_CLASS(obj) \
27 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
28 #define IOTHREAD_CLASS(klass) \
29 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
31 static void *iothread_run(void *opaque)
33 IOThread *iothread = opaque;
35 qemu_mutex_lock(&iothread->init_done_lock);
36 iothread->thread_id = qemu_get_thread_id();
37 qemu_cond_signal(&iothread->init_done_cond);
38 qemu_mutex_unlock(&iothread->init_done_lock);
40 while (!atomic_read(&iothread->stopping)) {
41 aio_poll(iothread->ctx, true);
43 return NULL;
46 static void iothread_instance_finalize(Object *obj)
48 IOThread *iothread = IOTHREAD(obj);
50 if (!iothread->ctx) {
51 return;
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 Error *local_error = NULL;
64 IOThread *iothread = IOTHREAD(obj);
66 iothread->stopping = false;
67 iothread->thread_id = -1;
68 iothread->ctx = aio_context_new(&local_error);
69 if (!iothread->ctx) {
70 error_propagate(errp, local_error);
71 return;
74 qemu_mutex_init(&iothread->init_done_lock);
75 qemu_cond_init(&iothread->init_done_cond);
77 /* This assumes we are called from a thread with useful CPU affinity for us
78 * to inherit.
80 qemu_thread_create(&iothread->thread, "iothread", iothread_run,
81 iothread, QEMU_THREAD_JOINABLE);
83 /* Wait for initialization to complete */
84 qemu_mutex_lock(&iothread->init_done_lock);
85 while (iothread->thread_id == -1) {
86 qemu_cond_wait(&iothread->init_done_cond,
87 &iothread->init_done_lock);
89 qemu_mutex_unlock(&iothread->init_done_lock);
92 static void iothread_class_init(ObjectClass *klass, void *class_data)
94 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
95 ucc->complete = iothread_complete;
98 static const TypeInfo iothread_info = {
99 .name = TYPE_IOTHREAD,
100 .parent = TYPE_OBJECT,
101 .class_init = iothread_class_init,
102 .instance_size = sizeof(IOThread),
103 .instance_finalize = iothread_instance_finalize,
104 .interfaces = (InterfaceInfo[]) {
105 {TYPE_USER_CREATABLE},
110 static void iothread_register_types(void)
112 type_register_static(&iothread_info);
115 type_init(iothread_register_types)
117 IOThread *iothread_find(const char *id)
119 Object *container = container_get(object_get_root(), IOTHREADS_PATH);
120 Object *child;
122 child = object_property_get_link(container, id, NULL);
123 if (!child) {
124 return NULL;
126 return (IOThread *)object_dynamic_cast(child, TYPE_IOTHREAD);
129 char *iothread_get_id(IOThread *iothread)
131 return object_get_canonical_path_component(OBJECT(iothread));
134 AioContext *iothread_get_aio_context(IOThread *iothread)
136 return iothread->ctx;
139 static int query_one_iothread(Object *object, void *opaque)
141 IOThreadInfoList ***prev = opaque;
142 IOThreadInfoList *elem;
143 IOThreadInfo *info;
144 IOThread *iothread;
146 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
147 if (!iothread) {
148 return 0;
151 info = g_new0(IOThreadInfo, 1);
152 info->id = iothread_get_id(iothread);
153 info->thread_id = iothread->thread_id;
155 elem = g_new0(IOThreadInfoList, 1);
156 elem->value = info;
157 elem->next = NULL;
159 **prev = elem;
160 *prev = &elem->next;
161 return 0;
164 IOThreadInfoList *qmp_query_iothreads(Error **errp)
166 IOThreadInfoList *head = NULL;
167 IOThreadInfoList **prev = &head;
168 Object *container = container_get(object_get_root(), IOTHREADS_PATH);
170 object_child_foreach(container, query_one_iothread, &prev);
171 return head;