1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "qemu/osdep.h"
16#include "trace.h"
17#include "qemu-common.h"
18#include "qemu/thread.h"
19#include "qemu/atomic.h"
20#include "qemu/coroutine.h"
21#include "qemu/coroutine_int.h"
22
23enum {
24 POOL_BATCH_SIZE = 64,
25};
26
27
28static 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);
31static __thread unsigned int alloc_pool_size;
32static __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}
44
45Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque)
46{
47 Coroutine *co = NULL;
48
49 if (CONFIG_COROUTINE_POOL) {
50 co = QSLIST_FIRST(&alloc_pool);
51 if (!co) {
52 if (release_pool_size > POOL_BATCH_SIZE) {
53
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
60
61
62
63 alloc_pool_size = atomic_xchg(&release_pool_size, 0);
64 QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool);
65 co = QSLIST_FIRST(&alloc_pool);
66 }
67 }
68 if (co) {
69 QSLIST_REMOVE_HEAD(&alloc_pool, pool_next);
70 alloc_pool_size--;
71 }
72 }
73
74 if (!co) {
75 co = qemu_coroutine_new();
76 }
77
78 co->entry = entry;
79 co->entry_arg = opaque;
80 QSIMPLEQ_INIT(&co->co_queue_wakeup);
81 return co;
82}
83
84static void coroutine_delete(Coroutine *co)
85{
86 co->caller = NULL;
87
88 if (CONFIG_COROUTINE_POOL) {
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);
92 return;
93 }
94 if (alloc_pool_size < POOL_BATCH_SIZE) {
95 QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next);
96 alloc_pool_size++;
97 return;
98 }
99 }
100
101 qemu_coroutine_delete(co);
102}
103
104void qemu_coroutine_enter(Coroutine *co)
105{
106 Coroutine *self = qemu_coroutine_self();
107 CoroutineAction ret;
108
109 trace_qemu_coroutine_enter(self, co, co->entry_arg);
110
111 if (co->caller) {
112 fprintf(stderr, "Co-routine re-entered recursively\n");
113 abort();
114 }
115
116 co->caller = self;
117 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 assert(!co->locks_held);
126 trace_qemu_coroutine_terminate(co);
127 coroutine_delete(co);
128 return;
129 default:
130 abort();
131 }
132}
133
134void coroutine_fn qemu_coroutine_yield(void)
135{
136 Coroutine *self = qemu_coroutine_self();
137 Coroutine *to = self->caller;
138
139 trace_qemu_coroutine_yield(self, to);
140
141 if (!to) {
142 fprintf(stderr, "Co-routine is yielding to no one\n");
143 abort();
144 }
145
146 self->caller = NULL;
147 qemu_coroutine_switch(self, to, COROUTINE_YIELD);
148}
149
150bool qemu_coroutine_entered(Coroutine *co)
151{
152 return co->caller;
153}
154