2 * QEMU block layer thread pool
4 * Copyright IBM, Corp. 2008
5 * Copyright Red Hat, Inc. 2012
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
17 #include "qemu/osdep.h"
18 #include "qemu/queue.h"
19 #include "qemu/thread.h"
20 #include "qemu/coroutine.h"
22 #include "block/thread-pool.h"
23 #include "qemu/main-loop.h"
25 static void do_spawn_thread(ThreadPool
*pool
);
27 typedef struct ThreadPoolElement ThreadPoolElement
;
35 struct ThreadPoolElement
{
41 /* Moving state out of THREAD_QUEUED is protected by lock. After
42 * that, only the worker thread can write to it. Reads and writes
43 * of state and ret are ordered with memory barriers.
45 enum ThreadState state
;
48 /* Access to this list is protected by lock. */
49 QTAILQ_ENTRY(ThreadPoolElement
) reqs
;
51 /* Access to this list is protected by the global mutex. */
52 QLIST_ENTRY(ThreadPoolElement
) all
;
57 QEMUBH
*completion_bh
;
59 QemuCond worker_stopped
;
60 QemuCond request_cond
;
61 QEMUBH
*new_thread_bh
;
63 /* The following variables are only accessed from one AioContext. */
64 QLIST_HEAD(, ThreadPoolElement
) head
;
66 /* The following variables are protected by lock. */
67 QTAILQ_HEAD(, ThreadPoolElement
) request_list
;
70 int new_threads
; /* backlog of threads we need to create */
71 int pending_threads
; /* threads created but not running yet */
76 static void *worker_thread(void *opaque
)
78 ThreadPool
*pool
= opaque
;
80 qemu_mutex_lock(&pool
->lock
);
81 pool
->pending_threads
--;
82 do_spawn_thread(pool
);
84 while (pool
->cur_threads
<= pool
->max_threads
) {
85 ThreadPoolElement
*req
;
88 if (QTAILQ_EMPTY(&pool
->request_list
)) {
90 ret
= qemu_cond_timedwait(&pool
->request_cond
, &pool
->lock
, 10000);
93 QTAILQ_EMPTY(&pool
->request_list
) &&
94 pool
->cur_threads
> pool
->min_threads
) {
95 /* Timed out + no work to do + no need for warm threads = exit. */
99 * Even if there was some work to do, check if there aren't
100 * too many worker threads before picking it up.
105 req
= QTAILQ_FIRST(&pool
->request_list
);
106 QTAILQ_REMOVE(&pool
->request_list
, req
, reqs
);
107 req
->state
= THREAD_ACTIVE
;
108 qemu_mutex_unlock(&pool
->lock
);
110 ret
= req
->func(req
->arg
);
113 /* Write ret before state. */
115 req
->state
= THREAD_DONE
;
117 qemu_bh_schedule(pool
->completion_bh
);
118 qemu_mutex_lock(&pool
->lock
);
122 qemu_cond_signal(&pool
->worker_stopped
);
123 qemu_mutex_unlock(&pool
->lock
);
126 * Wake up another thread, in case we got a wakeup but decided
127 * to exit due to pool->cur_threads > pool->max_threads.
129 qemu_cond_signal(&pool
->request_cond
);
133 static void do_spawn_thread(ThreadPool
*pool
)
137 /* Runs with lock taken. */
138 if (!pool
->new_threads
) {
143 pool
->pending_threads
++;
145 qemu_thread_create(&t
, "worker", worker_thread
, pool
, QEMU_THREAD_DETACHED
);
148 static void spawn_thread_bh_fn(void *opaque
)
150 ThreadPool
*pool
= opaque
;
152 qemu_mutex_lock(&pool
->lock
);
153 do_spawn_thread(pool
);
154 qemu_mutex_unlock(&pool
->lock
);
157 static void spawn_thread(ThreadPool
*pool
)
161 /* If there are threads being created, they will spawn new workers, so
162 * we don't spend time creating many threads in a loop holding a mutex or
163 * starving the current vcpu.
165 * If there are no idle threads, ask the main thread to create one, so we
166 * inherit the correct affinity instead of the vcpu affinity.
168 if (!pool
->pending_threads
) {
169 qemu_bh_schedule(pool
->new_thread_bh
);
173 static void thread_pool_completion_bh(void *opaque
)
175 ThreadPool
*pool
= opaque
;
176 ThreadPoolElement
*elem
, *next
;
178 aio_context_acquire(pool
->ctx
);
180 QLIST_FOREACH_SAFE(elem
, &pool
->head
, all
, next
) {
181 if (elem
->state
!= THREAD_DONE
) {
185 trace_thread_pool_complete(pool
, elem
, elem
->common
.opaque
,
187 QLIST_REMOVE(elem
, all
);
189 if (elem
->common
.cb
) {
190 /* Read state before ret. */
193 /* Schedule ourselves in case elem->common.cb() calls aio_poll() to
194 * wait for another request that completed at the same time.
196 qemu_bh_schedule(pool
->completion_bh
);
198 aio_context_release(pool
->ctx
);
199 elem
->common
.cb(elem
->common
.opaque
, elem
->ret
);
200 aio_context_acquire(pool
->ctx
);
202 /* We can safely cancel the completion_bh here regardless of someone
203 * else having scheduled it meanwhile because we reenter the
204 * completion function anyway (goto restart).
206 qemu_bh_cancel(pool
->completion_bh
);
208 qemu_aio_unref(elem
);
211 qemu_aio_unref(elem
);
214 aio_context_release(pool
->ctx
);
217 static void thread_pool_cancel(BlockAIOCB
*acb
)
219 ThreadPoolElement
*elem
= (ThreadPoolElement
*)acb
;
220 ThreadPool
*pool
= elem
->pool
;
222 trace_thread_pool_cancel(elem
, elem
->common
.opaque
);
224 QEMU_LOCK_GUARD(&pool
->lock
);
225 if (elem
->state
== THREAD_QUEUED
) {
226 QTAILQ_REMOVE(&pool
->request_list
, elem
, reqs
);
227 qemu_bh_schedule(pool
->completion_bh
);
229 elem
->state
= THREAD_DONE
;
230 elem
->ret
= -ECANCELED
;
235 static AioContext
*thread_pool_get_aio_context(BlockAIOCB
*acb
)
237 ThreadPoolElement
*elem
= (ThreadPoolElement
*)acb
;
238 ThreadPool
*pool
= elem
->pool
;
242 static const AIOCBInfo thread_pool_aiocb_info
= {
243 .aiocb_size
= sizeof(ThreadPoolElement
),
244 .cancel_async
= thread_pool_cancel
,
245 .get_aio_context
= thread_pool_get_aio_context
,
248 BlockAIOCB
*thread_pool_submit_aio(ThreadPool
*pool
,
249 ThreadPoolFunc
*func
, void *arg
,
250 BlockCompletionFunc
*cb
, void *opaque
)
252 ThreadPoolElement
*req
;
254 req
= qemu_aio_get(&thread_pool_aiocb_info
, NULL
, cb
, opaque
);
257 req
->state
= THREAD_QUEUED
;
260 QLIST_INSERT_HEAD(&pool
->head
, req
, all
);
262 trace_thread_pool_submit(pool
, req
, arg
);
264 qemu_mutex_lock(&pool
->lock
);
265 if (pool
->idle_threads
== 0 && pool
->cur_threads
< pool
->max_threads
) {
268 QTAILQ_INSERT_TAIL(&pool
->request_list
, req
, reqs
);
269 qemu_mutex_unlock(&pool
->lock
);
270 qemu_cond_signal(&pool
->request_cond
);
274 typedef struct ThreadPoolCo
{
279 static void thread_pool_co_cb(void *opaque
, int ret
)
281 ThreadPoolCo
*co
= opaque
;
287 int coroutine_fn
thread_pool_submit_co(ThreadPool
*pool
, ThreadPoolFunc
*func
,
290 ThreadPoolCo tpc
= { .co
= qemu_coroutine_self(), .ret
= -EINPROGRESS
};
291 assert(qemu_in_coroutine());
292 thread_pool_submit_aio(pool
, func
, arg
, thread_pool_co_cb
, &tpc
);
293 qemu_coroutine_yield();
297 void thread_pool_submit(ThreadPool
*pool
, ThreadPoolFunc
*func
, void *arg
)
299 thread_pool_submit_aio(pool
, func
, arg
, NULL
, NULL
);
302 void thread_pool_update_params(ThreadPool
*pool
, AioContext
*ctx
)
304 qemu_mutex_lock(&pool
->lock
);
306 pool
->min_threads
= ctx
->thread_pool_min
;
307 pool
->max_threads
= ctx
->thread_pool_max
;
311 * - Increase the number available of threads until over the min_threads
313 * - Bump the worker threads so that they exit, until under the max_threads
315 * - Do nothing. The current number of threads fall in between the min and
316 * max thresholds. We'll let the pool manage itself.
318 for (int i
= pool
->cur_threads
; i
< pool
->min_threads
; i
++) {
322 for (int i
= pool
->cur_threads
; i
> pool
->max_threads
; i
--) {
323 qemu_cond_signal(&pool
->request_cond
);
326 qemu_mutex_unlock(&pool
->lock
);
329 static void thread_pool_init_one(ThreadPool
*pool
, AioContext
*ctx
)
332 ctx
= qemu_get_aio_context();
335 memset(pool
, 0, sizeof(*pool
));
337 pool
->completion_bh
= aio_bh_new(ctx
, thread_pool_completion_bh
, pool
);
338 qemu_mutex_init(&pool
->lock
);
339 qemu_cond_init(&pool
->worker_stopped
);
340 qemu_cond_init(&pool
->request_cond
);
341 pool
->new_thread_bh
= aio_bh_new(ctx
, spawn_thread_bh_fn
, pool
);
343 QLIST_INIT(&pool
->head
);
344 QTAILQ_INIT(&pool
->request_list
);
346 thread_pool_update_params(pool
, ctx
);
349 ThreadPool
*thread_pool_new(AioContext
*ctx
)
351 ThreadPool
*pool
= g_new(ThreadPool
, 1);
352 thread_pool_init_one(pool
, ctx
);
356 void thread_pool_free(ThreadPool
*pool
)
362 assert(QLIST_EMPTY(&pool
->head
));
364 qemu_mutex_lock(&pool
->lock
);
366 /* Stop new threads from spawning */
367 qemu_bh_delete(pool
->new_thread_bh
);
368 pool
->cur_threads
-= pool
->new_threads
;
369 pool
->new_threads
= 0;
371 /* Wait for worker threads to terminate */
372 pool
->max_threads
= 0;
373 qemu_cond_broadcast(&pool
->request_cond
);
374 while (pool
->cur_threads
> 0) {
375 qemu_cond_wait(&pool
->worker_stopped
, &pool
->lock
);
378 qemu_mutex_unlock(&pool
->lock
);
380 qemu_bh_delete(pool
->completion_bh
);
381 qemu_cond_destroy(&pool
->request_cond
);
382 qemu_cond_destroy(&pool
->worker_stopped
);
383 qemu_mutex_destroy(&pool
->lock
);