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