]> git.proxmox.com Git - mirror_qemu.git/blame - util/qemu-coroutine.c
rbd: Don't accept -drive driver=rbd, keyvalue-pairs=...
[mirror_qemu.git] / util / qemu-coroutine.c
CommitLineData
00dccaf1
KW
1/*
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
aafd7584 15#include "qemu/osdep.h"
00dccaf1
KW
16#include "trace.h"
17#include "qemu-common.h"
b84c4586 18#include "qemu/thread.h"
4d68e86b 19#include "qemu/atomic.h"
10817bf0
DB
20#include "qemu/coroutine.h"
21#include "qemu/coroutine_int.h"
0c330a73 22#include "block/aio.h"
00dccaf1 23
40239784 24enum {
4d68e86b 25 POOL_BATCH_SIZE = 64,
40239784
PB
26};
27
28/** Free list to speed up creation */
4d68e86b
PB
29static 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);
51a2219b 32static __thread unsigned int alloc_pool_size;
4d68e86b
PB
33static __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}
40239784 45
0b8b8753 46Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque)
00dccaf1 47{
70c60c08 48 Coroutine *co = NULL;
40239784 49
70c60c08 50 if (CONFIG_COROUTINE_POOL) {
4d68e86b
PB
51 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 */
51a2219b 64 alloc_pool_size = atomic_xchg(&release_pool_size, 0);
4d68e86b
PB
65 QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool);
66 co = QSLIST_FIRST(&alloc_pool);
67 }
68 }
70c60c08 69 if (co) {
4d68e86b 70 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
51a2219b 71 alloc_pool_size--;
70c60c08 72 }
b84c4586 73 }
b84c4586
SH
74
75 if (!co) {
40239784
PB
76 co = qemu_coroutine_new();
77 }
78
00dccaf1 79 co->entry = entry;
0b8b8753 80 co->entry_arg = opaque;
7d9c8581 81 QSIMPLEQ_INIT(&co->co_queue_wakeup);
00dccaf1
KW
82 return co;
83}
84
40239784
PB
85static void coroutine_delete(Coroutine *co)
86{
4d68e86b
PB
87 co->caller = NULL;
88
70c60c08 89 if (CONFIG_COROUTINE_POOL) {
4d68e86b
PB
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);
70c60c08
SH
93 return;
94 }
51a2219b
PL
95 if (alloc_pool_size < POOL_BATCH_SIZE) {
96 QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next);
97 alloc_pool_size++;
98 return;
99 }
40239784
PB
100 }
101
102 qemu_coroutine_delete(co);
103}
104
0b8b8753 105void qemu_coroutine_enter(Coroutine *co)
00dccaf1
KW
106{
107 Coroutine *self = qemu_coroutine_self();
cd12bb56 108 CoroutineAction ret;
00dccaf1 109
0b8b8753 110 trace_qemu_coroutine_enter(self, co, co->entry_arg);
00dccaf1
KW
111
112 if (co->caller) {
113 fprintf(stderr, "Co-routine re-entered recursively\n");
114 abort();
115 }
116
117 co->caller = self;
0c330a73
PB
118 co->ctx = qemu_get_current_aio_context();
119
120 /* Store co->ctx before anything that stores co. Matches
480cff63 121 * barrier in aio_co_wake and qemu_co_mutex_wake.
0c330a73
PB
122 */
123 smp_wmb();
124
cd12bb56
KW
125 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:
1b7f01d9 133 assert(!co->locks_held);
cd12bb56
KW
134 trace_qemu_coroutine_terminate(co);
135 coroutine_delete(co);
136 return;
137 default:
138 abort();
139 }
00dccaf1
KW
140}
141
536fca7f
KW
142void qemu_coroutine_enter_if_inactive(Coroutine *co)
143{
144 if (!qemu_coroutine_entered(co)) {
145 qemu_coroutine_enter(co);
146 }
147}
148
00dccaf1
KW
149void coroutine_fn qemu_coroutine_yield(void)
150{
151 Coroutine *self = qemu_coroutine_self();
152 Coroutine *to = self->caller;
153
154 trace_qemu_coroutine_yield(self, to);
155
156 if (!to) {
157 fprintf(stderr, "Co-routine is yielding to no one\n");
158 abort();
159 }
160
161 self->caller = NULL;
315a1309 162 qemu_coroutine_switch(self, to, COROUTINE_YIELD);
00dccaf1 163}
f643e469
SH
164
165bool qemu_coroutine_entered(Coroutine *co)
166{
167 return co->caller;
168}