iothread.c 4.3 KB

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