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