Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU block layer thread pool |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2008 |
| 5 | * Copyright Red Hat, Inc. 2012 |
| 6 | * |
| 7 | * Authors: |
| 8 | * Anthony Liguori <aliguori@us.ibm.com> |
| 9 | * Paolo Bonzini <pbonzini@redhat.com> |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 12 | * the COPYING file in the top-level directory. |
| 13 | * |
| 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. |
| 16 | */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 17 | #include "qemu/osdep.h" |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 18 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 19 | #include "qemu/queue.h" |
| 20 | #include "qemu/thread.h" |
Daniel P. Berrange | 10817bf | 2015-09-01 14:48:02 +0100 | [diff] [blame] | 21 | #include "qemu/coroutine.h" |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 22 | #include "trace.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 23 | #include "block/thread-pool.h" |
Alex Bligh | 6a1751b | 2013-08-21 16:02:47 +0100 | [diff] [blame] | 24 | #include "qemu/main-loop.h" |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 25 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 26 | static void do_spawn_thread(ThreadPool *pool); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 27 | |
| 28 | typedef struct ThreadPoolElement ThreadPoolElement; |
| 29 | |
| 30 | enum ThreadState { |
| 31 | THREAD_QUEUED, |
| 32 | THREAD_ACTIVE, |
| 33 | THREAD_DONE, |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | struct ThreadPoolElement { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 37 | BlockAIOCB common; |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 38 | ThreadPool *pool; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 39 | ThreadPoolFunc *func; |
| 40 | void *arg; |
Paolo Bonzini | 19d092c | 2012-10-31 10:09:11 +0100 | [diff] [blame] | 41 | |
| 42 | /* Moving state out of THREAD_QUEUED is protected by lock. After |
| 43 | * that, only the worker thread can write to it. Reads and writes |
| 44 | * of state and ret are ordered with memory barriers. |
| 45 | */ |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 46 | enum ThreadState state; |
| 47 | int ret; |
| 48 | |
| 49 | /* Access to this list is protected by lock. */ |
| 50 | QTAILQ_ENTRY(ThreadPoolElement) reqs; |
| 51 | |
| 52 | /* Access to this list is protected by the global mutex. */ |
| 53 | QLIST_ENTRY(ThreadPoolElement) all; |
| 54 | }; |
| 55 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 56 | struct ThreadPool { |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 57 | AioContext *ctx; |
Stefan Hajnoczi | c2e50e3 | 2014-07-15 16:44:25 +0200 | [diff] [blame] | 58 | QEMUBH *completion_bh; |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 59 | QemuMutex lock; |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 60 | QemuCond worker_stopped; |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 61 | QemuSemaphore sem; |
| 62 | int max_threads; |
| 63 | QEMUBH *new_thread_bh; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 64 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 65 | /* The following variables are only accessed from one AioContext. */ |
| 66 | QLIST_HEAD(, ThreadPoolElement) head; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 67 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 68 | /* The following variables are protected by lock. */ |
| 69 | QTAILQ_HEAD(, ThreadPoolElement) request_list; |
| 70 | int cur_threads; |
| 71 | int idle_threads; |
| 72 | int new_threads; /* backlog of threads we need to create */ |
| 73 | int pending_threads; /* threads created but not running yet */ |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 74 | bool stopping; |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 75 | }; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 76 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 77 | static void *worker_thread(void *opaque) |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 78 | { |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 79 | ThreadPool *pool = opaque; |
| 80 | |
| 81 | qemu_mutex_lock(&pool->lock); |
| 82 | pool->pending_threads--; |
| 83 | do_spawn_thread(pool); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 84 | |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 85 | while (!pool->stopping) { |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 86 | ThreadPoolElement *req; |
| 87 | int ret; |
| 88 | |
| 89 | do { |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 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)); |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 96 | if (ret == -1 || pool->stopping) { |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 97 | break; |
| 98 | } |
| 99 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 100 | req = QTAILQ_FIRST(&pool->request_list); |
| 101 | QTAILQ_REMOVE(&pool->request_list, req, reqs); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 102 | req->state = THREAD_ACTIVE; |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 103 | qemu_mutex_unlock(&pool->lock); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 104 | |
| 105 | ret = req->func(req->arg); |
| 106 | |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 107 | req->ret = ret; |
Paolo Bonzini | 19d092c | 2012-10-31 10:09:11 +0100 | [diff] [blame] | 108 | /* Write ret before state. */ |
| 109 | smp_wmb(); |
| 110 | req->state = THREAD_DONE; |
| 111 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 112 | qemu_mutex_lock(&pool->lock); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 113 | |
Stefan Hajnoczi | c2e50e3 | 2014-07-15 16:44:25 +0200 | [diff] [blame] | 114 | qemu_bh_schedule(pool->completion_bh); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 117 | pool->cur_threads--; |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 118 | qemu_cond_signal(&pool->worker_stopped); |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 119 | qemu_mutex_unlock(&pool->lock); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
| 122 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 123 | static void do_spawn_thread(ThreadPool *pool) |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 124 | { |
| 125 | QemuThread t; |
| 126 | |
| 127 | /* Runs with lock taken. */ |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 128 | if (!pool->new_threads) { |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 132 | pool->new_threads--; |
| 133 | pool->pending_threads++; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 134 | |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 135 | qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void spawn_thread_bh_fn(void *opaque) |
| 139 | { |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 140 | ThreadPool *pool = opaque; |
| 141 | |
| 142 | qemu_mutex_lock(&pool->lock); |
| 143 | do_spawn_thread(pool); |
| 144 | qemu_mutex_unlock(&pool->lock); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 147 | static void spawn_thread(ThreadPool *pool) |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 148 | { |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 149 | pool->cur_threads++; |
| 150 | pool->new_threads++; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 151 | /* If there are threads being created, they will spawn new workers, so |
| 152 | * we don't spend time creating many threads in a loop holding a mutex or |
| 153 | * starving the current vcpu. |
| 154 | * |
| 155 | * If there are no idle threads, ask the main thread to create one, so we |
| 156 | * inherit the correct affinity instead of the vcpu affinity. |
| 157 | */ |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 158 | if (!pool->pending_threads) { |
| 159 | qemu_bh_schedule(pool->new_thread_bh); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Stefan Hajnoczi | c2e50e3 | 2014-07-15 16:44:25 +0200 | [diff] [blame] | 163 | static void thread_pool_completion_bh(void *opaque) |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 164 | { |
Stefan Hajnoczi | c2e50e3 | 2014-07-15 16:44:25 +0200 | [diff] [blame] | 165 | ThreadPool *pool = opaque; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 166 | ThreadPoolElement *elem, *next; |
| 167 | |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 168 | restart: |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 169 | QLIST_FOREACH_SAFE(elem, &pool->head, all, next) { |
Fam Zheng | 3391f5e | 2014-09-11 13:41:12 +0800 | [diff] [blame] | 170 | if (elem->state != THREAD_DONE) { |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 171 | continue; |
| 172 | } |
Stefan Hajnoczi | 1faa5bb | 2015-04-02 17:39:22 +0100 | [diff] [blame] | 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) { |
Paolo Bonzini | 19d092c | 2012-10-31 10:09:11 +0100 | [diff] [blame] | 179 | /* Read state before ret. */ |
| 180 | smp_rmb(); |
Stefan Hajnoczi | 3c80ca1 | 2014-07-15 16:44:26 +0200 | [diff] [blame] | 181 | |
| 182 | /* Schedule ourselves in case elem->common.cb() calls aio_poll() to |
| 183 | * wait for another request that completed at the same time. |
| 184 | */ |
| 185 | qemu_bh_schedule(pool->completion_bh); |
| 186 | |
Paolo Bonzini | 19d092c | 2012-10-31 10:09:11 +0100 | [diff] [blame] | 187 | elem->common.cb(elem->common.opaque, elem->ret); |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 188 | qemu_aio_unref(elem); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 189 | goto restart; |
| 190 | } else { |
Fam Zheng | 8007429 | 2014-09-11 13:41:28 +0800 | [diff] [blame] | 191 | qemu_aio_unref(elem); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 196 | static void thread_pool_cancel(BlockAIOCB *acb) |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 197 | { |
| 198 | ThreadPoolElement *elem = (ThreadPoolElement *)acb; |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 199 | ThreadPool *pool = elem->pool; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 200 | |
| 201 | trace_thread_pool_cancel(elem, elem->common.opaque); |
| 202 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 203 | qemu_mutex_lock(&pool->lock); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 204 | if (elem->state == THREAD_QUEUED && |
| 205 | /* No thread has yet started working on elem. we can try to "steal" |
| 206 | * the item from the worker if we can get a signal from the |
| 207 | * semaphore. Because this is non-blocking, we can do it with |
| 208 | * the lock taken and ensure that elem will remain THREAD_QUEUED. |
| 209 | */ |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 210 | qemu_sem_timedwait(&pool->sem, 0) == 0) { |
| 211 | QTAILQ_REMOVE(&pool->request_list, elem, reqs); |
Stefan Hajnoczi | c2e50e3 | 2014-07-15 16:44:25 +0200 | [diff] [blame] | 212 | qemu_bh_schedule(pool->completion_bh); |
Fam Zheng | 3391f5e | 2014-09-11 13:41:12 +0800 | [diff] [blame] | 213 | |
| 214 | elem->state = THREAD_DONE; |
| 215 | elem->ret = -ECANCELED; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 216 | } |
Fam Zheng | 3391f5e | 2014-09-11 13:41:12 +0800 | [diff] [blame] | 217 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 218 | qemu_mutex_unlock(&pool->lock); |
Fam Zheng | 3391f5e | 2014-09-11 13:41:12 +0800 | [diff] [blame] | 219 | } |
| 220 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 221 | static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb) |
Fam Zheng | 3391f5e | 2014-09-11 13:41:12 +0800 | [diff] [blame] | 222 | { |
| 223 | ThreadPoolElement *elem = (ThreadPoolElement *)acb; |
| 224 | ThreadPool *pool = elem->pool; |
| 225 | return pool->ctx; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 228 | static const AIOCBInfo thread_pool_aiocb_info = { |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 229 | .aiocb_size = sizeof(ThreadPoolElement), |
Fam Zheng | 3391f5e | 2014-09-11 13:41:12 +0800 | [diff] [blame] | 230 | .cancel_async = thread_pool_cancel, |
| 231 | .get_aio_context = thread_pool_get_aio_context, |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 232 | }; |
| 233 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 234 | BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool, |
Stefan Hajnoczi | c4d9d19 | 2013-03-07 13:41:49 +0100 | [diff] [blame] | 235 | ThreadPoolFunc *func, void *arg, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 236 | BlockCompletionFunc *cb, void *opaque) |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 237 | { |
| 238 | ThreadPoolElement *req; |
| 239 | |
Stefan Hajnoczi | d7331be | 2012-10-31 16:34:37 +0100 | [diff] [blame] | 240 | req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 241 | req->func = func; |
| 242 | req->arg = arg; |
| 243 | req->state = THREAD_QUEUED; |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 244 | req->pool = pool; |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 245 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 246 | QLIST_INSERT_HEAD(&pool->head, req, all); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 247 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 248 | trace_thread_pool_submit(pool, req, arg); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 249 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 250 | qemu_mutex_lock(&pool->lock); |
| 251 | if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) { |
| 252 | spawn_thread(pool); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 253 | } |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 254 | QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs); |
| 255 | qemu_mutex_unlock(&pool->lock); |
| 256 | qemu_sem_post(&pool->sem); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 257 | return &req->common; |
| 258 | } |
| 259 | |
| 260 | typedef struct ThreadPoolCo { |
| 261 | Coroutine *co; |
| 262 | int ret; |
| 263 | } ThreadPoolCo; |
| 264 | |
| 265 | static void thread_pool_co_cb(void *opaque, int ret) |
| 266 | { |
| 267 | ThreadPoolCo *co = opaque; |
| 268 | |
| 269 | co->ret = ret; |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 270 | qemu_coroutine_enter(co->co); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Stefan Hajnoczi | c4d9d19 | 2013-03-07 13:41:49 +0100 | [diff] [blame] | 273 | int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func, |
| 274 | void *arg) |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 275 | { |
| 276 | ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS }; |
| 277 | assert(qemu_in_coroutine()); |
Stefan Hajnoczi | c4d9d19 | 2013-03-07 13:41:49 +0100 | [diff] [blame] | 278 | thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 279 | qemu_coroutine_yield(); |
| 280 | return tpc.ret; |
| 281 | } |
| 282 | |
Stefan Hajnoczi | c4d9d19 | 2013-03-07 13:41:49 +0100 | [diff] [blame] | 283 | void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg) |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 284 | { |
Stefan Hajnoczi | c4d9d19 | 2013-03-07 13:41:49 +0100 | [diff] [blame] | 285 | thread_pool_submit_aio(pool, func, arg, NULL, NULL); |
Paolo Bonzini | d354c7e | 2012-02-23 13:23:34 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 288 | static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx) |
| 289 | { |
| 290 | if (!ctx) { |
| 291 | ctx = qemu_get_aio_context(); |
| 292 | } |
| 293 | |
| 294 | memset(pool, 0, sizeof(*pool)); |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 295 | pool->ctx = ctx; |
Stefan Hajnoczi | c2e50e3 | 2014-07-15 16:44:25 +0200 | [diff] [blame] | 296 | pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool); |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 297 | qemu_mutex_init(&pool->lock); |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 298 | qemu_cond_init(&pool->worker_stopped); |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 299 | qemu_sem_init(&pool->sem, 0); |
| 300 | pool->max_threads = 64; |
| 301 | pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool); |
| 302 | |
| 303 | QLIST_INIT(&pool->head); |
| 304 | QTAILQ_INIT(&pool->request_list); |
Stefan Hajnoczi | b811203 | 2013-03-07 13:41:45 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 307 | ThreadPool *thread_pool_new(AioContext *ctx) |
| 308 | { |
| 309 | ThreadPool *pool = g_new(ThreadPool, 1); |
| 310 | thread_pool_init_one(pool, ctx); |
| 311 | return pool; |
| 312 | } |
| 313 | |
| 314 | void thread_pool_free(ThreadPool *pool) |
| 315 | { |
| 316 | if (!pool) { |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | assert(QLIST_EMPTY(&pool->head)); |
| 321 | |
| 322 | qemu_mutex_lock(&pool->lock); |
| 323 | |
| 324 | /* Stop new threads from spawning */ |
| 325 | qemu_bh_delete(pool->new_thread_bh); |
| 326 | pool->cur_threads -= pool->new_threads; |
| 327 | pool->new_threads = 0; |
| 328 | |
| 329 | /* Wait for worker threads to terminate */ |
| 330 | pool->stopping = true; |
| 331 | while (pool->cur_threads > 0) { |
| 332 | qemu_sem_post(&pool->sem); |
| 333 | qemu_cond_wait(&pool->worker_stopped, &pool->lock); |
| 334 | } |
| 335 | |
| 336 | qemu_mutex_unlock(&pool->lock); |
| 337 | |
Stefan Hajnoczi | c2e50e3 | 2014-07-15 16:44:25 +0200 | [diff] [blame] | 338 | qemu_bh_delete(pool->completion_bh); |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 339 | qemu_sem_destroy(&pool->sem); |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 340 | qemu_cond_destroy(&pool->worker_stopped); |
| 341 | qemu_mutex_destroy(&pool->lock); |
Stefan Hajnoczi | f7311cc | 2013-03-07 13:41:46 +0100 | [diff] [blame] | 342 | g_free(pool); |
| 343 | } |