jpayne@69: // Copyright © 2015, Battelle National Biodefense Institute (BNBI); jpayne@69: // all rights reserved. Authored by: Brian Ondov, Todd Treangen, jpayne@69: // Sergey Koren, and Adam Phillippy jpayne@69: // jpayne@69: // See the LICENSE.txt file included with this software for license information. jpayne@69: jpayne@69: #ifndef ThreadPool_h jpayne@69: #define ThreadPool_h jpayne@69: jpayne@69: #include jpayne@69: #include jpayne@69: jpayne@69: template jpayne@69: class ThreadPool jpayne@69: { jpayne@69: public: jpayne@69: jpayne@69: ThreadPool(TypeOutput * (* functionNew)(TypeInput *), unsigned int threadCountNew); jpayne@69: ~ThreadPool(); jpayne@69: jpayne@69: bool outputAvailable() const; jpayne@69: TypeOutput * popOutputWhenAvailable(); // output must be deleted by calling function jpayne@69: bool running() const; jpayne@69: void runWhenThreadAvailable(TypeInput * input); // thread deletes input when finished jpayne@69: void runWhenThreadAvailable(TypeInput * input, TypeOutput * (* functionNew)(TypeInput *)); // thread deletes input when finished jpayne@69: jpayne@69: private: jpayne@69: jpayne@69: struct OutputQueueNode jpayne@69: { jpayne@69: // used to preserve input order when outputting jpayne@69: jpayne@69: OutputQueueNode * prev; jpayne@69: OutputQueueNode * next; jpayne@69: jpayne@69: TypeOutput * output; jpayne@69: bool ready; jpayne@69: }; jpayne@69: jpayne@69: unsigned int threadCount; jpayne@69: jpayne@69: pthread_t * threads; jpayne@69: jpayne@69: static void * thread(void *); jpayne@69: jpayne@69: TypeOutput * (* function)(TypeInput *); jpayne@69: TypeInput * inputCurrent; jpayne@69: OutputQueueNode * outputQueueNodeCurrent; jpayne@69: jpayne@69: pthread_mutex_t * mutexInput; jpayne@69: pthread_mutex_t * mutexOutput; jpayne@69: jpayne@69: pthread_cond_t * condInput; jpayne@69: pthread_cond_t * condOutput; jpayne@69: jpayne@69: OutputQueueNode * outputQueueHead; jpayne@69: OutputQueueNode * outputQueueTail; jpayne@69: jpayne@69: bool finished; jpayne@69: friend void * thread(void *); jpayne@69: }; jpayne@69: jpayne@69: jpayne@69: #include "ThreadPool.hxx" jpayne@69: jpayne@69: #endif