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 "sysemu/iothread.h"
20 #include "qmp-commands.h"
21 #include "qemu/error-report.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
;
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
);
55 while (!iothread
->stopping
&& aio_poll(iothread
->ctx
, blocking
)) {
56 /* Progress was made, keep going */
59 aio_context_release(iothread
->ctx
);
62 rcu_unregister_thread();
66 static int iothread_stop(Object
*object
, void *opaque
)
70 iothread
= (IOThread
*)object_dynamic_cast(object
, TYPE_IOTHREAD
);
71 if (!iothread
|| !iothread
->ctx
) {
74 iothread
->stopping
= true;
75 aio_notify(iothread
->ctx
);
76 qemu_thread_join(&iothread
->thread
);
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
);
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
);
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
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
);
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
;
171 iothread
= (IOThread
*)object_dynamic_cast(object
, TYPE_IOTHREAD
);
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);
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
);
199 void iothread_stop_all(void)
201 Object
*container
= object_get_objects_root();
203 object_child_foreach(container
, iothread_stop
, NULL
);