blob: 486af9a62275813e235259cd578a390152131358 [file] [log] [blame]
Kevin Wolf00dccaf2011-01-17 16:08:14 +00001/*
2 * QEMU coroutines
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Kevin Wolf <kwolf@redhat.com>
9 *
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.
12 *
13 */
14
Peter Maydellaafd7582016-01-29 17:49:55 +000015#include "qemu/osdep.h"
Kevin Wolf00dccaf2011-01-17 16:08:14 +000016#include "trace.h"
17#include "qemu-common.h"
Stefan Hajnoczib84c4582013-05-17 15:51:25 +020018#include "qemu/thread.h"
Paolo Bonzini4d68e862014-12-02 12:05:48 +010019#include "qemu/atomic.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010020#include "qemu/coroutine.h"
21#include "qemu/coroutine_int.h"
Paolo Bonzini0c330a72017-02-13 14:52:19 +010022#include "block/aio.h"
Kevin Wolf00dccaf2011-01-17 16:08:14 +000023
Paolo Bonzini40239782013-02-19 11:59:09 +010024enum {
Paolo Bonzini4d68e862014-12-02 12:05:48 +010025 POOL_BATCH_SIZE = 64,
Paolo Bonzini40239782013-02-19 11:59:09 +010026};
27
28/** Free list to speed up creation */
Paolo Bonzini4d68e862014-12-02 12:05:48 +010029static QSLIST_HEAD(, Coroutine) release_pool = QSLIST_HEAD_INITIALIZER(pool);
30static unsigned int release_pool_size;
31static __thread QSLIST_HEAD(, Coroutine) alloc_pool = QSLIST_HEAD_INITIALIZER(pool);
Peter Lieven51a22192014-12-02 12:05:50 +010032static __thread unsigned int alloc_pool_size;
Paolo Bonzini4d68e862014-12-02 12:05:48 +010033static __thread Notifier coroutine_pool_cleanup_notifier;
34
35static void coroutine_pool_cleanup(Notifier *n, void *value)
36{
37 Coroutine *co;
38 Coroutine *tmp;
39
40 QSLIST_FOREACH_SAFE(co, &alloc_pool, pool_next, tmp) {
41 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
42 qemu_coroutine_delete(co);
43 }
44}
Paolo Bonzini40239782013-02-19 11:59:09 +010045
Paolo Bonzini0b8b8752016-07-04 19:10:01 +020046Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque)
Kevin Wolf00dccaf2011-01-17 16:08:14 +000047{
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020048 Coroutine *co = NULL;
Paolo Bonzini40239782013-02-19 11:59:09 +010049
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020050 if (CONFIG_COROUTINE_POOL) {
Paolo Bonzini4d68e862014-12-02 12:05:48 +010051 co = QSLIST_FIRST(&alloc_pool);
52 if (!co) {
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);
58 }
59
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.
63 */
Peter Lieven51a22192014-12-02 12:05:50 +010064 alloc_pool_size = atomic_xchg(&release_pool_size, 0);
Paolo Bonzini4d68e862014-12-02 12:05:48 +010065 QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool);
66 co = QSLIST_FIRST(&alloc_pool);
67 }
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020068 }
Paolo Bonzini4d68e862014-12-02 12:05:48 +010069 if (co) {
70 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
Peter Lieven51a22192014-12-02 12:05:50 +010071 alloc_pool_size--;
Paolo Bonzini4d68e862014-12-02 12:05:48 +010072 }
Stefan Hajnoczib84c4582013-05-17 15:51:25 +020073 }
Stefan Hajnoczib84c4582013-05-17 15:51:25 +020074
75 if (!co) {
Paolo Bonzini40239782013-02-19 11:59:09 +010076 co = qemu_coroutine_new();
77 }
78
Kevin Wolf00dccaf2011-01-17 16:08:14 +000079 co->entry = entry;
Paolo Bonzini0b8b8752016-07-04 19:10:01 +020080 co->entry_arg = opaque;
Paolo Bonzini7d9c8582016-07-04 19:09:59 +020081 QSIMPLEQ_INIT(&co->co_queue_wakeup);
Kevin Wolf00dccaf2011-01-17 16:08:14 +000082 return co;
83}
84
Paolo Bonzini40239782013-02-19 11:59:09 +010085static void coroutine_delete(Coroutine *co)
86{
Paolo Bonzini4d68e862014-12-02 12:05:48 +010087 co->caller = NULL;
88
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020089 if (CONFIG_COROUTINE_POOL) {
Paolo Bonzini4d68e862014-12-02 12:05:48 +010090 if (release_pool_size < POOL_BATCH_SIZE * 2) {
91 QSLIST_INSERT_HEAD_ATOMIC(&release_pool, co, pool_next);
92 atomic_inc(&release_pool_size);
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020093 return;
94 }
Peter Lieven51a22192014-12-02 12:05:50 +010095 if (alloc_pool_size < POOL_BATCH_SIZE) {
96 QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next);
97 alloc_pool_size++;
98 return;
99 }
Paolo Bonzini40239782013-02-19 11:59:09 +0100100 }
101
102 qemu_coroutine_delete(co);
103}
104
Fam Zhengba9e75c2017-04-10 20:06:12 +0800105void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co)
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000106{
107 Coroutine *self = qemu_coroutine_self();
Kevin Wolfcd12bb52015-02-10 11:31:52 +0100108 CoroutineAction ret;
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000109
Fam Zhengba9e75c2017-04-10 20:06:12 +0800110 trace_qemu_aio_coroutine_enter(ctx, self, co, co->entry_arg);
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000111
112 if (co->caller) {
113 fprintf(stderr, "Co-routine re-entered recursively\n");
114 abort();
115 }
116
117 co->caller = self;
Fam Zhengba9e75c2017-04-10 20:06:12 +0800118 co->ctx = ctx;
Paolo Bonzini0c330a72017-02-13 14:52:19 +0100119
120 /* Store co->ctx before anything that stores co. Matches
Paolo Bonzini480cff62017-02-13 19:12:40 +0100121 * barrier in aio_co_wake and qemu_co_mutex_wake.
Paolo Bonzini0c330a72017-02-13 14:52:19 +0100122 */
123 smp_wmb();
124
Kevin Wolfcd12bb52015-02-10 11:31:52 +0100125 ret = qemu_coroutine_switch(self, co, COROUTINE_ENTER);
126
127 qemu_co_queue_run_restart(co);
128
129 switch (ret) {
130 case COROUTINE_YIELD:
131 return;
132 case COROUTINE_TERMINATE:
Kevin Wolf1b7f01d2016-08-11 17:51:59 +0200133 assert(!co->locks_held);
Kevin Wolfcd12bb52015-02-10 11:31:52 +0100134 trace_qemu_coroutine_terminate(co);
135 coroutine_delete(co);
136 return;
137 default:
138 abort();
139 }
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000140}
141
Fam Zhengba9e75c2017-04-10 20:06:12 +0800142void qemu_coroutine_enter(Coroutine *co)
143{
144 qemu_aio_coroutine_enter(qemu_get_current_aio_context(), co);
145}
146
Kevin Wolf536fca72016-11-07 16:34:35 +0100147void qemu_coroutine_enter_if_inactive(Coroutine *co)
148{
149 if (!qemu_coroutine_entered(co)) {
150 qemu_coroutine_enter(co);
151 }
152}
153
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000154void coroutine_fn qemu_coroutine_yield(void)
155{
156 Coroutine *self = qemu_coroutine_self();
157 Coroutine *to = self->caller;
158
159 trace_qemu_coroutine_yield(self, to);
160
161 if (!to) {
162 fprintf(stderr, "Co-routine is yielding to no one\n");
163 abort();
164 }
165
166 self->caller = NULL;
Kevin Wolf315a1302015-02-10 11:17:53 +0100167 qemu_coroutine_switch(self, to, COROUTINE_YIELD);
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000168}
Stefan Hajnoczif643e462016-09-27 16:18:34 +0100169
170bool qemu_coroutine_entered(Coroutine *co)
171{
172 return co->caller;
173}