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-common.h"
18 #include "qemu-queue.h"
19 #include "qemu-thread.h"
21 #include "qemu-coroutine.h"
23 #include "block_int.h"
24 #include "event_notifier.h"
25 #include "thread-pool.h"
27 static void do_spawn_thread(void);
29 typedef struct ThreadPoolElement ThreadPoolElement
;
38 struct ThreadPoolElement
{
39 BlockDriverAIOCB common
;
42 enum ThreadState state
;
45 /* Access to this list is protected by lock. */
46 QTAILQ_ENTRY(ThreadPoolElement
) reqs
;
48 /* Access to this list is protected by the global mutex. */
49 QLIST_ENTRY(ThreadPoolElement
) all
;
52 static EventNotifier notifier
;
53 static QemuMutex lock
;
54 static QemuCond check_cancel
;
55 static QemuSemaphore sem
;
56 static int max_threads
= 64;
57 static QEMUBH
*new_thread_bh
;
59 /* The following variables are protected by the global mutex. */
60 static QLIST_HEAD(, ThreadPoolElement
) head
;
62 /* The following variables are protected by lock. */
63 static QTAILQ_HEAD(, ThreadPoolElement
) request_list
;
64 static int cur_threads
;
65 static int idle_threads
;
66 static int new_threads
; /* backlog of threads we need to create */
67 static int pending_threads
; /* threads created but not running yet */
68 static int pending_cancellations
; /* whether we need a cond_broadcast */
70 static void *worker_thread(void *unused
)
72 qemu_mutex_lock(&lock
);
77 ThreadPoolElement
*req
;
82 qemu_mutex_unlock(&lock
);
83 ret
= qemu_sem_timedwait(&sem
, 10000);
84 qemu_mutex_lock(&lock
);
86 } while (ret
== -1 && !QTAILQ_EMPTY(&request_list
));
91 req
= QTAILQ_FIRST(&request_list
);
92 QTAILQ_REMOVE(&request_list
, req
, reqs
);
93 req
->state
= THREAD_ACTIVE
;
94 qemu_mutex_unlock(&lock
);
96 ret
= req
->func(req
->arg
);
98 qemu_mutex_lock(&lock
);
99 req
->state
= THREAD_DONE
;
101 if (pending_cancellations
) {
102 qemu_cond_broadcast(&check_cancel
);
105 event_notifier_set(¬ifier
);
109 qemu_mutex_unlock(&lock
);
113 static void do_spawn_thread(void)
117 /* Runs with lock taken. */
125 qemu_thread_create(&t
, worker_thread
, NULL
, QEMU_THREAD_DETACHED
);
128 static void spawn_thread_bh_fn(void *opaque
)
130 qemu_mutex_lock(&lock
);
132 qemu_mutex_unlock(&lock
);
135 static void spawn_thread(void)
139 /* If there are threads being created, they will spawn new workers, so
140 * we don't spend time creating many threads in a loop holding a mutex or
141 * starving the current vcpu.
143 * If there are no idle threads, ask the main thread to create one, so we
144 * inherit the correct affinity instead of the vcpu affinity.
146 if (!pending_threads
) {
147 qemu_bh_schedule(new_thread_bh
);
151 static void event_notifier_ready(EventNotifier
*notifier
)
153 ThreadPoolElement
*elem
, *next
;
155 event_notifier_test_and_clear(notifier
);
157 QLIST_FOREACH_SAFE(elem
, &head
, all
, next
) {
158 if (elem
->state
!= THREAD_CANCELED
&& elem
->state
!= THREAD_DONE
) {
161 if (elem
->state
== THREAD_DONE
) {
162 trace_thread_pool_complete(elem
, elem
->common
.opaque
, elem
->ret
);
164 if (elem
->state
== THREAD_DONE
&& elem
->common
.cb
) {
165 qemu_mutex_lock(&lock
);
167 qemu_mutex_unlock(&lock
);
168 QLIST_REMOVE(elem
, all
);
169 elem
->common
.cb(elem
->common
.opaque
, ret
);
170 qemu_aio_release(elem
);
173 /* remove the request */
174 QLIST_REMOVE(elem
, all
);
175 qemu_aio_release(elem
);
180 static int thread_pool_active(EventNotifier
*notifier
)
182 return !QLIST_EMPTY(&head
);
185 static void thread_pool_cancel(BlockDriverAIOCB
*acb
)
187 ThreadPoolElement
*elem
= (ThreadPoolElement
*)acb
;
189 trace_thread_pool_cancel(elem
, elem
->common
.opaque
);
191 qemu_mutex_lock(&lock
);
192 if (elem
->state
== THREAD_QUEUED
&&
193 /* No thread has yet started working on elem. we can try to "steal"
194 * the item from the worker if we can get a signal from the
195 * semaphore. Because this is non-blocking, we can do it with
196 * the lock taken and ensure that elem will remain THREAD_QUEUED.
198 qemu_sem_timedwait(&sem
, 0) == 0) {
199 QTAILQ_REMOVE(&request_list
, elem
, reqs
);
200 elem
->state
= THREAD_CANCELED
;
201 event_notifier_set(¬ifier
);
203 pending_cancellations
++;
204 while (elem
->state
!= THREAD_CANCELED
&& elem
->state
!= THREAD_DONE
) {
205 qemu_cond_wait(&check_cancel
, &lock
);
207 pending_cancellations
--;
209 qemu_mutex_unlock(&lock
);
212 static AIOPool thread_pool_cb_pool
= {
213 .aiocb_size
= sizeof(ThreadPoolElement
),
214 .cancel
= thread_pool_cancel
,
217 BlockDriverAIOCB
*thread_pool_submit_aio(ThreadPoolFunc
*func
, void *arg
,
218 BlockDriverCompletionFunc
*cb
, void *opaque
)
220 ThreadPoolElement
*req
;
222 req
= qemu_aio_get(&thread_pool_cb_pool
, NULL
, cb
, opaque
);
225 req
->state
= THREAD_QUEUED
;
227 QLIST_INSERT_HEAD(&head
, req
, all
);
229 trace_thread_pool_submit(req
, arg
);
231 qemu_mutex_lock(&lock
);
232 if (idle_threads
== 0 && cur_threads
< max_threads
) {
235 QTAILQ_INSERT_TAIL(&request_list
, req
, reqs
);
236 qemu_mutex_unlock(&lock
);
241 typedef struct ThreadPoolCo
{
246 static void thread_pool_co_cb(void *opaque
, int ret
)
248 ThreadPoolCo
*co
= opaque
;
251 qemu_coroutine_enter(co
->co
, NULL
);
254 int coroutine_fn
thread_pool_submit_co(ThreadPoolFunc
*func
, void *arg
)
256 ThreadPoolCo tpc
= { .co
= qemu_coroutine_self(), .ret
= -EINPROGRESS
};
257 assert(qemu_in_coroutine());
258 thread_pool_submit_aio(func
, arg
, thread_pool_co_cb
, &tpc
);
259 qemu_coroutine_yield();
263 void thread_pool_submit(ThreadPoolFunc
*func
, void *arg
)
265 thread_pool_submit_aio(func
, arg
, NULL
, NULL
);
268 static void thread_pool_init(void)
271 event_notifier_init(¬ifier
, false);
272 qemu_mutex_init(&lock
);
273 qemu_cond_init(&check_cancel
);
274 qemu_sem_init(&sem
, 0);
275 qemu_aio_set_event_notifier(¬ifier
, event_notifier_ready
,
278 QTAILQ_INIT(&request_list
);
279 new_thread_bh
= qemu_bh_new(spawn_thread_bh_fn
, NULL
);
282 block_init(thread_pool_init
)