m68k: is_mem is useless
[qemu.git] / qemu-coroutine.c
blobc17a92b107e8e7064b8616a2f46553ba9bac614d
1 /*
2 * QEMU coroutines
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Kevin Wolf <kwolf@redhat.com>
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.
15 #include "trace.h"
16 #include "qemu-common.h"
17 #include "qemu/thread.h"
18 #include "qemu/atomic.h"
19 #include "block/coroutine.h"
20 #include "block/coroutine_int.h"
22 enum {
23 POOL_BATCH_SIZE = 64,
26 /** Free list to speed up creation */
27 static QSLIST_HEAD(, Coroutine) release_pool = QSLIST_HEAD_INITIALIZER(pool);
28 static unsigned int release_pool_size;
29 static __thread QSLIST_HEAD(, Coroutine) alloc_pool = QSLIST_HEAD_INITIALIZER(pool);
30 static __thread unsigned int alloc_pool_size;
31 static __thread Notifier coroutine_pool_cleanup_notifier;
33 static void coroutine_pool_cleanup(Notifier *n, void *value)
35 Coroutine *co;
36 Coroutine *tmp;
38 QSLIST_FOREACH_SAFE(co, &alloc_pool, pool_next, tmp) {
39 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
40 qemu_coroutine_delete(co);
44 Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
46 Coroutine *co = NULL;
48 if (CONFIG_COROUTINE_POOL) {
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);
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.
62 alloc_pool_size = atomic_xchg(&release_pool_size, 0);
63 QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool);
64 co = QSLIST_FIRST(&alloc_pool);
67 if (co) {
68 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
69 alloc_pool_size--;
73 if (!co) {
74 co = qemu_coroutine_new();
77 co->entry = entry;
78 QTAILQ_INIT(&co->co_queue_wakeup);
79 return co;
82 static void coroutine_delete(Coroutine *co)
84 co->caller = NULL;
86 if (CONFIG_COROUTINE_POOL) {
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);
90 return;
92 if (alloc_pool_size < POOL_BATCH_SIZE) {
93 QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next);
94 alloc_pool_size++;
95 return;
99 qemu_coroutine_delete(co);
102 void qemu_coroutine_enter(Coroutine *co, void *opaque)
104 Coroutine *self = qemu_coroutine_self();
105 CoroutineAction ret;
107 trace_qemu_coroutine_enter(self, co, opaque);
109 if (co->caller) {
110 fprintf(stderr, "Co-routine re-entered recursively\n");
111 abort();
114 co->caller = self;
115 co->entry_arg = opaque;
116 ret = qemu_coroutine_switch(self, co, COROUTINE_ENTER);
118 qemu_co_queue_run_restart(co);
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();
132 void coroutine_fn qemu_coroutine_yield(void)
134 Coroutine *self = qemu_coroutine_self();
135 Coroutine *to = self->caller;
137 trace_qemu_coroutine_yield(self, to);
139 if (!to) {
140 fprintf(stderr, "Co-routine is yielding to no one\n");
141 abort();
144 self->caller = NULL;
145 qemu_coroutine_switch(self, to, COROUTINE_YIELD);