]> git.proxmox.com Git - mirror_qemu.git/blame - iothread.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-vga-20150615-1' into staging
[mirror_qemu.git] / iothread.c
CommitLineData
be8d8537
SH
1/*
2 * Event loop thread
3 *
4 * Copyright Red Hat Inc., 2013
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
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.
11 *
12 */
13
14#include "qom/object.h"
15#include "qom/object_interfaces.h"
16#include "qemu/module.h"
be8d8537
SH
17#include "block/aio.h"
18#include "sysemu/iothread.h"
dc3dd0d2 19#include "qmp-commands.h"
2f78e491 20#include "qemu/error-report.h"
be8d8537
SH
21
22#define IOTHREADS_PATH "/objects"
23
24typedef ObjectClass IOThreadClass;
be8d8537
SH
25
26#define IOTHREAD_GET_CLASS(obj) \
27 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
28#define IOTHREAD_CLASS(klass) \
29 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
30
31static void *iothread_run(void *opaque)
32{
33 IOThread *iothread = opaque;
da5e1de9 34 bool blocking;
be8d8537 35
88eb7c29
SH
36 qemu_mutex_lock(&iothread->init_done_lock);
37 iothread->thread_id = qemu_get_thread_id();
38 qemu_cond_signal(&iothread->init_done_cond);
39 qemu_mutex_unlock(&iothread->init_done_lock);
40
da5e1de9
SH
41 while (!iothread->stopping) {
42 aio_context_acquire(iothread->ctx);
43 blocking = true;
44 while (!iothread->stopping && aio_poll(iothread->ctx, blocking)) {
45 /* Progress was made, keep going */
46 blocking = false;
47 }
48 aio_context_release(iothread->ctx);
be8d8537
SH
49 }
50 return NULL;
51}
52
53static void iothread_instance_finalize(Object *obj)
54{
55 IOThread *iothread = IOTHREAD(obj);
56
2f78e491
CN
57 if (!iothread->ctx) {
58 return;
59 }
be8d8537
SH
60 iothread->stopping = true;
61 aio_notify(iothread->ctx);
62 qemu_thread_join(&iothread->thread);
88eb7c29
SH
63 qemu_cond_destroy(&iothread->init_done_cond);
64 qemu_mutex_destroy(&iothread->init_done_lock);
be8d8537
SH
65 aio_context_unref(iothread->ctx);
66}
67
68static void iothread_complete(UserCreatable *obj, Error **errp)
69{
2f78e491 70 Error *local_error = NULL;
be8d8537
SH
71 IOThread *iothread = IOTHREAD(obj);
72
73 iothread->stopping = false;
88eb7c29 74 iothread->thread_id = -1;
2f78e491
CN
75 iothread->ctx = aio_context_new(&local_error);
76 if (!iothread->ctx) {
77 error_propagate(errp, local_error);
78 return;
79 }
88eb7c29
SH
80
81 qemu_mutex_init(&iothread->init_done_lock);
82 qemu_cond_init(&iothread->init_done_cond);
be8d8537
SH
83
84 /* This assumes we are called from a thread with useful CPU affinity for us
85 * to inherit.
86 */
87 qemu_thread_create(&iothread->thread, "iothread", iothread_run,
88 iothread, QEMU_THREAD_JOINABLE);
88eb7c29
SH
89
90 /* Wait for initialization to complete */
91 qemu_mutex_lock(&iothread->init_done_lock);
92 while (iothread->thread_id == -1) {
93 qemu_cond_wait(&iothread->init_done_cond,
94 &iothread->init_done_lock);
95 }
96 qemu_mutex_unlock(&iothread->init_done_lock);
be8d8537
SH
97}
98
99static void iothread_class_init(ObjectClass *klass, void *class_data)
100{
101 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
102 ucc->complete = iothread_complete;
103}
104
105static const TypeInfo iothread_info = {
106 .name = TYPE_IOTHREAD,
107 .parent = TYPE_OBJECT,
108 .class_init = iothread_class_init,
109 .instance_size = sizeof(IOThread),
110 .instance_finalize = iothread_instance_finalize,
111 .interfaces = (InterfaceInfo[]) {
112 {TYPE_USER_CREATABLE},
113 {}
114 },
115};
116
117static void iothread_register_types(void)
118{
119 type_register_static(&iothread_info);
120}
121
122type_init(iothread_register_types)
123
be8d8537
SH
124char *iothread_get_id(IOThread *iothread)
125{
126 return object_get_canonical_path_component(OBJECT(iothread));
127}
128
129AioContext *iothread_get_aio_context(IOThread *iothread)
130{
131 return iothread->ctx;
132}
dc3dd0d2
SH
133
134static int query_one_iothread(Object *object, void *opaque)
135{
136 IOThreadInfoList ***prev = opaque;
137 IOThreadInfoList *elem;
138 IOThreadInfo *info;
139 IOThread *iothread;
140
141 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
142 if (!iothread) {
143 return 0;
144 }
145
146 info = g_new0(IOThreadInfo, 1);
147 info->id = iothread_get_id(iothread);
148 info->thread_id = iothread->thread_id;
149
150 elem = g_new0(IOThreadInfoList, 1);
151 elem->value = info;
152 elem->next = NULL;
153
154 **prev = elem;
155 *prev = &elem->next;
156 return 0;
157}
158
159IOThreadInfoList *qmp_query_iothreads(Error **errp)
160{
161 IOThreadInfoList *head = NULL;
162 IOThreadInfoList **prev = &head;
163 Object *container = container_get(object_get_root(), IOTHREADS_PATH);
164
165 object_child_foreach(container, query_one_iothread, &prev);
166 return head;
167}