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