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 "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"
23 typedef ObjectClass IOThreadClass
;
25 #define IOTHREAD_GET_CLASS(obj) \
26 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
27 #define IOTHREAD_CLASS(klass) \
28 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
30 static void *iothread_run(void *opaque
)
32 IOThread
*iothread
= opaque
;
35 rcu_register_thread();
37 qemu_mutex_lock(&iothread
->init_done_lock
);
38 iothread
->thread_id
= qemu_get_thread_id();
39 qemu_cond_signal(&iothread
->init_done_cond
);
40 qemu_mutex_unlock(&iothread
->init_done_lock
);
42 while (!iothread
->stopping
) {
43 aio_context_acquire(iothread
->ctx
);
45 while (!iothread
->stopping
&& aio_poll(iothread
->ctx
, blocking
)) {
46 /* Progress was made, keep going */
49 aio_context_release(iothread
->ctx
);
52 rcu_unregister_thread();
56 static void iothread_instance_finalize(Object
*obj
)
58 IOThread
*iothread
= IOTHREAD(obj
);
63 iothread
->stopping
= true;
64 aio_notify(iothread
->ctx
);
65 qemu_thread_join(&iothread
->thread
);
66 qemu_cond_destroy(&iothread
->init_done_cond
);
67 qemu_mutex_destroy(&iothread
->init_done_lock
);
68 aio_context_unref(iothread
->ctx
);
71 static void iothread_complete(UserCreatable
*obj
, Error
**errp
)
73 Error
*local_error
= NULL
;
74 IOThread
*iothread
= IOTHREAD(obj
);
75 char *name
, *thread_name
;
77 iothread
->stopping
= false;
78 iothread
->thread_id
= -1;
79 iothread
->ctx
= aio_context_new(&local_error
);
81 error_propagate(errp
, local_error
);
85 qemu_mutex_init(&iothread
->init_done_lock
);
86 qemu_cond_init(&iothread
->init_done_cond
);
88 /* This assumes we are called from a thread with useful CPU affinity for us
91 name
= object_get_canonical_path_component(OBJECT(obj
));
92 thread_name
= g_strdup_printf("IO %s", name
);
93 qemu_thread_create(&iothread
->thread
, thread_name
, iothread_run
,
94 iothread
, QEMU_THREAD_JOINABLE
);
98 /* Wait for initialization to complete */
99 qemu_mutex_lock(&iothread
->init_done_lock
);
100 while (iothread
->thread_id
== -1) {
101 qemu_cond_wait(&iothread
->init_done_cond
,
102 &iothread
->init_done_lock
);
104 qemu_mutex_unlock(&iothread
->init_done_lock
);
107 static void iothread_class_init(ObjectClass
*klass
, void *class_data
)
109 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(klass
);
110 ucc
->complete
= iothread_complete
;
113 static const TypeInfo iothread_info
= {
114 .name
= TYPE_IOTHREAD
,
115 .parent
= TYPE_OBJECT
,
116 .class_init
= iothread_class_init
,
117 .instance_size
= sizeof(IOThread
),
118 .instance_finalize
= iothread_instance_finalize
,
119 .interfaces
= (InterfaceInfo
[]) {
120 {TYPE_USER_CREATABLE
},
125 static void iothread_register_types(void)
127 type_register_static(&iothread_info
);
130 type_init(iothread_register_types
)
132 char *iothread_get_id(IOThread
*iothread
)
134 return object_get_canonical_path_component(OBJECT(iothread
));
137 AioContext
*iothread_get_aio_context(IOThread
*iothread
)
139 return iothread
->ctx
;
142 static int query_one_iothread(Object
*object
, void *opaque
)
144 IOThreadInfoList
***prev
= opaque
;
145 IOThreadInfoList
*elem
;
149 iothread
= (IOThread
*)object_dynamic_cast(object
, TYPE_IOTHREAD
);
154 info
= g_new0(IOThreadInfo
, 1);
155 info
->id
= iothread_get_id(iothread
);
156 info
->thread_id
= iothread
->thread_id
;
158 elem
= g_new0(IOThreadInfoList
, 1);
167 IOThreadInfoList
*qmp_query_iothreads(Error
**errp
)
169 IOThreadInfoList
*head
= NULL
;
170 IOThreadInfoList
**prev
= &head
;
171 Object
*container
= object_get_objects_root();
173 object_child_foreach(container
, query_one_iothread
, &prev
);