]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-coroutine.c
Merge remote-tracking branch 'remotes/gonglei/tags/bootdevice-next-20150303' into...
[mirror_qemu.git] / 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
15#include "trace.h"
16#include "qemu-common.h"
b84c4586 17#include "qemu/thread.h"
4d68e86b 18#include "qemu/atomic.h"
737e150e
PB
19#include "block/coroutine.h"
20#include "block/coroutine_int.h"
00dccaf1 21
40239784 22enum {
4d68e86b 23 POOL_BATCH_SIZE = 64,
40239784
PB
24};
25
26/** Free list to speed up creation */
4d68e86b
PB
27static QSLIST_HEAD(, Coroutine) release_pool = QSLIST_HEAD_INITIALIZER(pool);
28static unsigned int release_pool_size;
29static __thread QSLIST_HEAD(, Coroutine) alloc_pool = QSLIST_HEAD_INITIALIZER(pool);
51a2219b 30static __thread unsigned int alloc_pool_size;
4d68e86b
PB
31static __thread Notifier coroutine_pool_cleanup_notifier;
32
33static void coroutine_pool_cleanup(Notifier *n, void *value)
34{
35 Coroutine *co;
36 Coroutine *tmp;
37
38 QSLIST_FOREACH_SAFE(co, &alloc_pool, pool_next, tmp) {
39 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
40 qemu_coroutine_delete(co);
41 }
42}
40239784 43
00dccaf1
KW
44Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
45{
70c60c08 46 Coroutine *co = NULL;
40239784 47
70c60c08 48 if (CONFIG_COROUTINE_POOL) {
4d68e86b
PB
49 co = QSLIST_FIRST(&alloc_pool);
50 if (!co) {
51 if (release_pool_size > POOL_BATCH_SIZE) {
52 /* Slow path; a good place to register the destructor, too. */
53 if (!coroutine_pool_cleanup_notifier.notify) {
54 coroutine_pool_cleanup_notifier.notify = coroutine_pool_cleanup;
55 qemu_thread_atexit_add(&coroutine_pool_cleanup_notifier);
56 }
57
58 /* This is not exact; there could be a little skew between
59 * release_pool_size and the actual size of release_pool. But
60 * it is just a heuristic, it does not need to be perfect.
61 */
51a2219b 62 alloc_pool_size = atomic_xchg(&release_pool_size, 0);
4d68e86b
PB
63 QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool);
64 co = QSLIST_FIRST(&alloc_pool);
65 }
66 }
70c60c08 67 if (co) {
4d68e86b 68 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
51a2219b 69 alloc_pool_size--;
70c60c08 70 }
b84c4586 71 }
b84c4586
SH
72
73 if (!co) {
40239784
PB
74 co = qemu_coroutine_new();
75 }
76
00dccaf1 77 co->entry = entry;
02ffb504 78 QTAILQ_INIT(&co->co_queue_wakeup);
00dccaf1
KW
79 return co;
80}
81
40239784
PB
82static void coroutine_delete(Coroutine *co)
83{
4d68e86b
PB
84 co->caller = NULL;
85
70c60c08 86 if (CONFIG_COROUTINE_POOL) {
4d68e86b
PB
87 if (release_pool_size < POOL_BATCH_SIZE * 2) {
88 QSLIST_INSERT_HEAD_ATOMIC(&release_pool, co, pool_next);
89 atomic_inc(&release_pool_size);
70c60c08
SH
90 return;
91 }
51a2219b
PL
92 if (alloc_pool_size < POOL_BATCH_SIZE) {
93 QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next);
94 alloc_pool_size++;
95 return;
96 }
40239784
PB
97 }
98
99 qemu_coroutine_delete(co);
100}
101
00dccaf1
KW
102static void coroutine_swap(Coroutine *from, Coroutine *to)
103{
104 CoroutineAction ret;
105
106 ret = qemu_coroutine_switch(from, to, COROUTINE_YIELD);
107
02ffb504
SH
108 qemu_co_queue_run_restart(to);
109
00dccaf1
KW
110 switch (ret) {
111 case COROUTINE_YIELD:
112 return;
113 case COROUTINE_TERMINATE:
114 trace_qemu_coroutine_terminate(to);
40239784 115 coroutine_delete(to);
00dccaf1
KW
116 return;
117 default:
118 abort();
119 }
120}
121
122void qemu_coroutine_enter(Coroutine *co, void *opaque)
123{
124 Coroutine *self = qemu_coroutine_self();
125
126 trace_qemu_coroutine_enter(self, co, opaque);
127
128 if (co->caller) {
129 fprintf(stderr, "Co-routine re-entered recursively\n");
130 abort();
131 }
132
133 co->caller = self;
134 co->entry_arg = opaque;
135 coroutine_swap(self, co);
136}
137
138void coroutine_fn qemu_coroutine_yield(void)
139{
140 Coroutine *self = qemu_coroutine_self();
141 Coroutine *to = self->caller;
142
143 trace_qemu_coroutine_yield(self, to);
144
145 if (!to) {
146 fprintf(stderr, "Co-routine is yielding to no one\n");
147 abort();
148 }
149
150 self->caller = NULL;
151 coroutine_swap(self, to);
152}