1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include "qemu/osdep.h"
18#include "qemu-common.h"
19#include "qemu/queue.h"
20#include "qemu/thread.h"
21#include "qemu/coroutine.h"
22#include "trace.h"
23#include "block/thread-pool.h"
24#include "qemu/main-loop.h"
25
26static void do_spawn_thread(ThreadPool *pool);
27
28typedef struct ThreadPoolElement ThreadPoolElement;
29
30enum ThreadState {
31 THREAD_QUEUED,
32 THREAD_ACTIVE,
33 THREAD_DONE,
34};
35
36struct ThreadPoolElement {
37 BlockAIOCB common;
38 ThreadPool *pool;
39 ThreadPoolFunc *func;
40 void *arg;
41
42
43
44
45
46 enum ThreadState state;
47 int ret;
48
49
50 QTAILQ_ENTRY(ThreadPoolElement) reqs;
51
52
53 QLIST_ENTRY(ThreadPoolElement) all;
54};
55
56struct ThreadPool {
57 AioContext *ctx;
58 QEMUBH *completion_bh;
59 QemuMutex lock;
60 QemuCond worker_stopped;
61 QemuSemaphore sem;
62 int max_threads;
63 QEMUBH *new_thread_bh;
64
65
66 QLIST_HEAD(, ThreadPoolElement) head;
67
68
69 QTAILQ_HEAD(, ThreadPoolElement) request_list;
70 int cur_threads;
71 int idle_threads;
72 int new_threads;
73 int pending_threads;
74 bool stopping;
75};
76
77static void *worker_thread(void *opaque)
78{
79 ThreadPool *pool = opaque;
80
81 qemu_mutex_lock(&pool->lock);
82 pool->pending_threads--;
83 do_spawn_thread(pool);
84
85 while (!pool->stopping) {
86 ThreadPoolElement *req;
87 int ret;
88
89 do {
90 pool->idle_threads++;
91 qemu_mutex_unlock(&pool->lock);
92 ret = qemu_sem_timedwait(&pool->sem, 10000);
93 qemu_mutex_lock(&pool->lock);
94 pool->idle_threads--;
95 } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list));
96 if (ret == -1 || pool->stopping) {
97 break;
98 }
99
100 req = QTAILQ_FIRST(&pool->request_list);
101 QTAILQ_REMOVE(&pool->request_list, req, reqs);
102 req->state = THREAD_ACTIVE;
103 qemu_mutex_unlock(&pool->lock);
104
105 ret = req->func(req->arg);
106
107 req->ret = ret;
108
109 smp_wmb();
110 req->state = THREAD_DONE;
111
112 qemu_mutex_lock(&pool->lock);
113
114 qemu_bh_schedule(pool->completion_bh);
115 }
116
117 pool->cur_threads--;
118 qemu_cond_signal(&pool->worker_stopped);
119 qemu_mutex_unlock(&pool->lock);
120 return NULL;
121}
122
123static void do_spawn_thread(ThreadPool *pool)
124{
125 QemuThread t;
126
127
128 if (!pool->new_threads) {
129 return;
130 }
131
132 pool->new_threads--;
133 pool->pending_threads++;
134
135 qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED);
136}
137
138static void spawn_thread_bh_fn(void *opaque)
139{
140 ThreadPool *pool = opaque;
141
142 qemu_mutex_lock(&pool->lock);
143 do_spawn_thread(pool);
144 qemu_mutex_unlock(&pool->lock);
145}
146
147static void spawn_thread(ThreadPool *pool)
148{
149 pool->cur_threads++;
150 pool->new_threads++;
151
152
153
154
155
156
157
158 if (!pool->pending_threads) {
159 qemu_bh_schedule(pool->new_thread_bh);
160 }
161}
162
163static void thread_pool_completion_bh(void *opaque)
164{
165 ThreadPool *pool = opaque;
166 ThreadPoolElement *elem, *next;
167
168restart:
169 QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
170 if (elem->state != THREAD_DONE) {
171 continue;
172 }
173
174 trace_thread_pool_complete(pool, elem, elem->common.opaque,
175 elem->ret);
176 QLIST_REMOVE(elem, all);
177
178 if (elem->common.cb) {
179
180 smp_rmb();
181
182
183
184
185 qemu_bh_schedule(pool->completion_bh);
186
187 elem->common.cb(elem->common.opaque, elem->ret);
188
189
190
191
192
193 qemu_bh_cancel(pool->completion_bh);
194
195 qemu_aio_unref(elem);
196 goto restart;
197 } else {
198 qemu_aio_unref(elem);
199 }
200 }
201}
202
203static void thread_pool_cancel(BlockAIOCB *acb)
204{
205 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
206 ThreadPool *pool = elem->pool;
207
208 trace_thread_pool_cancel(elem, elem->common.opaque);
209
210 qemu_mutex_lock(&pool->lock);
211 if (elem->state == THREAD_QUEUED &&
212
213
214
215
216
217 qemu_sem_timedwait(&pool->sem, 0) == 0) {
218 QTAILQ_REMOVE(&pool->request_list, elem, reqs);
219 qemu_bh_schedule(pool->completion_bh);
220
221 elem->state = THREAD_DONE;
222 elem->ret = -ECANCELED;
223 }
224
225 qemu_mutex_unlock(&pool->lock);
226}
227
228static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb)
229{
230 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
231 ThreadPool *pool = elem->pool;
232 return pool->ctx;
233}
234
235static const AIOCBInfo thread_pool_aiocb_info = {
236 .aiocb_size = sizeof(ThreadPoolElement),
237 .cancel_async = thread_pool_cancel,
238 .get_aio_context = thread_pool_get_aio_context,
239};
240
241BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool,
242 ThreadPoolFunc *func, void *arg,
243 BlockCompletionFunc *cb, void *opaque)
244{
245 ThreadPoolElement *req;
246
247 req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
248 req->func = func;
249 req->arg = arg;
250 req->state = THREAD_QUEUED;
251 req->pool = pool;
252
253 QLIST_INSERT_HEAD(&pool->head, req, all);
254
255 trace_thread_pool_submit(pool, req, arg);
256
257 qemu_mutex_lock(&pool->lock);
258 if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
259 spawn_thread(pool);
260 }
261 QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
262 qemu_mutex_unlock(&pool->lock);
263 qemu_sem_post(&pool->sem);
264 return &req->common;
265}
266
267typedef struct ThreadPoolCo {
268 Coroutine *co;
269 int ret;
270} ThreadPoolCo;
271
272static void thread_pool_co_cb(void *opaque, int ret)
273{
274 ThreadPoolCo *co = opaque;
275
276 co->ret = ret;
277 qemu_coroutine_enter(co->co);
278}
279
280int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func,
281 void *arg)
282{
283 ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
284 assert(qemu_in_coroutine());
285 thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc);
286 qemu_coroutine_yield();
287 return tpc.ret;
288}
289
290void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg)
291{
292 thread_pool_submit_aio(pool, func, arg, NULL, NULL);
293}
294
295static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx)
296{
297 if (!ctx) {
298 ctx = qemu_get_aio_context();
299 }
300
301 memset(pool, 0, sizeof(*pool));
302 pool->ctx = ctx;
303 pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool);
304 qemu_mutex_init(&pool->lock);
305 qemu_cond_init(&pool->worker_stopped);
306 qemu_sem_init(&pool->sem, 0);
307 pool->max_threads = 64;
308 pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
309
310 QLIST_INIT(&pool->head);
311 QTAILQ_INIT(&pool->request_list);
312}
313
314ThreadPool *thread_pool_new(AioContext *ctx)
315{
316 ThreadPool *pool = g_new(ThreadPool, 1);
317 thread_pool_init_one(pool, ctx);
318 return pool;
319}
320
321void thread_pool_free(ThreadPool *pool)
322{
323 if (!pool) {
324 return;
325 }
326
327 assert(QLIST_EMPTY(&pool->head));
328
329 qemu_mutex_lock(&pool->lock);
330
331
332 qemu_bh_delete(pool->new_thread_bh);
333 pool->cur_threads -= pool->new_threads;
334 pool->new_threads = 0;
335
336
337 pool->stopping = true;
338 while (pool->cur_threads > 0) {
339 qemu_sem_post(&pool->sem);
340 qemu_cond_wait(&pool->worker_stopped, &pool->lock);
341 }
342
343 qemu_mutex_unlock(&pool->lock);
344
345 qemu_bh_delete(pool->completion_bh);
346 qemu_sem_destroy(&pool->sem);
347 qemu_cond_destroy(&pool->worker_stopped);
348 qemu_mutex_destroy(&pool->lock);
349 g_free(pool);
350}
351