4 * Copyright IBM, Corp. 2011
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Kevin Wolf <kwolf@redhat.com>
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
17 #include "qemu-common.h"
18 #include "qemu/thread.h"
19 #include "qemu/atomic.h"
20 #include "qemu/coroutine.h"
21 #include "qemu/coroutine_int.h"
22 #include "block/aio.h"
28 /** Free list to speed up creation */
29 static QSLIST_HEAD(, Coroutine
) release_pool
= QSLIST_HEAD_INITIALIZER(pool
);
30 static unsigned int release_pool_size
;
31 static __thread
QSLIST_HEAD(, Coroutine
) alloc_pool
= QSLIST_HEAD_INITIALIZER(pool
);
32 static __thread
unsigned int alloc_pool_size
;
33 static __thread Notifier coroutine_pool_cleanup_notifier
;
35 static void coroutine_pool_cleanup(Notifier
*n
, void *value
)
40 QSLIST_FOREACH_SAFE(co
, &alloc_pool
, pool_next
, tmp
) {
41 QSLIST_REMOVE_HEAD(&alloc_pool
, pool_next
);
42 qemu_coroutine_delete(co
);
46 Coroutine
*qemu_coroutine_create(CoroutineEntry
*entry
, void *opaque
)
50 if (CONFIG_COROUTINE_POOL
) {
51 co
= QSLIST_FIRST(&alloc_pool
);
53 if (release_pool_size
> POOL_BATCH_SIZE
) {
54 /* Slow path; a good place to register the destructor, too. */
55 if (!coroutine_pool_cleanup_notifier
.notify
) {
56 coroutine_pool_cleanup_notifier
.notify
= coroutine_pool_cleanup
;
57 qemu_thread_atexit_add(&coroutine_pool_cleanup_notifier
);
60 /* This is not exact; there could be a little skew between
61 * release_pool_size and the actual size of release_pool. But
62 * it is just a heuristic, it does not need to be perfect.
64 alloc_pool_size
= atomic_xchg(&release_pool_size
, 0);
65 QSLIST_MOVE_ATOMIC(&alloc_pool
, &release_pool
);
66 co
= QSLIST_FIRST(&alloc_pool
);
70 QSLIST_REMOVE_HEAD(&alloc_pool
, pool_next
);
76 co
= qemu_coroutine_new();
80 co
->entry_arg
= opaque
;
81 QSIMPLEQ_INIT(&co
->co_queue_wakeup
);
85 static void coroutine_delete(Coroutine
*co
)
89 if (CONFIG_COROUTINE_POOL
) {
90 if (release_pool_size
< POOL_BATCH_SIZE
* 2) {
91 QSLIST_INSERT_HEAD_ATOMIC(&release_pool
, co
, pool_next
);
92 atomic_inc(&release_pool_size
);
95 if (alloc_pool_size
< POOL_BATCH_SIZE
) {
96 QSLIST_INSERT_HEAD(&alloc_pool
, co
, pool_next
);
102 qemu_coroutine_delete(co
);
105 void qemu_aio_coroutine_enter(AioContext
*ctx
, Coroutine
*co
)
107 QSIMPLEQ_HEAD(, Coroutine
) pending
= QSIMPLEQ_HEAD_INITIALIZER(pending
);
108 Coroutine
*from
= qemu_coroutine_self();
110 QSIMPLEQ_INSERT_TAIL(&pending
, co
, co_queue_next
);
112 /* Run co and any queued coroutines */
113 while (!QSIMPLEQ_EMPTY(&pending
)) {
114 Coroutine
*to
= QSIMPLEQ_FIRST(&pending
);
117 /* Cannot rely on the read barrier for to in aio_co_wake(), as there are
118 * callers outside of aio_co_wake() */
119 const char *scheduled
= atomic_mb_read(&to
->scheduled
);
121 QSIMPLEQ_REMOVE_HEAD(&pending
, co_queue_next
);
123 trace_qemu_aio_coroutine_enter(ctx
, from
, to
, to
->entry_arg
);
125 /* if the Coroutine has already been scheduled, entering it again will
126 * cause us to enter it twice, potentially even after the coroutine has
130 "%s: Co-routine was already scheduled in '%s'\n",
131 __func__
, scheduled
);
136 fprintf(stderr
, "Co-routine re-entered recursively\n");
143 /* Store to->ctx before anything that stores to. Matches
144 * barrier in aio_co_wake and qemu_co_mutex_wake.
148 ret
= qemu_coroutine_switch(from
, to
, COROUTINE_ENTER
);
150 /* Queued coroutines are run depth-first; previously pending coroutines
151 * run after those queued more recently.
153 QSIMPLEQ_PREPEND(&pending
, &to
->co_queue_wakeup
);
156 case COROUTINE_YIELD
:
158 case COROUTINE_TERMINATE
:
159 assert(!to
->locks_held
);
160 trace_qemu_coroutine_terminate(to
);
161 coroutine_delete(to
);
169 void qemu_coroutine_enter(Coroutine
*co
)
171 qemu_aio_coroutine_enter(qemu_get_current_aio_context(), co
);
174 void qemu_coroutine_enter_if_inactive(Coroutine
*co
)
176 if (!qemu_coroutine_entered(co
)) {
177 qemu_coroutine_enter(co
);
181 void coroutine_fn
qemu_coroutine_yield(void)
183 Coroutine
*self
= qemu_coroutine_self();
184 Coroutine
*to
= self
->caller
;
186 trace_qemu_coroutine_yield(self
, to
);
189 fprintf(stderr
, "Co-routine is yielding to no one\n");
194 qemu_coroutine_switch(self
, to
, COROUTINE_YIELD
);
197 bool qemu_coroutine_entered(Coroutine
*co
)