aio: add a ThreadPool instance to AioContext
[qemu/cris-port.git] / thread-pool.c
blobd1e4570829fbbc0fd5d944fd29437533826f9f62
1 /*
2 * QEMU block layer thread pool
4 * Copyright IBM, Corp. 2008
5 * Copyright Red Hat, Inc. 2012
7 * Authors:
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"
20 #include "qemu/osdep.h"
21 #include "block/coroutine.h"
22 #include "trace.h"
23 #include "block/block_int.h"
24 #include "qemu/event_notifier.h"
25 #include "block/thread-pool.h"
27 static void do_spawn_thread(ThreadPool *pool);
29 typedef struct ThreadPoolElement ThreadPoolElement;
31 enum ThreadState {
32 THREAD_QUEUED,
33 THREAD_ACTIVE,
34 THREAD_DONE,
35 THREAD_CANCELED,
38 struct ThreadPoolElement {
39 BlockDriverAIOCB common;
40 ThreadPool *pool;
41 ThreadPoolFunc *func;
42 void *arg;
44 /* Moving state out of THREAD_QUEUED is protected by lock. After
45 * that, only the worker thread can write to it. Reads and writes
46 * of state and ret are ordered with memory barriers.
48 enum ThreadState state;
49 int ret;
51 /* Access to this list is protected by lock. */
52 QTAILQ_ENTRY(ThreadPoolElement) reqs;
54 /* Access to this list is protected by the global mutex. */
55 QLIST_ENTRY(ThreadPoolElement) all;
58 struct ThreadPool {
59 EventNotifier notifier;
60 AioContext *ctx;
61 QemuMutex lock;
62 QemuCond check_cancel;
63 QemuCond worker_stopped;
64 QemuSemaphore sem;
65 int max_threads;
66 QEMUBH *new_thread_bh;
68 /* The following variables are only accessed from one AioContext. */
69 QLIST_HEAD(, ThreadPoolElement) head;
71 /* The following variables are protected by lock. */
72 QTAILQ_HEAD(, ThreadPoolElement) request_list;
73 int cur_threads;
74 int idle_threads;
75 int new_threads; /* backlog of threads we need to create */
76 int pending_threads; /* threads created but not running yet */
77 int pending_cancellations; /* whether we need a cond_broadcast */
78 bool stopping;
81 /* Currently there is only one thread pool instance. */
82 static ThreadPool global_pool;
84 static void *worker_thread(void *opaque)
86 ThreadPool *pool = opaque;
88 qemu_mutex_lock(&pool->lock);
89 pool->pending_threads--;
90 do_spawn_thread(pool);
92 while (!pool->stopping) {
93 ThreadPoolElement *req;
94 int ret;
96 do {
97 pool->idle_threads++;
98 qemu_mutex_unlock(&pool->lock);
99 ret = qemu_sem_timedwait(&pool->sem, 10000);
100 qemu_mutex_lock(&pool->lock);
101 pool->idle_threads--;
102 } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list));
103 if (ret == -1 || pool->stopping) {
104 break;
107 req = QTAILQ_FIRST(&pool->request_list);
108 QTAILQ_REMOVE(&pool->request_list, req, reqs);
109 req->state = THREAD_ACTIVE;
110 qemu_mutex_unlock(&pool->lock);
112 ret = req->func(req->arg);
114 req->ret = ret;
115 /* Write ret before state. */
116 smp_wmb();
117 req->state = THREAD_DONE;
119 qemu_mutex_lock(&pool->lock);
120 if (pool->pending_cancellations) {
121 qemu_cond_broadcast(&pool->check_cancel);
124 event_notifier_set(&pool->notifier);
127 pool->cur_threads--;
128 qemu_cond_signal(&pool->worker_stopped);
129 qemu_mutex_unlock(&pool->lock);
130 return NULL;
133 static void do_spawn_thread(ThreadPool *pool)
135 QemuThread t;
137 /* Runs with lock taken. */
138 if (!pool->new_threads) {
139 return;
142 pool->new_threads--;
143 pool->pending_threads++;
145 qemu_thread_create(&t, 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)
159 pool->cur_threads++;
160 pool->new_threads++;
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 event_notifier_ready(EventNotifier *notifier)
175 ThreadPool *pool = container_of(notifier, ThreadPool, notifier);
176 ThreadPoolElement *elem, *next;
178 event_notifier_test_and_clear(notifier);
179 restart:
180 QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
181 if (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) {
182 continue;
184 if (elem->state == THREAD_DONE) {
185 trace_thread_pool_complete(pool, elem, elem->common.opaque,
186 elem->ret);
188 if (elem->state == THREAD_DONE && elem->common.cb) {
189 QLIST_REMOVE(elem, all);
190 /* Read state before ret. */
191 smp_rmb();
192 elem->common.cb(elem->common.opaque, elem->ret);
193 qemu_aio_release(elem);
194 goto restart;
195 } else {
196 /* remove the request */
197 QLIST_REMOVE(elem, all);
198 qemu_aio_release(elem);
203 static int thread_pool_active(EventNotifier *notifier)
205 ThreadPool *pool = container_of(notifier, ThreadPool, notifier);
206 return !QLIST_EMPTY(&pool->head);
209 static void thread_pool_cancel(BlockDriverAIOCB *acb)
211 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
212 ThreadPool *pool = elem->pool;
214 trace_thread_pool_cancel(elem, elem->common.opaque);
216 qemu_mutex_lock(&pool->lock);
217 if (elem->state == THREAD_QUEUED &&
218 /* No thread has yet started working on elem. we can try to "steal"
219 * the item from the worker if we can get a signal from the
220 * semaphore. Because this is non-blocking, we can do it with
221 * the lock taken and ensure that elem will remain THREAD_QUEUED.
223 qemu_sem_timedwait(&pool->sem, 0) == 0) {
224 QTAILQ_REMOVE(&pool->request_list, elem, reqs);
225 elem->state = THREAD_CANCELED;
226 event_notifier_set(&pool->notifier);
227 } else {
228 pool->pending_cancellations++;
229 while (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) {
230 qemu_cond_wait(&pool->check_cancel, &pool->lock);
232 pool->pending_cancellations--;
234 qemu_mutex_unlock(&pool->lock);
237 static const AIOCBInfo thread_pool_aiocb_info = {
238 .aiocb_size = sizeof(ThreadPoolElement),
239 .cancel = thread_pool_cancel,
242 BlockDriverAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg,
243 BlockDriverCompletionFunc *cb, void *opaque)
245 ThreadPool *pool = &global_pool;
246 ThreadPoolElement *req;
248 req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
249 req->func = func;
250 req->arg = arg;
251 req->state = THREAD_QUEUED;
252 req->pool = pool;
254 QLIST_INSERT_HEAD(&pool->head, req, all);
256 trace_thread_pool_submit(pool, req, arg);
258 qemu_mutex_lock(&pool->lock);
259 if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
260 spawn_thread(pool);
262 QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
263 qemu_mutex_unlock(&pool->lock);
264 qemu_sem_post(&pool->sem);
265 return &req->common;
268 typedef struct ThreadPoolCo {
269 Coroutine *co;
270 int ret;
271 } ThreadPoolCo;
273 static void thread_pool_co_cb(void *opaque, int ret)
275 ThreadPoolCo *co = opaque;
277 co->ret = ret;
278 qemu_coroutine_enter(co->co, NULL);
281 int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg)
283 ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
284 assert(qemu_in_coroutine());
285 thread_pool_submit_aio(func, arg, thread_pool_co_cb, &tpc);
286 qemu_coroutine_yield();
287 return tpc.ret;
290 void thread_pool_submit(ThreadPoolFunc *func, void *arg)
292 thread_pool_submit_aio(func, arg, NULL, NULL);
295 static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx)
297 if (!ctx) {
298 ctx = qemu_get_aio_context();
301 memset(pool, 0, sizeof(*pool));
302 event_notifier_init(&pool->notifier, false);
303 pool->ctx = ctx;
304 qemu_mutex_init(&pool->lock);
305 qemu_cond_init(&pool->check_cancel);
306 qemu_cond_init(&pool->worker_stopped);
307 qemu_sem_init(&pool->sem, 0);
308 pool->max_threads = 64;
309 pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
311 QLIST_INIT(&pool->head);
312 QTAILQ_INIT(&pool->request_list);
314 aio_set_event_notifier(ctx, &pool->notifier, event_notifier_ready,
315 thread_pool_active);
318 ThreadPool *thread_pool_new(AioContext *ctx)
320 ThreadPool *pool = g_new(ThreadPool, 1);
321 thread_pool_init_one(pool, ctx);
322 return pool;
325 void thread_pool_free(ThreadPool *pool)
327 if (!pool) {
328 return;
331 assert(QLIST_EMPTY(&pool->head));
333 qemu_mutex_lock(&pool->lock);
335 /* Stop new threads from spawning */
336 qemu_bh_delete(pool->new_thread_bh);
337 pool->cur_threads -= pool->new_threads;
338 pool->new_threads = 0;
340 /* Wait for worker threads to terminate */
341 pool->stopping = true;
342 while (pool->cur_threads > 0) {
343 qemu_sem_post(&pool->sem);
344 qemu_cond_wait(&pool->worker_stopped, &pool->lock);
347 qemu_mutex_unlock(&pool->lock);
349 aio_set_event_notifier(pool->ctx, &pool->notifier, NULL, NULL);
350 qemu_sem_destroy(&pool->sem);
351 qemu_cond_destroy(&pool->check_cancel);
352 qemu_cond_destroy(&pool->worker_stopped);
353 qemu_mutex_destroy(&pool->lock);
354 event_notifier_cleanup(&pool->notifier);
355 g_free(pool);
358 static void thread_pool_init(void)
360 thread_pool_init_one(&global_pool, NULL);
363 block_init(thread_pool_init)