]> git.proxmox.com Git - mirror_qemu.git/blob - iothread.c
aio: introduce qemu_get_current_aio_context
[mirror_qemu.git] / iothread.c
1 /*
2 * Event loop thread
3 *
4 * Copyright Red Hat Inc., 2013
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
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.
11 *
12 */
13
14 #include "qemu/osdep.h"
15 #include "qom/object.h"
16 #include "qom/object_interfaces.h"
17 #include "qemu/module.h"
18 #include "block/aio.h"
19 #include "sysemu/iothread.h"
20 #include "qmp-commands.h"
21 #include "qemu/error-report.h"
22 #include "qemu/rcu.h"
23 #include "qemu/main-loop.h"
24
25 typedef ObjectClass IOThreadClass;
26
27 #define IOTHREAD_GET_CLASS(obj) \
28 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
29 #define IOTHREAD_CLASS(klass) \
30 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
31
32 static __thread IOThread *my_iothread;
33
34 AioContext *qemu_get_current_aio_context(void)
35 {
36 return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
37 }
38
39 static void *iothread_run(void *opaque)
40 {
41 IOThread *iothread = opaque;
42 bool blocking;
43
44 rcu_register_thread();
45
46 my_iothread = iothread;
47 qemu_mutex_lock(&iothread->init_done_lock);
48 iothread->thread_id = qemu_get_thread_id();
49 qemu_cond_signal(&iothread->init_done_cond);
50 qemu_mutex_unlock(&iothread->init_done_lock);
51
52 while (!iothread->stopping) {
53 aio_context_acquire(iothread->ctx);
54 blocking = true;
55 while (!iothread->stopping && aio_poll(iothread->ctx, blocking)) {
56 /* Progress was made, keep going */
57 blocking = false;
58 }
59 aio_context_release(iothread->ctx);
60 }
61
62 rcu_unregister_thread();
63 return NULL;
64 }
65
66 static int iothread_stop(Object *object, void *opaque)
67 {
68 IOThread *iothread;
69
70 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
71 if (!iothread || !iothread->ctx) {
72 return 0;
73 }
74 iothread->stopping = true;
75 aio_notify(iothread->ctx);
76 qemu_thread_join(&iothread->thread);
77 return 0;
78 }
79
80 static void iothread_instance_finalize(Object *obj)
81 {
82 IOThread *iothread = IOTHREAD(obj);
83
84 iothread_stop(obj, NULL);
85 qemu_cond_destroy(&iothread->init_done_cond);
86 qemu_mutex_destroy(&iothread->init_done_lock);
87 if (!iothread->ctx) {
88 return;
89 }
90 aio_context_unref(iothread->ctx);
91 }
92
93 static void iothread_complete(UserCreatable *obj, Error **errp)
94 {
95 Error *local_error = NULL;
96 IOThread *iothread = IOTHREAD(obj);
97 char *name, *thread_name;
98
99 iothread->stopping = false;
100 iothread->thread_id = -1;
101 iothread->ctx = aio_context_new(&local_error);
102 if (!iothread->ctx) {
103 error_propagate(errp, local_error);
104 return;
105 }
106
107 qemu_mutex_init(&iothread->init_done_lock);
108 qemu_cond_init(&iothread->init_done_cond);
109
110 /* This assumes we are called from a thread with useful CPU affinity for us
111 * to inherit.
112 */
113 name = object_get_canonical_path_component(OBJECT(obj));
114 thread_name = g_strdup_printf("IO %s", name);
115 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
116 iothread, QEMU_THREAD_JOINABLE);
117 g_free(thread_name);
118 g_free(name);
119
120 /* Wait for initialization to complete */
121 qemu_mutex_lock(&iothread->init_done_lock);
122 while (iothread->thread_id == -1) {
123 qemu_cond_wait(&iothread->init_done_cond,
124 &iothread->init_done_lock);
125 }
126 qemu_mutex_unlock(&iothread->init_done_lock);
127 }
128
129 static void iothread_class_init(ObjectClass *klass, void *class_data)
130 {
131 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
132 ucc->complete = iothread_complete;
133 }
134
135 static const TypeInfo iothread_info = {
136 .name = TYPE_IOTHREAD,
137 .parent = TYPE_OBJECT,
138 .class_init = iothread_class_init,
139 .instance_size = sizeof(IOThread),
140 .instance_finalize = iothread_instance_finalize,
141 .interfaces = (InterfaceInfo[]) {
142 {TYPE_USER_CREATABLE},
143 {}
144 },
145 };
146
147 static void iothread_register_types(void)
148 {
149 type_register_static(&iothread_info);
150 }
151
152 type_init(iothread_register_types)
153
154 char *iothread_get_id(IOThread *iothread)
155 {
156 return object_get_canonical_path_component(OBJECT(iothread));
157 }
158
159 AioContext *iothread_get_aio_context(IOThread *iothread)
160 {
161 return iothread->ctx;
162 }
163
164 static int query_one_iothread(Object *object, void *opaque)
165 {
166 IOThreadInfoList ***prev = opaque;
167 IOThreadInfoList *elem;
168 IOThreadInfo *info;
169 IOThread *iothread;
170
171 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
172 if (!iothread) {
173 return 0;
174 }
175
176 info = g_new0(IOThreadInfo, 1);
177 info->id = iothread_get_id(iothread);
178 info->thread_id = iothread->thread_id;
179
180 elem = g_new0(IOThreadInfoList, 1);
181 elem->value = info;
182 elem->next = NULL;
183
184 **prev = elem;
185 *prev = &elem->next;
186 return 0;
187 }
188
189 IOThreadInfoList *qmp_query_iothreads(Error **errp)
190 {
191 IOThreadInfoList *head = NULL;
192 IOThreadInfoList **prev = &head;
193 Object *container = object_get_objects_root();
194
195 object_child_foreach(container, query_one_iothread, &prev);
196 return head;
197 }
198
199 void iothread_stop_all(void)
200 {
201 Object *container = object_get_objects_root();
202
203 object_child_foreach(container, iothread_stop, NULL);
204 }