1 /* 2 threadpool.h - part of lz4 project 3 Copyright (C) Yann Collet 2023 4 GPL v2 License 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License along 17 with this program; if not, write to the Free Software Foundation, Inc., 18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 20 You can contact the author at : 21 - LZ4 source repository : https://github.com/lz4/lz4 22 - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c 23 */ 24 25 #ifndef THREADPOOL_H 26 #define THREADPOOL_H 27 28 #if defined (__cplusplus) 29 extern "C" { 30 #endif 31 32 typedef struct TPool_s TPool; 33 34 /*! TPool_create() : 35 * Create a thread pool with at most @nbThreads. 36 * @nbThreads must be at least 1. 37 * @queueSize is the maximum number of pending jobs before blocking. 38 * @return : TPool* pointer on success, else NULL. 39 */ 40 TPool* TPool_create(int nbThreads, int queueSize); 41 42 /*! TPool_free() : 43 * Free a thread pool returned by TPool_create(). 44 * Waits for the completion of running jobs before freeing resources. 45 */ 46 void TPool_free(TPool* ctx); 47 48 /*! TPool_submitJob() : 49 * Add @job_function(arg) to the thread pool. 50 * @ctx must be valid. 51 * Invocation can block if queue is full. 52 * Note: Ensure @arg's lifetime extends until @job_function completes. 53 * Alternatively, @arg's lifetime must be managed by @job_function. 54 */ 55 void TPool_submitJob(TPool* ctx, void (*job_function)(void*), void* arg); 56 57 /*! TPool_jobsCompleted() : 58 * Blocks until all queued jobs are completed. 59 */ 60 void TPool_jobsCompleted(TPool* ctx); 61 62 63 64 #if defined (__cplusplus) 65 } 66 #endif 67 68 #endif /* THREADPOOL_H */ 69