aio: introduce qemu_get_current_aio_context
[qemu/kevin.git] / iothread.c
blob62c87966198067879304f7f05b48ccfc71e6ef13
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 "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"
25 typedef ObjectClass IOThreadClass;
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)
32 static __thread IOThread *my_iothread;
34 AioContext *qemu_get_current_aio_context(void)
36 return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
39 static void *iothread_run(void *opaque)
41 IOThread *iothread = opaque;
42 bool blocking;
44 rcu_register_thread();
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);
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;
59 aio_context_release(iothread->ctx);
62 rcu_unregister_thread();
63 return NULL;
66 static int iothread_stop(Object *object, void *opaque)
68 IOThread *iothread;
70 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
71 if (!iothread || !iothread->ctx) {
72 return 0;
74 iothread->stopping = true;
75 aio_notify(iothread->ctx);
76 qemu_thread_join(&iothread->thread);
77 return 0;
80 static void iothread_instance_finalize(Object *obj)
82 IOThread *iothread = IOTHREAD(obj);
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;
90 aio_context_unref(iothread->ctx);
93 static void iothread_complete(UserCreatable *obj, Error **errp)
95 Error *local_error = NULL;
96 IOThread *iothread = IOTHREAD(obj);
97 char *name, *thread_name;
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;
107 qemu_mutex_init(&iothread->init_done_lock);
108 qemu_cond_init(&iothread->init_done_cond);
110 /* This assumes we are called from a thread with useful CPU affinity for us
111 * to inherit.
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);
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);
126 qemu_mutex_unlock(&iothread->init_done_lock);
129 static void iothread_class_init(ObjectClass *klass, void *class_data)
131 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
132 ucc->complete = iothread_complete;
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},
147 static void iothread_register_types(void)
149 type_register_static(&iothread_info);
152 type_init(iothread_register_types)
154 char *iothread_get_id(IOThread *iothread)
156 return object_get_canonical_path_component(OBJECT(iothread));
159 AioContext *iothread_get_aio_context(IOThread *iothread)
161 return iothread->ctx;
164 static int query_one_iothread(Object *object, void *opaque)
166 IOThreadInfoList ***prev = opaque;
167 IOThreadInfoList *elem;
168 IOThreadInfo *info;
169 IOThread *iothread;
171 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
172 if (!iothread) {
173 return 0;
176 info = g_new0(IOThreadInfo, 1);
177 info->id = iothread_get_id(iothread);
178 info->thread_id = iothread->thread_id;
180 elem = g_new0(IOThreadInfoList, 1);
181 elem->value = info;
182 elem->next = NULL;
184 **prev = elem;
185 *prev = &elem->next;
186 return 0;
189 IOThreadInfoList *qmp_query_iothreads(Error **errp)
191 IOThreadInfoList *head = NULL;
192 IOThreadInfoList **prev = &head;
193 Object *container = object_get_objects_root();
195 object_child_foreach(container, query_one_iothread, &prev);
196 return head;
199 void iothread_stop_all(void)
201 Object *container = object_get_objects_root();
203 object_child_foreach(container, iothread_stop, NULL);