blob: 5816702cc5e5044b6e56c583cd7b60036ed719e6 [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"
Kevin Wolf00dccaf2011-01-17 16:08:14 +000022
Paolo Bonzini40239782013-02-19 11:59:09 +010023enum {
Paolo Bonzini4d68e862014-12-02 12:05:48 +010024 POOL_BATCH_SIZE = 64,
Paolo Bonzini40239782013-02-19 11:59:09 +010025};
26
27/** Free list to speed up creation */
Paolo Bonzini4d68e862014-12-02 12:05:48 +010028static QSLIST_HEAD(, Coroutine) release_pool = QSLIST_HEAD_INITIALIZER(pool);
29static unsigned int release_pool_size;
30static __thread QSLIST_HEAD(, Coroutine) alloc_pool = QSLIST_HEAD_INITIALIZER(pool);
Peter Lieven51a22192014-12-02 12:05:50 +010031static __thread unsigned int alloc_pool_size;
Paolo Bonzini4d68e862014-12-02 12:05:48 +010032static __thread Notifier coroutine_pool_cleanup_notifier;
33
34static 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}
Paolo Bonzini40239782013-02-19 11:59:09 +010044
Kevin Wolf00dccaf2011-01-17 16:08:14 +000045Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
46{
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020047 Coroutine *co = NULL;
Paolo Bonzini40239782013-02-19 11:59:09 +010048
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020049 if (CONFIG_COROUTINE_POOL) {
Paolo Bonzini4d68e862014-12-02 12:05:48 +010050 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 */
Peter Lieven51a22192014-12-02 12:05:50 +010063 alloc_pool_size = atomic_xchg(&release_pool_size, 0);
Paolo Bonzini4d68e862014-12-02 12:05:48 +010064 QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool);
65 co = QSLIST_FIRST(&alloc_pool);
66 }
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020067 }
Paolo Bonzini4d68e862014-12-02 12:05:48 +010068 if (co) {
69 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
Peter Lieven51a22192014-12-02 12:05:50 +010070 alloc_pool_size--;
Paolo Bonzini4d68e862014-12-02 12:05:48 +010071 }
Stefan Hajnoczib84c4582013-05-17 15:51:25 +020072 }
Stefan Hajnoczib84c4582013-05-17 15:51:25 +020073
74 if (!co) {
Paolo Bonzini40239782013-02-19 11:59:09 +010075 co = qemu_coroutine_new();
76 }
77
Kevin Wolf00dccaf2011-01-17 16:08:14 +000078 co->entry = entry;
Stefan Hajnoczi02ffb502013-05-17 15:51:26 +020079 QTAILQ_INIT(&co->co_queue_wakeup);
Kevin Wolf00dccaf2011-01-17 16:08:14 +000080 return co;
81}
82
Paolo Bonzini40239782013-02-19 11:59:09 +010083static void coroutine_delete(Coroutine *co)
84{
Paolo Bonzini4d68e862014-12-02 12:05:48 +010085 co->caller = NULL;
86
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020087 if (CONFIG_COROUTINE_POOL) {
Paolo Bonzini4d68e862014-12-02 12:05:48 +010088 if (release_pool_size < POOL_BATCH_SIZE * 2) {
89 QSLIST_INSERT_HEAD_ATOMIC(&release_pool, co, pool_next);
90 atomic_inc(&release_pool_size);
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020091 return;
92 }
Peter Lieven51a22192014-12-02 12:05:50 +010093 if (alloc_pool_size < POOL_BATCH_SIZE) {
94 QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next);
95 alloc_pool_size++;
96 return;
97 }
Paolo Bonzini40239782013-02-19 11:59:09 +010098 }
99
100 qemu_coroutine_delete(co);
101}
102
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000103void qemu_coroutine_enter(Coroutine *co, void *opaque)
104{
105 Coroutine *self = qemu_coroutine_self();
Kevin Wolfcd12bb52015-02-10 11:31:52 +0100106 CoroutineAction ret;
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000107
108 trace_qemu_coroutine_enter(self, co, opaque);
109
110 if (co->caller) {
111 fprintf(stderr, "Co-routine re-entered recursively\n");
112 abort();
113 }
114
115 co->caller = self;
116 co->entry_arg = opaque;
Kevin Wolfcd12bb52015-02-10 11:31:52 +0100117 ret = qemu_coroutine_switch(self, co, COROUTINE_ENTER);
118
119 qemu_co_queue_run_restart(co);
120
121 switch (ret) {
122 case COROUTINE_YIELD:
123 return;
124 case COROUTINE_TERMINATE:
125 trace_qemu_coroutine_terminate(co);
126 coroutine_delete(co);
127 return;
128 default:
129 abort();
130 }
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000131}
132
133void coroutine_fn qemu_coroutine_yield(void)
134{
135 Coroutine *self = qemu_coroutine_self();
136 Coroutine *to = self->caller;
137
138 trace_qemu_coroutine_yield(self, to);
139
140 if (!to) {
141 fprintf(stderr, "Co-routine is yielding to no one\n");
142 abort();
143 }
144
145 self->caller = NULL;
Kevin Wolf315a1302015-02-10 11:17:53 +0100146 qemu_coroutine_switch(self, to, COROUTINE_YIELD);
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000147}