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