1 module dhtslib.htslib.thread_pool; 2 3 extern(C): 4 // @file htslib/thread_pool.h 5 // Thread pool for multi-threading applications. 6 /* 7 Copyright (c) 2013-2017 Genome Research Ltd. 8 9 Author: James Bonfield <jkb@sanger.ac.uk> 10 11 Permission is hereby granted, free of charge, to any person obtaining a copy 12 of this software and associated documentation files (the "Software"), to deal 13 in the Software without restriction, including without limitation the rights 14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 copies of the Software, and to permit persons to whom the Software is 16 furnished to do so, subject to the following conditions: 17 18 The above copyright notice and this permission notice shall be included in 19 all copies or substantial portions of the Software. 20 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 24 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 DEALINGS IN THE SOFTWARE. */ 28 29 /* 30 * This file implements a thread pool for multi-threading applications. It 31 * consists of two distinct interfaces: thread pools and thread process 32 * queues (a queue of both jobs to-do and of the results of completed jobs). 33 * Do not confuse "process" here with a unix PID; rather it is analogous to a 34 * program reading a stream of data blocks, processing them in some manner, 35 * and outputting a stream of new data blocks. 36 * 37 * The pool of threads is given a function pointer and void* data to pass in. 38 * This means the pool can run jobs of multiple types, albeit first come 39 * first served with no job scheduling except to pick tasks for the 40 * processes that have room to store the result. 41 * 42 * Upon completion, the return value from the function pointer is 43 * added to back to the process result queue if required. We may have 44 * multiple "processes" in use for the one pool. 45 * 46 * To see example usage, please look at the #ifdef TEST_MAIN code in 47 * thread_pool.c. 48 */ 49 50 /*----------------------------------------------------------------------------- 51 * Opaque data types. 52 * 53 * Actual definitions are in thread_pool_internal.h, but these should only 54 * be used by thread_pool.c itself. 55 */ 56 57 /** 58 * An hts_tpool_process implements a queue of input jobs to process and a 59 * queue of resulting output post-processing. Internally it consists of two 60 * buffered queues, analogous to the pipes in a unix pipeline: 61 * ...input | process | output... 62 * 63 * Both input and output queues have size limits to prevent either queue from 64 * growing too large and serial numbers to ensure sequential consumption of 65 * the output. 66 * 67 * The thread pool may have many heterogeneous tasks, each using its own 68 * process mixed into the same thread pool. 69 */ 70 struct hts_tpool_process; 71 72 /** 73 * The single pool structure itself. 74 * 75 * This knows nothing about the nature of the jobs or where their output is 76 * going, but it maintains a list of process-queues associated with this pool 77 * from which the jobs are taken. 78 */ 79 struct hts_tpool; 80 81 /** 82 * An output, after job has executed. 83 */ 84 struct hts_tpool_result; 85 86 87 /*----------------------------------------------------------------------------- 88 * Thread pool external functions 89 */ 90 91 92 /** 93 * Creates a worker pool with n worker threads. 94 * 95 * Returns pool pointer on success; 96 * NULL on failure 97 */ 98 hts_tpool *hts_tpool_init(int n); 99 100 101 /** 102 * Returns the number of requested threads for a pool. 103 */ 104 int hts_tpool_size(hts_tpool *p); 105 106 107 /** 108 * Adds an item to the work pool. 109 * 110 * FIXME: permit q to be NULL, indicating a global/default pool held by 111 * the thread pool itself? This pool would be for jobs that have no 112 * output, so fire and forget only with.. 113 * 114 * Returns 0 on success 115 * -1 on failure 116 */ 117 118 // FIXME: should this drop the hts_tpool*p argument? It's just q->p 119 //int hts_tpool_dispatch(hts_tpool *p, hts_tpool_process *q, 120 // void *(*func)(void *arg), void *arg); 121 int hts_tpool_dispatch(hts_tpool *p, hts_tpool_process *q, 122 void* function(void* arg), void *arg); 123 /// ditto 124 //int hts_tpool_dispatch2(hts_tpool *p, hts_tpool_process *q, 125 // void *(*func)(void *arg), void *arg, int nonblock); 126 int hts_tpool_dispatch2(hts_tpool *p, hts_tpool_process *q, 127 void* function(void* arg), void *arg, int nonblock); 128 129 /** 130 * Wakes up a single thread stuck in dispatch and make it return with 131 * errno EAGAIN. 132 */ 133 void hts_tpool_wake_dispatch(hts_tpool_process *q); 134 135 /** 136 * Flushes the process-queue, but doesn't exit. This simply drains the queue 137 * and ensures all worker threads have finished their current tasks 138 * associated with this process. 139 * 140 * NOT: This does not mean the worker threads are not executing jobs in 141 * another process-queue. 142 * 143 * Returns 0 on success; 144 * -1 on failure 145 */ 146 int hts_tpool_process_flush(hts_tpool_process *q); 147 148 /** 149 * Resets a process to the initial state. 150 * 151 * This removes any queued up input jobs, disables any notification of 152 * new results/output, flushes what is left and then discards any 153 * queued output. Anything consumer stuck in a wait on results to 154 * appear should stay stuck and will only wake up when new data is 155 * pushed through the queue. 156 * 157 * Returns 0 on success; 158 * -1 on failure 159 */ 160 int hts_tpool_process_reset(hts_tpool_process *q, int free_results); 161 162 /** Returns the process queue size */ 163 int hts_tpool_process_qsize(hts_tpool_process *q); 164 165 166 /** 167 * Destroys a thread pool. The threads are joined into the main 168 * thread so they will finish their current work load. 169 */ 170 void hts_tpool_destroy(hts_tpool *p); 171 172 /** 173 * Destroys a thread pool without waiting on jobs to complete. 174 * Use hts_tpool_kill(p) to quickly exit after a fatal error. 175 */ 176 void hts_tpool_kill(hts_tpool *p); 177 178 /** 179 * Pulls the next item off the process result queue. The caller should free 180 * it (and any internals as appropriate) after use. This doesn't wait for a 181 * result to be present. 182 * 183 * Results will be returned in strict order. 184 * 185 * Returns hts_tpool_result pointer if a result is ready. 186 * NULL if not. 187 */ 188 hts_tpool_result *hts_tpool_next_result(hts_tpool_process *q); 189 190 /** 191 * Pulls the next item off the process result queue. The caller should free 192 * it (and any internals as appropriate) after use. This will wait for 193 * a result to be present if none are currently available. 194 * 195 * Results will be returned in strict order. 196 * 197 * Returns hts_tpool_result pointer if a result is ready. 198 * NULL on error or during shutdown. 199 */ 200 hts_tpool_result *hts_tpool_next_result_wait(hts_tpool_process *q); 201 202 /** 203 * Frees a result 'r' and if free_data is true also frees 204 * the internal r->data result too. 205 */ 206 void hts_tpool_delete_result(hts_tpool_result *r, int free_data); 207 208 /** 209 * Returns the data portion of a hts_tpool_result, corresponding 210 * to the actual "result" itself. 211 */ 212 void *hts_tpool_result_data(hts_tpool_result *r); 213 214 /** 215 * Initialises a thread process-queue. 216 * 217 * In_only, if true, indicates that the process generates does not need to 218 * hold any output. Otherwise an output queue is used to store the results 219 * of processing each input job. 220 * 221 * Results hts_tpool_process pointer on success; 222 * NULL on failure 223 */ 224 hts_tpool_process *hts_tpool_process_init(hts_tpool *p, int qsize, int in_only); 225 226 227 /**Deallocates memory for a thread process-queue. 228 * Must be called before the thread pool is destroyed. 229 */ 230 void hts_tpool_process_destroy(hts_tpool_process *q); 231 232 /* 233 * Flushes the thread pool, but doesn't exit. This simply drains the 234 * process-queue and ensures all worker threads have finished their current 235 * task if associated with this process. 236 * 237 * Returns 0 on success; 238 * -1 on failure 239 */ 240 // Duplicate prototype with slightly different doc 241 //int hts_tpool_process_flush(hts_tpool_process *q); 242 243 /** 244 * Returns true if there are no items in the process results queue and 245 * also none still pending. 246 */ 247 int hts_tpool_process_empty(hts_tpool_process *q); 248 249 /** 250 * Returns the number of completed jobs in the process results queue. 251 */ 252 int hts_tpool_process_len(hts_tpool_process *q); 253 254 /** 255 * Returns the number of completed jobs in the process results queue plus the 256 * number running and queued up to run. 257 */ 258 int hts_tpool_process_sz(hts_tpool_process *q); 259 260 /** 261 * Shutdown a process. 262 * 263 * This sets the shutdown flag and wakes any threads waiting on process 264 * condition variables. 265 */ 266 void hts_tpool_process_shutdown(hts_tpool_process *q); 267 268 /** 269 * Attach and detach a thread process-queue with / from the thread pool 270 * scheduler. 271 * 272 * We need to do attach after making a thread process, but may also wish 273 * to temporarily detach if we wish to stop running jobs on a specific 274 * process while permitting other process to continue. 275 */ 276 void hts_tpool_process_attach(hts_tpool *p, hts_tpool_process *q); 277 /// ditto 278 void hts_tpool_process_detach(hts_tpool *p, hts_tpool_process *q); 279 280 /** 281 * Increment and decrement the reference count in a process-queue. 282 * If the queue is being driven from two external (non thread-pool) 283 * threads, eg "main" and a "reader", this permits each end to 284 * decrement its use of the process-queue independently. 285 */ 286 void hts_tpool_process_ref_incr(hts_tpool_process *q); 287 /// ditto 288 void hts_tpool_process_ref_decr(hts_tpool_process *q);