]>
Commit | Line | Data |
---|---|---|
00dccaf1 KW |
1 | /* |
2 | * QEMU coroutines | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <[email protected]> | |
8 | * Kevin Wolf <[email protected]> | |
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 | 16 | #include "trace.h" |
b84c4586 | 17 | #include "qemu/thread.h" |
4d68e86b | 18 | #include "qemu/atomic.h" |
10817bf0 DB |
19 | #include "qemu/coroutine.h" |
20 | #include "qemu/coroutine_int.h" | |
0c330a73 | 21 | #include "block/aio.h" |
00dccaf1 | 22 | |
40239784 | 23 | enum { |
4d68e86b | 24 | POOL_BATCH_SIZE = 64, |
40239784 PB |
25 | }; |
26 | ||
27 | /** Free list to speed up creation */ | |
4d68e86b PB |
28 | static QSLIST_HEAD(, Coroutine) release_pool = QSLIST_HEAD_INITIALIZER(pool); |
29 | static unsigned int release_pool_size; | |
30 | static __thread QSLIST_HEAD(, Coroutine) alloc_pool = QSLIST_HEAD_INITIALIZER(pool); | |
51a2219b | 31 | static __thread unsigned int alloc_pool_size; |
4d68e86b PB |
32 | static __thread Notifier coroutine_pool_cleanup_notifier; |
33 | ||
34 | static void coroutine_pool_cleanup(Notifier *n, void *value) | |
35 | { | |
36 | Coroutine *co; | |
37 | Coroutine *tmp; | |
38 | ||
39 | QSLIST_FOREACH_SAFE(co, &alloc_pool, pool_next, tmp) { | |
40 | QSLIST_REMOVE_HEAD(&alloc_pool, pool_next); | |
41 | qemu_coroutine_delete(co); | |
42 | } | |
43 | } | |
40239784 | 44 | |
0b8b8753 | 45 | Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque) |
00dccaf1 | 46 | { |
70c60c08 | 47 | Coroutine *co = NULL; |
40239784 | 48 | |
70c60c08 | 49 | if (CONFIG_COROUTINE_POOL) { |
4d68e86b PB |
50 | co = QSLIST_FIRST(&alloc_pool); |
51 | if (!co) { | |
52 | if (release_pool_size > POOL_BATCH_SIZE) { | |
53 | /* Slow path; a good place to register the destructor, too. */ | |
54 | if (!coroutine_pool_cleanup_notifier.notify) { | |
55 | coroutine_pool_cleanup_notifier.notify = coroutine_pool_cleanup; | |
56 | qemu_thread_atexit_add(&coroutine_pool_cleanup_notifier); | |
57 | } | |
58 | ||
59 | /* This is not exact; there could be a little skew between | |
60 | * release_pool_size and the actual size of release_pool. But | |
61 | * it is just a heuristic, it does not need to be perfect. | |
62 | */ | |
51a2219b | 63 | alloc_pool_size = atomic_xchg(&release_pool_size, 0); |
4d68e86b PB |
64 | QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool); |
65 | co = QSLIST_FIRST(&alloc_pool); | |
66 | } | |
67 | } | |
70c60c08 | 68 | if (co) { |
4d68e86b | 69 | QSLIST_REMOVE_HEAD(&alloc_pool, pool_next); |
51a2219b | 70 | alloc_pool_size--; |
70c60c08 | 71 | } |
b84c4586 | 72 | } |
b84c4586 SH |
73 | |
74 | if (!co) { | |
40239784 PB |
75 | co = qemu_coroutine_new(); |
76 | } | |
77 | ||
00dccaf1 | 78 | co->entry = entry; |
0b8b8753 | 79 | co->entry_arg = opaque; |
7d9c8581 | 80 | QSIMPLEQ_INIT(&co->co_queue_wakeup); |
00dccaf1 KW |
81 | return co; |
82 | } | |
83 | ||
40239784 PB |
84 | static void coroutine_delete(Coroutine *co) |
85 | { | |
4d68e86b PB |
86 | co->caller = NULL; |
87 | ||
70c60c08 | 88 | if (CONFIG_COROUTINE_POOL) { |
4d68e86b PB |
89 | if (release_pool_size < POOL_BATCH_SIZE * 2) { |
90 | QSLIST_INSERT_HEAD_ATOMIC(&release_pool, co, pool_next); | |
91 | atomic_inc(&release_pool_size); | |
70c60c08 SH |
92 | return; |
93 | } | |
51a2219b PL |
94 | if (alloc_pool_size < POOL_BATCH_SIZE) { |
95 | QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next); | |
96 | alloc_pool_size++; | |
97 | return; | |
98 | } | |
40239784 PB |
99 | } |
100 | ||
101 | qemu_coroutine_delete(co); | |
102 | } | |
103 | ||
ba9e75ce | 104 | void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co) |
00dccaf1 | 105 | { |
c40a2545 SH |
106 | QSIMPLEQ_HEAD(, Coroutine) pending = QSIMPLEQ_HEAD_INITIALIZER(pending); |
107 | Coroutine *from = qemu_coroutine_self(); | |
6133b39f | 108 | |
c40a2545 | 109 | QSIMPLEQ_INSERT_TAIL(&pending, co, co_queue_next); |
00dccaf1 | 110 | |
c40a2545 SH |
111 | /* Run co and any queued coroutines */ |
112 | while (!QSIMPLEQ_EMPTY(&pending)) { | |
113 | Coroutine *to = QSIMPLEQ_FIRST(&pending); | |
114 | CoroutineAction ret; | |
6133b39f | 115 | |
c40a2545 SH |
116 | /* Cannot rely on the read barrier for to in aio_co_wake(), as there are |
117 | * callers outside of aio_co_wake() */ | |
118 | const char *scheduled = atomic_mb_read(&to->scheduled); | |
00dccaf1 | 119 | |
c40a2545 | 120 | QSIMPLEQ_REMOVE_HEAD(&pending, co_queue_next); |
0c330a73 | 121 | |
c40a2545 | 122 | trace_qemu_aio_coroutine_enter(ctx, from, to, to->entry_arg); |
0c330a73 | 123 | |
c40a2545 SH |
124 | /* if the Coroutine has already been scheduled, entering it again will |
125 | * cause us to enter it twice, potentially even after the coroutine has | |
126 | * been deleted */ | |
127 | if (scheduled) { | |
128 | fprintf(stderr, | |
129 | "%s: Co-routine was already scheduled in '%s'\n", | |
130 | __func__, scheduled); | |
131 | abort(); | |
132 | } | |
cd12bb56 | 133 | |
c40a2545 SH |
134 | if (to->caller) { |
135 | fprintf(stderr, "Co-routine re-entered recursively\n"); | |
136 | abort(); | |
137 | } | |
528f449f | 138 | |
c40a2545 SH |
139 | to->caller = from; |
140 | to->ctx = ctx; | |
141 | ||
142 | /* Store to->ctx before anything that stores to. Matches | |
143 | * barrier in aio_co_wake and qemu_co_mutex_wake. | |
144 | */ | |
145 | smp_wmb(); | |
146 | ||
147 | ret = qemu_coroutine_switch(from, to, COROUTINE_ENTER); | |
148 | ||
149 | /* Queued coroutines are run depth-first; previously pending coroutines | |
150 | * run after those queued more recently. | |
151 | */ | |
152 | QSIMPLEQ_PREPEND(&pending, &to->co_queue_wakeup); | |
153 | ||
154 | switch (ret) { | |
155 | case COROUTINE_YIELD: | |
156 | break; | |
157 | case COROUTINE_TERMINATE: | |
158 | assert(!to->locks_held); | |
159 | trace_qemu_coroutine_terminate(to); | |
160 | coroutine_delete(to); | |
161 | break; | |
162 | default: | |
163 | abort(); | |
164 | } | |
cd12bb56 | 165 | } |
00dccaf1 KW |
166 | } |
167 | ||
ba9e75ce FZ |
168 | void qemu_coroutine_enter(Coroutine *co) |
169 | { | |
170 | qemu_aio_coroutine_enter(qemu_get_current_aio_context(), co); | |
171 | } | |
172 | ||
536fca7f KW |
173 | void qemu_coroutine_enter_if_inactive(Coroutine *co) |
174 | { | |
175 | if (!qemu_coroutine_entered(co)) { | |
176 | qemu_coroutine_enter(co); | |
177 | } | |
178 | } | |
179 | ||
00dccaf1 KW |
180 | void coroutine_fn qemu_coroutine_yield(void) |
181 | { | |
182 | Coroutine *self = qemu_coroutine_self(); | |
183 | Coroutine *to = self->caller; | |
184 | ||
185 | trace_qemu_coroutine_yield(self, to); | |
186 | ||
187 | if (!to) { | |
188 | fprintf(stderr, "Co-routine is yielding to no one\n"); | |
189 | abort(); | |
190 | } | |
191 | ||
192 | self->caller = NULL; | |
315a1309 | 193 | qemu_coroutine_switch(self, to, COROUTINE_YIELD); |
00dccaf1 | 194 | } |
f643e469 SH |
195 | |
196 | bool qemu_coroutine_entered(Coroutine *co) | |
197 | { | |
198 | return co->caller; | |
199 | } | |
aa1361d5 KW |
200 | |
201 | AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co) | |
202 | { | |
203 | return co->ctx; | |
204 | } |