Permalink
Browse files

getting rid of heap allocation in ThreadSafe constructor

  • Loading branch information...
1 parent f1c2576 commit bccffc0d34ee87b85fb50dfa863cbcd2f0593c4d @BenLangmead committed Apr 16, 2017
Showing with 60 additions and 30 deletions.
  1. +32 −11 pat.cpp
  2. +15 −4 pat.h
  3. +13 −15 threading.h
View
43 pat.cpp
@@ -226,17 +226,12 @@ pair<bool, int> DualPatternComposer::nextBatch(PerThreadReadBuf& pt) {
* Returns pair<bool, int> where bool indicates whether we're
* completely done, and int indicates how many reads were read.
*/
-pair<bool, int> CFilePatternSource::nextBatch(
+pair<bool, int> 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<bool, int> CFilePatternSource::nextBatch(
return make_pair(done, nread);
}
+pair<bool, int> 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<bool, int> VectorPatternSource::nextBatch(
+pair<bool, int> VectorPatternSource::nextBatchImpl(
PerThreadReadBuf& pt,
- bool batch_a,
- bool lock)
+ bool batch_a)
{
- ThreadSafe ts(&mutex, lock);
pt.setReadId(cur_);
vector<Read>& readbuf = batch_a ? pt.bufa_ : pt.bufb_;
size_t readi = 0;
@@ -384,6 +392,19 @@ pair<bool, int> VectorPatternSource::nextBatch(
return make_pair(cur_ == bufs_.size(), readi);
}
+pair<bool, int> 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.
*/
View
19 pat.h
@@ -503,10 +503,10 @@ class VectorPatternSource : public TrimmingPatternSource {
public:
VectorPatternSource(
const vector<string>& 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<bool, int> 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<bool, int> nextBatchImpl(
+ PerThreadReadBuf& pt,
+ bool batch_a);
+
};
/**
View
@@ -5,6 +5,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
+#include <cassert>
#ifdef WITH_TBB
# include <tbb/mutex.h>
@@ -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
};

0 comments on commit bccffc0

Please sign in to comment.