4 * Copyright Red Hat Inc., 2013
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 "block/block.h"
20 #include "sysemu/iothread.h"
21 #include "qmp-commands.h"
22 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
26 typedef ObjectClass IOThreadClass
;
28 #define IOTHREAD_GET_CLASS(obj) \
29 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
30 #define IOTHREAD_CLASS(klass) \
31 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
33 static __thread IOThread
*my_iothread
;
35 AioContext
*qemu_get_current_aio_context(void)
37 return my_iothread
? my_iothread
->ctx
: qemu_get_aio_context();
40 static void *iothread_run(void *opaque
)
42 IOThread
*iothread
= opaque
;
45 rcu_register_thread();
47 my_iothread
= iothread
;
48 qemu_mutex_lock(&iothread
->init_done_lock
);
49 iothread
->thread_id
= qemu_get_thread_id();
50 qemu_cond_signal(&iothread
->init_done_cond
);
51 qemu_mutex_unlock(&iothread
->init_done_lock
);
53 while (!iothread
->stopping
) {
54 aio_context_acquire(iothread
->ctx
);
56 while (!iothread
->stopping
&& aio_poll(iothread
->ctx
, blocking
)) {
57 /* Progress was made, keep going */
60 aio_context_release(iothread
->ctx
);
63 rcu_unregister_thread();
67 static int iothread_stop(Object
*object
, void *opaque
)
71 iothread
= (IOThread
*)object_dynamic_cast(object
, TYPE_IOTHREAD
);
72 if (!iothread
|| !iothread
->ctx
) {
75 iothread
->stopping
= true;
76 aio_notify(iothread
->ctx
);
77 qemu_thread_join(&iothread
->thread
);
81 static void iothread_instance_finalize(Object
*obj
)
83 IOThread
*iothread
= IOTHREAD(obj
);
85 iothread_stop(obj
, NULL
);
86 qemu_cond_destroy(&iothread
->init_done_cond
);
87 qemu_mutex_destroy(&iothread
->init_done_lock
);
91 aio_context_unref(iothread
->ctx
);
94 static void iothread_complete(UserCreatable
*obj
, Error
**errp
)
96 Error
*local_error
= NULL
;
97 IOThread
*iothread
= IOTHREAD(obj
);
98 char *name
, *thread_name
;
100 iothread
->stopping
= false;
101 iothread
->thread_id
= -1;
102 iothread
->ctx
= aio_context_new(&local_error
);
103 if (!iothread
->ctx
) {
104 error_propagate(errp
, local_error
);
108 qemu_mutex_init(&iothread
->init_done_lock
);
109 qemu_cond_init(&iothread
->init_done_cond
);
111 /* This assumes we are called from a thread with useful CPU affinity for us
114 name
= object_get_canonical_path_component(OBJECT(obj
));
115 thread_name
= g_strdup_printf("IO %s", name
);
116 qemu_thread_create(&iothread
->thread
, thread_name
, iothread_run
,
117 iothread
, QEMU_THREAD_JOINABLE
);
121 /* Wait for initialization to complete */
122 qemu_mutex_lock(&iothread
->init_done_lock
);
123 while (iothread
->thread_id
== -1) {
124 qemu_cond_wait(&iothread
->init_done_cond
,
125 &iothread
->init_done_lock
);
127 qemu_mutex_unlock(&iothread
->init_done_lock
);
130 static void iothread_class_init(ObjectClass
*klass
, void *class_data
)
132 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(klass
);
133 ucc
->complete
= iothread_complete
;
136 static const TypeInfo iothread_info
= {
137 .name
= TYPE_IOTHREAD
,
138 .parent
= TYPE_OBJECT
,
139 .class_init
= iothread_class_init
,
140 .instance_size
= sizeof(IOThread
),
141 .instance_finalize
= iothread_instance_finalize
,
142 .interfaces
= (InterfaceInfo
[]) {
143 {TYPE_USER_CREATABLE
},
148 static void iothread_register_types(void)
150 type_register_static(&iothread_info
);
153 type_init(iothread_register_types
)
155 char *iothread_get_id(IOThread
*iothread
)
157 return object_get_canonical_path_component(OBJECT(iothread
));
160 AioContext
*iothread_get_aio_context(IOThread
*iothread
)
162 return iothread
->ctx
;
165 static int query_one_iothread(Object
*object
, void *opaque
)
167 IOThreadInfoList
***prev
= opaque
;
168 IOThreadInfoList
*elem
;
172 iothread
= (IOThread
*)object_dynamic_cast(object
, TYPE_IOTHREAD
);
177 info
= g_new0(IOThreadInfo
, 1);
178 info
->id
= iothread_get_id(iothread
);
179 info
->thread_id
= iothread
->thread_id
;
181 elem
= g_new0(IOThreadInfoList
, 1);
190 IOThreadInfoList
*qmp_query_iothreads(Error
**errp
)
192 IOThreadInfoList
*head
= NULL
;
193 IOThreadInfoList
**prev
= &head
;
194 Object
*container
= object_get_objects_root();
196 object_child_foreach(container
, query_one_iothread
, &prev
);
200 void iothread_stop_all(void)
202 Object
*container
= object_get_objects_root();
203 BlockDriverState
*bs
;
206 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
207 AioContext
*ctx
= bdrv_get_aio_context(bs
);
208 if (ctx
== qemu_get_aio_context()) {
211 aio_context_acquire(ctx
);
212 bdrv_set_aio_context(bs
, qemu_get_aio_context());
213 aio_context_release(ctx
);
216 object_child_foreach(container
, iothread_stop
, NULL
);