Mercurial > repos > rliterman > csp2
annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/mash/ThreadPool.h @ 69:33d812a61356
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 17:55:14 -0400 |
parents | |
children |
rev | line source |
---|---|
jpayne@69 | 1 // Copyright © 2015, Battelle National Biodefense Institute (BNBI); |
jpayne@69 | 2 // all rights reserved. Authored by: Brian Ondov, Todd Treangen, |
jpayne@69 | 3 // Sergey Koren, and Adam Phillippy |
jpayne@69 | 4 // |
jpayne@69 | 5 // See the LICENSE.txt file included with this software for license information. |
jpayne@69 | 6 |
jpayne@69 | 7 #ifndef ThreadPool_h |
jpayne@69 | 8 #define ThreadPool_h |
jpayne@69 | 9 |
jpayne@69 | 10 #include <pthread.h> |
jpayne@69 | 11 #include <queue> |
jpayne@69 | 12 |
jpayne@69 | 13 template <class TypeInput, class TypeOutput> |
jpayne@69 | 14 class ThreadPool |
jpayne@69 | 15 { |
jpayne@69 | 16 public: |
jpayne@69 | 17 |
jpayne@69 | 18 ThreadPool(TypeOutput * (* functionNew)(TypeInput *), unsigned int threadCountNew); |
jpayne@69 | 19 ~ThreadPool(); |
jpayne@69 | 20 |
jpayne@69 | 21 bool outputAvailable() const; |
jpayne@69 | 22 TypeOutput * popOutputWhenAvailable(); // output must be deleted by calling function |
jpayne@69 | 23 bool running() const; |
jpayne@69 | 24 void runWhenThreadAvailable(TypeInput * input); // thread deletes input when finished |
jpayne@69 | 25 void runWhenThreadAvailable(TypeInput * input, TypeOutput * (* functionNew)(TypeInput *)); // thread deletes input when finished |
jpayne@69 | 26 |
jpayne@69 | 27 private: |
jpayne@69 | 28 |
jpayne@69 | 29 struct OutputQueueNode |
jpayne@69 | 30 { |
jpayne@69 | 31 // used to preserve input order when outputting |
jpayne@69 | 32 |
jpayne@69 | 33 OutputQueueNode * prev; |
jpayne@69 | 34 OutputQueueNode * next; |
jpayne@69 | 35 |
jpayne@69 | 36 TypeOutput * output; |
jpayne@69 | 37 bool ready; |
jpayne@69 | 38 }; |
jpayne@69 | 39 |
jpayne@69 | 40 unsigned int threadCount; |
jpayne@69 | 41 |
jpayne@69 | 42 pthread_t * threads; |
jpayne@69 | 43 |
jpayne@69 | 44 static void * thread(void *); |
jpayne@69 | 45 |
jpayne@69 | 46 TypeOutput * (* function)(TypeInput *); |
jpayne@69 | 47 TypeInput * inputCurrent; |
jpayne@69 | 48 OutputQueueNode * outputQueueNodeCurrent; |
jpayne@69 | 49 |
jpayne@69 | 50 pthread_mutex_t * mutexInput; |
jpayne@69 | 51 pthread_mutex_t * mutexOutput; |
jpayne@69 | 52 |
jpayne@69 | 53 pthread_cond_t * condInput; |
jpayne@69 | 54 pthread_cond_t * condOutput; |
jpayne@69 | 55 |
jpayne@69 | 56 OutputQueueNode * outputQueueHead; |
jpayne@69 | 57 OutputQueueNode * outputQueueTail; |
jpayne@69 | 58 |
jpayne@69 | 59 bool finished; |
jpayne@69 | 60 friend void * thread(void *); |
jpayne@69 | 61 }; |
jpayne@69 | 62 |
jpayne@69 | 63 |
jpayne@69 | 64 #include "ThreadPool.hxx" |
jpayne@69 | 65 |
jpayne@69 | 66 #endif |