blob: c17a92b107e8e7064b8616a2f46553ba9bac614d [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
15#include "trace.h"
16#include "qemu-common.h"
Stefan Hajnoczib84c4582013-05-17 15:51:25 +020017#include "qemu/thread.h"
Paolo Bonzini4d68e862014-12-02 12:05:48 +010018#include "qemu/atomic.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/coroutine.h"
20#include "block/coroutine_int.h"
Kevin Wolf00dccaf2011-01-17 16:08:14 +000021
Paolo Bonzini40239782013-02-19 11:59:09 +010022enum {
Paolo Bonzini4d68e862014-12-02 12:05:48 +010023 POOL_BATCH_SIZE = 64,
Paolo Bonzini40239782013-02-19 11:59:09 +010024};
25
26/** Free list to speed up creation */
Paolo Bonzini4d68e862014-12-02 12:05:48 +010027static 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);
Peter Lieven51a22192014-12-02 12:05:50 +010030static __thread unsigned int alloc_pool_size;
Paolo Bonzini4d68e862014-12-02 12:05:48 +010031static __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}
Paolo Bonzini40239782013-02-19 11:59:09 +010043
Kevin Wolf00dccaf2011-01-17 16:08:14 +000044Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
45{
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020046 Coroutine *co = NULL;
Paolo Bonzini40239782013-02-19 11:59:09 +010047
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020048 if (CONFIG_COROUTINE_POOL) {
Paolo Bonzini4d68e862014-12-02 12:05:48 +010049 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 */
Peter Lieven51a22192014-12-02 12:05:50 +010062 alloc_pool_size = atomic_xchg(&release_pool_size, 0);
Paolo Bonzini4d68e862014-12-02 12:05:48 +010063 QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool);
64 co = QSLIST_FIRST(&alloc_pool);
65 }
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020066 }
Paolo Bonzini4d68e862014-12-02 12:05:48 +010067 if (co) {
68 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
Peter Lieven51a22192014-12-02 12:05:50 +010069 alloc_pool_size--;
Paolo Bonzini4d68e862014-12-02 12:05:48 +010070 }
Stefan Hajnoczib84c4582013-05-17 15:51:25 +020071 }
Stefan Hajnoczib84c4582013-05-17 15:51:25 +020072
73 if (!co) {
Paolo Bonzini40239782013-02-19 11:59:09 +010074 co = qemu_coroutine_new();
75 }
76
Kevin Wolf00dccaf2011-01-17 16:08:14 +000077 co->entry = entry;
Stefan Hajnoczi02ffb502013-05-17 15:51:26 +020078 QTAILQ_INIT(&co->co_queue_wakeup);
Kevin Wolf00dccaf2011-01-17 16:08:14 +000079 return co;
80}
81
Paolo Bonzini40239782013-02-19 11:59:09 +010082static void coroutine_delete(Coroutine *co)
83{
Paolo Bonzini4d68e862014-12-02 12:05:48 +010084 co->caller = NULL;
85
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020086 if (CONFIG_COROUTINE_POOL) {
Paolo Bonzini4d68e862014-12-02 12:05:48 +010087 if (release_pool_size < POOL_BATCH_SIZE * 2) {
88 QSLIST_INSERT_HEAD_ATOMIC(&release_pool, co, pool_next);
89 atomic_inc(&release_pool_size);
Stefan Hajnoczi70c60c02013-09-11 16:42:35 +020090 return;
91 }
Peter Lieven51a22192014-12-02 12:05:50 +010092 if (alloc_pool_size < POOL_BATCH_SIZE) {
93 QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next);
94 alloc_pool_size++;
95 return;
96 }
Paolo Bonzini40239782013-02-19 11:59:09 +010097 }
98
99 qemu_coroutine_delete(co);
100}
101
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000102void qemu_coroutine_enter(Coroutine *co, void *opaque)
103{
104 Coroutine *self = qemu_coroutine_self();
Kevin Wolfcd12bb52015-02-10 11:31:52 +0100105 CoroutineAction ret;
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000106
107 trace_qemu_coroutine_enter(self, co, opaque);
108
109 if (co->caller) {
110 fprintf(stderr, "Co-routine re-entered recursively\n");
111 abort();
112 }
113
114 co->caller = self;
115 co->entry_arg = opaque;
Kevin Wolfcd12bb52015-02-10 11:31:52 +0100116 ret = qemu_coroutine_switch(self, co, COROUTINE_ENTER);
117
118 qemu_co_queue_run_restart(co);
119
120 switch (ret) {
121 case COROUTINE_YIELD:
122 return;
123 case COROUTINE_TERMINATE:
124 trace_qemu_coroutine_terminate(co);
125 coroutine_delete(co);
126 return;
127 default:
128 abort();
129 }
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000130}
131
132void coroutine_fn qemu_coroutine_yield(void)
133{
134 Coroutine *self = qemu_coroutine_self();
135 Coroutine *to = self->caller;
136
137 trace_qemu_coroutine_yield(self, to);
138
139 if (!to) {
140 fprintf(stderr, "Co-routine is yielding to no one\n");
141 abort();
142 }
143
144 self->caller = NULL;
Kevin Wolf315a1302015-02-10 11:17:53 +0100145 qemu_coroutine_switch(self, to, COROUTINE_YIELD);
Kevin Wolf00dccaf2011-01-17 16:08:14 +0000146}