diff --git a/pat.cpp b/pat.cpp index 11f3f5a..f939608 100644 --- a/pat.cpp +++ b/pat.cpp @@ -226,17 +226,12 @@ pair DualPatternComposer::nextBatch(PerThreadReadBuf& pt) { * Returns pair where bool indicates whether we're * completely done, and int indicates how many reads were read. */ -pair CFilePatternSource::nextBatch( +pair CFilePatternSource::nextBatchImpl( PerThreadReadBuf& pt, - bool batch_a, - bool lock) + bool batch_a) { bool done = false; int nread = 0; - - // synchronization at this level because both reading and manipulation of - // current file pointer have to be protected - ThreadSafe ts(&mutex, lock); pt.setReadId(readCnt_); while(true) { // loop that moves on to next file when needed do { @@ -259,6 +254,21 @@ pair CFilePatternSource::nextBatch( return make_pair(done, nread); } +pair CFilePatternSource::nextBatch( + PerThreadReadBuf& pt, + bool batch_a, + bool lock) +{ + if(lock) { + // synchronization at this level because both reading and manipulation of + // current file pointer have to be protected + ThreadSafe ts(&mutex); + return nextBatchImpl(pt, batch_a); + } else { + return nextBatchImpl(pt, batch_a); + } +} + /** * Open the next file in the list of input files. */ @@ -366,12 +376,10 @@ VectorPatternSource::VectorPatternSource( * in the contsructor. This essentially modifies the pt as though we read * in some number of patterns. */ -pair VectorPatternSource::nextBatch( +pair VectorPatternSource::nextBatchImpl( PerThreadReadBuf& pt, - bool batch_a, - bool lock) + bool batch_a) { - ThreadSafe ts(&mutex, lock); pt.setReadId(cur_); vector& readbuf = batch_a ? pt.bufa_ : pt.bufb_; size_t readi = 0; @@ -384,6 +392,19 @@ pair VectorPatternSource::nextBatch( return make_pair(cur_ == bufs_.size(), readi); } +pair VectorPatternSource::nextBatch( + PerThreadReadBuf& pt, + bool batch_a, + bool lock) +{ + if(lock) { + ThreadSafe ts(&mutex); + return nextBatchImpl(pt, batch_a); + } else { + return nextBatchImpl(pt, batch_a); + } +} + /** * Finishes parsing outside the critical section. */ diff --git a/pat.h b/pat.h index 6c9420a..01b6b15 100644 --- a/pat.h +++ b/pat.h @@ -503,10 +503,10 @@ class VectorPatternSource : public TrimmingPatternSource { public: VectorPatternSource( const vector& v, - bool color, - const char *dumpfile = NULL, - int trim3 = 0, - int trim5 = 0); + bool color, + const char *dumpfile = NULL, + int trim3 = 0, + int trim5 = 0); virtual ~VectorPatternSource() { } @@ -537,6 +537,10 @@ class VectorPatternSource : public TrimmingPatternSource { private: + pair nextBatchImpl( + PerThreadReadBuf& pt, + bool batch_a); + bool color_; // colorspace? size_t cur_; // index for first read of next batch bool paired_; // whether reads are paired @@ -648,6 +652,13 @@ class CFilePatternSource : public TrimmingPatternSource { bool first_; char buf_[64*1024]; /// file buffer for sequences char qbuf_[64*1024]; /// file buffer for qualities + +private: + + pair nextBatchImpl( + PerThreadReadBuf& pt, + bool batch_a); + }; /** diff --git a/threading.h b/threading.h index 30ebd2d..a5cf944 100644 --- a/threading.h +++ b/threading.h @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef WITH_TBB # include @@ -54,35 +55,32 @@ struct thread_tracking_pair { class ThreadSafe { public: - ThreadSafe() : ptr_mutex(NULL) { } - - ThreadSafe(MUTEX_T* ptr_mutex, bool locked = true) : ptr_mutex(NULL) { - if(locked) { + ThreadSafe(MUTEX_T* ptr_mutex) : #if WITH_TBB && NO_SPINLOCK && WITH_QUEUELOCK - //have to use the heap as we can't copy - //the scoped lock - this->ptr_mutex = new MUTEX_T::scoped_lock(*ptr_mutex); + mutex_(*ptr_mutex) #else - this->ptr_mutex = ptr_mutex; - ptr_mutex->lock(); + ptr_mutex_(ptr_mutex) +#endif + { +#if WITH_TBB && NO_SPINLOCK && WITH_QUEUELOCK +#else + assert(ptr_mutex_ != NULL); + ptr_mutex_->lock(); #endif - } } ~ThreadSafe() { - if (ptr_mutex != NULL) #if WITH_TBB && NO_SPINLOCK && WITH_QUEUELOCK - delete ptr_mutex; #else - ptr_mutex->unlock(); + ptr_mutex_->unlock(); #endif } private: #if WITH_TBB && NO_SPINLOCK && WITH_QUEUELOCK - MUTEX_T::scoped_lock* ptr_mutex; + MUTEX_T::scoped_lock mutex_; #else - MUTEX_T *ptr_mutex; + MUTEX_T *ptr_mutex_; #endif };