json: Update references to RFC 7159 to RFC 8259
[qemu/ar7.git] / tests / iothread.c
blob777d9eea4620e22661ffb2697e71653377fb4f69
1 /*
2 * Event loop thread implementation for unit tests
4 * Copyright Red Hat Inc., 2013, 2016
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "block/aio.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/rcu.h"
20 #include "iothread.h"
22 struct IOThread {
23 AioContext *ctx;
25 QemuThread thread;
26 QemuMutex init_done_lock;
27 QemuCond init_done_cond; /* is thread initialization done? */
28 bool stopping;
31 static __thread IOThread *my_iothread;
33 AioContext *qemu_get_current_aio_context(void)
35 return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
38 static void *iothread_run(void *opaque)
40 IOThread *iothread = opaque;
42 rcu_register_thread();
44 my_iothread = iothread;
45 qemu_mutex_lock(&iothread->init_done_lock);
46 iothread->ctx = aio_context_new(&error_abort);
47 qemu_cond_signal(&iothread->init_done_cond);
48 qemu_mutex_unlock(&iothread->init_done_lock);
50 while (!atomic_read(&iothread->stopping)) {
51 aio_poll(iothread->ctx, true);
54 rcu_unregister_thread();
55 return NULL;
58 void iothread_join(IOThread *iothread)
60 iothread->stopping = true;
61 aio_notify(iothread->ctx);
62 qemu_thread_join(&iothread->thread);
63 qemu_cond_destroy(&iothread->init_done_cond);
64 qemu_mutex_destroy(&iothread->init_done_lock);
65 aio_context_unref(iothread->ctx);
66 g_free(iothread);
69 IOThread *iothread_new(void)
71 IOThread *iothread = g_new0(IOThread, 1);
73 qemu_mutex_init(&iothread->init_done_lock);
74 qemu_cond_init(&iothread->init_done_cond);
75 qemu_thread_create(&iothread->thread, NULL, iothread_run,
76 iothread, QEMU_THREAD_JOINABLE);
78 /* Wait for initialization to complete */
79 qemu_mutex_lock(&iothread->init_done_lock);
80 while (iothread->ctx == NULL) {
81 qemu_cond_wait(&iothread->init_done_cond,
82 &iothread->init_done_lock);
84 qemu_mutex_unlock(&iothread->init_done_lock);
85 return iothread;
88 AioContext *iothread_get_aio_context(IOThread *iothread)
90 return iothread->ctx;