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 "qemu/thread.h"
18 #include "block/aio.h"
19 #include "sysemu/iothread.h"
21 #define IOTHREADS_PATH "/objects"
23 typedef ObjectClass IOThreadClass
;
32 #define IOTHREAD_GET_CLASS(obj) \
33 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
34 #define IOTHREAD_CLASS(klass) \
35 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
37 static void *iothread_run(void *opaque
)
39 IOThread
*iothread
= opaque
;
41 while (!iothread
->stopping
) {
42 aio_context_acquire(iothread
->ctx
);
43 while (!iothread
->stopping
&& aio_poll(iothread
->ctx
, true)) {
44 /* Progress was made, keep going */
46 aio_context_release(iothread
->ctx
);
51 static void iothread_instance_finalize(Object
*obj
)
53 IOThread
*iothread
= IOTHREAD(obj
);
55 iothread
->stopping
= true;
56 aio_notify(iothread
->ctx
);
57 qemu_thread_join(&iothread
->thread
);
58 aio_context_unref(iothread
->ctx
);
61 static void iothread_complete(UserCreatable
*obj
, Error
**errp
)
63 IOThread
*iothread
= IOTHREAD(obj
);
65 iothread
->stopping
= false;
66 iothread
->ctx
= aio_context_new();
68 /* This assumes we are called from a thread with useful CPU affinity for us
71 qemu_thread_create(&iothread
->thread
, "iothread", iothread_run
,
72 iothread
, QEMU_THREAD_JOINABLE
);
75 static void iothread_class_init(ObjectClass
*klass
, void *class_data
)
77 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(klass
);
78 ucc
->complete
= iothread_complete
;
81 static const TypeInfo iothread_info
= {
82 .name
= TYPE_IOTHREAD
,
83 .parent
= TYPE_OBJECT
,
84 .class_init
= iothread_class_init
,
85 .instance_size
= sizeof(IOThread
),
86 .instance_finalize
= iothread_instance_finalize
,
87 .interfaces
= (InterfaceInfo
[]) {
88 {TYPE_USER_CREATABLE
},
93 static void iothread_register_types(void)
95 type_register_static(&iothread_info
);
98 type_init(iothread_register_types
)
100 IOThread
*iothread_find(const char *id
)
102 Object
*container
= container_get(object_get_root(), IOTHREADS_PATH
);
105 child
= object_property_get_link(container
, id
, NULL
);
109 return (IOThread
*)object_dynamic_cast(child
, TYPE_IOTHREAD
);
112 char *iothread_get_id(IOThread
*iothread
)
114 return object_get_canonical_path_component(OBJECT(iothread
));
117 AioContext
*iothread_get_aio_context(IOThread
*iothread
)
119 return iothread
->ctx
;