| 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 | */ |
| 17 | |
| 18 | #ifndef QEMU_THREAD_POOL_H |
| 19 | #define QEMU_THREAD_POOL_H |
| 20 | |
| 21 | #include "qemu/aiocb.h" |
| 22 | #include "qemu/aio.h" |
| 23 | |
| 24 | #define THREAD_POOL_MAX_THREADS_DEFAULT 64 |
| 25 | |
| 26 | typedef int ThreadPoolFunc(void *opaque); |
| 27 | |
| 28 | typedef struct ThreadPoolAio ThreadPoolAio; |
| 29 | |
| 30 | ThreadPoolAio *thread_pool_new_aio(struct AioContext *ctx); |
| 31 | void thread_pool_free_aio(ThreadPoolAio *pool); |
| 32 | |
| 33 | /* |
| 34 | * thread_pool_submit_{aio,co} API: submit I/O requests in the thread's |
| 35 | * current AioContext. |
| 36 | */ |
| 37 | BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg, |
| 38 | BlockCompletionFunc *cb, void *opaque); |
| 39 | int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg); |
| 40 | void thread_pool_update_params(ThreadPoolAio *pool, struct AioContext *ctx); |
| 41 | |
| 42 | /* ------------------------------------------- */ |
| 43 | /* Generic thread pool types and methods below */ |
| 44 | typedef struct ThreadPool ThreadPool; |
| 45 | |
| 46 | /* Create a new thread pool. Never returns NULL. */ |
| 47 | ThreadPool *thread_pool_new(void); |
| 48 | |
| 49 | /* |
| 50 | * Free the thread pool. |
| 51 | * Waits for all the previously submitted work to complete before performing |
| 52 | * the actual freeing operation. |
| 53 | */ |
| 54 | void thread_pool_free(ThreadPool *pool); |
| 55 | |
| 56 | /* |
| 57 | * Submit a new work (task) for the pool. |
| 58 | * |
| 59 | * @opaque_destroy is an optional GDestroyNotify for the @opaque argument |
| 60 | * to the work function at @func. |
| 61 | */ |
| 62 | void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, |
| 63 | void *opaque, GDestroyNotify opaque_destroy); |
| 64 | |
| 65 | /* |
| 66 | * Submit a new work (task) for the pool, making sure it starts getting |
| 67 | * processed immediately, launching a new thread for it if necessary. |
| 68 | * |
| 69 | * @opaque_destroy is an optional GDestroyNotify for the @opaque argument |
| 70 | * to the work function at @func. |
| 71 | */ |
| 72 | void thread_pool_submit_immediate(ThreadPool *pool, ThreadPoolFunc *func, |
| 73 | void *opaque, GDestroyNotify opaque_destroy); |
| 74 | |
| 75 | /* |
| 76 | * Wait for all previously submitted work to complete before returning. |
| 77 | * |
| 78 | * Can be used as a barrier between two sets of tasks executed on a thread |
| 79 | * pool without destroying it or in a performance sensitive path where the |
| 80 | * caller just wants to wait for all tasks to complete while deferring the |
| 81 | * pool free operation for later, less performance sensitive time. |
| 82 | */ |
| 83 | void thread_pool_wait(ThreadPool *pool); |
| 84 | |
| 85 | /* Set the maximum number of threads in the pool. */ |
| 86 | bool thread_pool_set_max_threads(ThreadPool *pool, int max_threads); |
| 87 | |
| 88 | /* |
| 89 | * Adjust the maximum number of threads in the pool to give each task its |
| 90 | * own thread (exactly one thread per task). |
| 91 | */ |
| 92 | bool thread_pool_adjust_max_threads_to_work(ThreadPool *pool); |
| 93 | |
| 94 | #endif |