target-ppc: Use the new deposit and extract ops
[qemu/ar7.git] / iothread.c
blob7bedde87e98fc61c1a2214145995fe8c542c60fa
1 /*
2 * Event loop thread
4 * Copyright Red Hat Inc., 2013
6 * Authors:
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 "block/block.h"
20 #include "sysemu/iothread.h"
21 #include "qmp-commands.h"
22 #include "qemu/error-report.h"
23 #include "qemu/rcu.h"
24 #include "qemu/main-loop.h"
26 typedef ObjectClass IOThreadClass;
28 #define IOTHREAD_GET_CLASS(obj) \
29 OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
30 #define IOTHREAD_CLASS(klass) \
31 OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
33 static __thread IOThread *my_iothread;
35 AioContext *qemu_get_current_aio_context(void)
37 return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
40 static void *iothread_run(void *opaque)
42 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 (!atomic_read(&iothread->stopping)) {
53 aio_poll(iothread->ctx, true);
56 rcu_unregister_thread();
57 return NULL;
60 static int iothread_stop(Object *object, void *opaque)
62 IOThread *iothread;
64 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
65 if (!iothread || !iothread->ctx) {
66 return 0;
68 iothread->stopping = true;
69 aio_notify(iothread->ctx);
70 qemu_thread_join(&iothread->thread);
71 return 0;
74 static void iothread_instance_finalize(Object *obj)
76 IOThread *iothread = IOTHREAD(obj);
78 iothread_stop(obj, NULL);
79 qemu_cond_destroy(&iothread->init_done_cond);
80 qemu_mutex_destroy(&iothread->init_done_lock);
81 if (!iothread->ctx) {
82 return;
84 aio_context_unref(iothread->ctx);
87 static void iothread_complete(UserCreatable *obj, Error **errp)
89 Error *local_error = NULL;
90 IOThread *iothread = IOTHREAD(obj);
91 char *name, *thread_name;
93 iothread->stopping = false;
94 iothread->thread_id = -1;
95 iothread->ctx = aio_context_new(&local_error);
96 if (!iothread->ctx) {
97 error_propagate(errp, local_error);
98 return;
101 aio_context_set_poll_params(iothread->ctx,
102 iothread->poll_max_ns,
103 iothread->poll_grow,
104 iothread->poll_shrink,
105 &local_error);
106 if (local_error) {
107 error_propagate(errp, local_error);
108 aio_context_unref(iothread->ctx);
109 iothread->ctx = NULL;
110 return;
113 qemu_mutex_init(&iothread->init_done_lock);
114 qemu_cond_init(&iothread->init_done_cond);
116 /* This assumes we are called from a thread with useful CPU affinity for us
117 * to inherit.
119 name = object_get_canonical_path_component(OBJECT(obj));
120 thread_name = g_strdup_printf("IO %s", name);
121 qemu_thread_create(&iothread->thread, thread_name, iothread_run,
122 iothread, QEMU_THREAD_JOINABLE);
123 g_free(thread_name);
124 g_free(name);
126 /* Wait for initialization to complete */
127 qemu_mutex_lock(&iothread->init_done_lock);
128 while (iothread->thread_id == -1) {
129 qemu_cond_wait(&iothread->init_done_cond,
130 &iothread->init_done_lock);
132 qemu_mutex_unlock(&iothread->init_done_lock);
135 typedef struct {
136 const char *name;
137 ptrdiff_t offset; /* field's byte offset in IOThread struct */
138 } PollParamInfo;
140 static PollParamInfo poll_max_ns_info = {
141 "poll-max-ns", offsetof(IOThread, poll_max_ns),
143 static PollParamInfo poll_grow_info = {
144 "poll-grow", offsetof(IOThread, poll_grow),
146 static PollParamInfo poll_shrink_info = {
147 "poll-shrink", offsetof(IOThread, poll_shrink),
150 static void iothread_get_poll_param(Object *obj, Visitor *v,
151 const char *name, void *opaque, Error **errp)
153 IOThread *iothread = IOTHREAD(obj);
154 PollParamInfo *info = opaque;
155 int64_t *field = (void *)iothread + info->offset;
157 visit_type_int64(v, name, field, errp);
160 static void iothread_set_poll_param(Object *obj, Visitor *v,
161 const char *name, void *opaque, Error **errp)
163 IOThread *iothread = IOTHREAD(obj);
164 PollParamInfo *info = opaque;
165 int64_t *field = (void *)iothread + info->offset;
166 Error *local_err = NULL;
167 int64_t value;
169 visit_type_int64(v, name, &value, &local_err);
170 if (local_err) {
171 goto out;
174 if (value < 0) {
175 error_setg(&local_err, "%s value must be in range [0, %"PRId64"]",
176 info->name, INT64_MAX);
177 goto out;
180 *field = value;
182 if (iothread->ctx) {
183 aio_context_set_poll_params(iothread->ctx,
184 iothread->poll_max_ns,
185 iothread->poll_grow,
186 iothread->poll_shrink,
187 &local_err);
190 out:
191 error_propagate(errp, local_err);
194 static void iothread_class_init(ObjectClass *klass, void *class_data)
196 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
197 ucc->complete = iothread_complete;
199 object_class_property_add(klass, "poll-max-ns", "int",
200 iothread_get_poll_param,
201 iothread_set_poll_param,
202 NULL, &poll_max_ns_info, &error_abort);
203 object_class_property_add(klass, "poll-grow", "int",
204 iothread_get_poll_param,
205 iothread_set_poll_param,
206 NULL, &poll_grow_info, &error_abort);
207 object_class_property_add(klass, "poll-shrink", "int",
208 iothread_get_poll_param,
209 iothread_set_poll_param,
210 NULL, &poll_shrink_info, &error_abort);
213 static const TypeInfo iothread_info = {
214 .name = TYPE_IOTHREAD,
215 .parent = TYPE_OBJECT,
216 .class_init = iothread_class_init,
217 .instance_size = sizeof(IOThread),
218 .instance_finalize = iothread_instance_finalize,
219 .interfaces = (InterfaceInfo[]) {
220 {TYPE_USER_CREATABLE},
225 static void iothread_register_types(void)
227 type_register_static(&iothread_info);
230 type_init(iothread_register_types)
232 char *iothread_get_id(IOThread *iothread)
234 return object_get_canonical_path_component(OBJECT(iothread));
237 AioContext *iothread_get_aio_context(IOThread *iothread)
239 return iothread->ctx;
242 static int query_one_iothread(Object *object, void *opaque)
244 IOThreadInfoList ***prev = opaque;
245 IOThreadInfoList *elem;
246 IOThreadInfo *info;
247 IOThread *iothread;
249 iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
250 if (!iothread) {
251 return 0;
254 info = g_new0(IOThreadInfo, 1);
255 info->id = iothread_get_id(iothread);
256 info->thread_id = iothread->thread_id;
258 elem = g_new0(IOThreadInfoList, 1);
259 elem->value = info;
260 elem->next = NULL;
262 **prev = elem;
263 *prev = &elem->next;
264 return 0;
267 IOThreadInfoList *qmp_query_iothreads(Error **errp)
269 IOThreadInfoList *head = NULL;
270 IOThreadInfoList **prev = &head;
271 Object *container = object_get_objects_root();
273 object_child_foreach(container, query_one_iothread, &prev);
274 return head;
277 void iothread_stop_all(void)
279 Object *container = object_get_objects_root();
280 BlockDriverState *bs;
281 BdrvNextIterator it;
283 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
284 AioContext *ctx = bdrv_get_aio_context(bs);
285 if (ctx == qemu_get_aio_context()) {
286 continue;
288 aio_context_acquire(ctx);
289 bdrv_set_aio_context(bs, qemu_get_aio_context());
290 aio_context_release(ctx);
293 object_child_foreach(container, iothread_stop, NULL);