From ceec06a69714a6514c1f5b778638bfb53aed4902 Mon Sep 17 00:00:00 2001 From: Chris Cannam Date: Mon, 26 Nov 2007 21:51:05 +0000 Subject: [PATCH] * Named conditions; one data condition per channel --- src/StretcherImpl.cpp | 9 +++++---- src/StretcherImpl.h | 8 +++++--- src/StretcherProcess.cpp | 18 +++++++++++++++--- src/Thread.cpp | 29 ++++++++++++++++------------- src/Thread.h | 5 ++++- 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/StretcherImpl.cpp b/src/StretcherImpl.cpp index eecab8b..c021194 100644 --- a/src/StretcherImpl.cpp +++ b/src/StretcherImpl.cpp @@ -67,6 +67,7 @@ RubberBandStretcher::Impl::Impl(RubberBandStretcher *stretcher, m_mode(JustCreated), m_window(0), m_studyFFT(0), + m_spaceAvailable("space"), m_inputDuration(0), m_lastProcessOutputIncrements(16), m_lastProcessPhaseResetDf(16), @@ -962,12 +963,12 @@ RubberBandStretcher::Impl::process(const float *const *input, size_t samples, bo } if (m_threaded) { - m_dataAvailable.signal(); - m_spaceAvailable.lock(); + for (ThreadSet::iterator i = m_threadSet.begin(); + i != m_threadSet.end(); ++i) { + (*i)->signalDataAvailable(); + } if (!allConsumed) { m_spaceAvailable.wait(500); - } else { - m_spaceAvailable.unlock(); } /* } else { diff --git a/src/StretcherImpl.h b/src/StretcherImpl.h index 762d419..e53cb9c 100644 --- a/src/StretcherImpl.h +++ b/src/StretcherImpl.h @@ -137,21 +137,23 @@ protected: Window *m_window; FFT *m_studyFFT; - Condition m_dataAvailable; Condition m_spaceAvailable; class ProcessThread : public Thread { public: - ProcessThread(Impl *s, size_t c) : m_s(s), m_channel(c) { } + ProcessThread(Impl *s, size_t c); void run(); + void signalDataAvailable(); private: Impl *m_s; size_t m_channel; + Condition m_dataAvailable; }; mutable Mutex m_threadSetMutex; - std::set m_threadSet; + typedef std::set ThreadSet; + ThreadSet m_threadSet; size_t m_inputDuration; diff --git a/src/StretcherProcess.cpp b/src/StretcherProcess.cpp index ffcf130..983ae86 100644 --- a/src/StretcherProcess.cpp +++ b/src/StretcherProcess.cpp @@ -30,6 +30,12 @@ using std::endl; namespace RubberBand { +RubberBandStretcher::Impl::ProcessThread::ProcessThread(Impl *s, size_t c) : + m_s(s), + m_channel(c), + m_dataAvailable(std::string("data ") + char('A' + c)) +{ } + void RubberBandStretcher::Impl::ProcessThread::run() { @@ -54,11 +60,11 @@ RubberBandStretcher::Impl::ProcessThread::run() if (any) m_s->m_spaceAvailable.signal(); - m_s->m_dataAvailable.lock(); + m_dataAvailable.lock(); if (!m_s->testInbufReadSpace(m_channel)) { - m_s->m_dataAvailable.wait(500); + m_dataAvailable.wait(); } else { - m_s->m_dataAvailable.unlock(); + m_dataAvailable.unlock(); } } @@ -71,6 +77,12 @@ RubberBandStretcher::Impl::ProcessThread::run() } } +void +RubberBandStretcher::Impl::ProcessThread::signalDataAvailable() +{ + m_dataAvailable.signal(); +} + void RubberBandStretcher::Impl::processChunks(size_t c, bool &any, bool &last) { diff --git a/src/Thread.cpp b/src/Thread.cpp index b985597..c5f0570 100644 --- a/src/Thread.cpp +++ b/src/Thread.cpp @@ -25,6 +25,7 @@ using std::cerr; using std::endl; +using std::string; namespace RubberBand { @@ -164,20 +165,21 @@ Mutex::trylock() } } -Condition::Condition() +Condition::Condition(string name) : + m_name(name) { pthread_mutex_init(&m_mutex, 0); m_locked = false; pthread_cond_init(&m_condition, 0); #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Initialised condition " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Initialised condition " << &m_condition << " \"" << m_name << "\"" << endl; #endif } Condition::~Condition() { #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Destroying condition " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Destroying condition " << &m_condition << " \"" << m_name << "\"" << endl; #endif if (m_locked) pthread_mutex_unlock(&m_mutex); pthread_cond_destroy(&m_condition); @@ -189,17 +191,17 @@ Condition::lock() { if (m_locked) { #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Already locked " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Already locked " << &m_condition << " \"" << m_name << "\"" << endl; #endif return; } #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Want to lock " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Want to lock " << &m_condition << " \"" << m_name << "\"" << endl; #endif pthread_mutex_lock(&m_mutex); m_locked = true; #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Locked " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Locked " << &m_condition << " \"" << m_name << "\"" << endl; #endif } @@ -208,12 +210,12 @@ Condition::unlock() { if (!m_locked) { #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Not locked " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Not locked " << &m_condition << " \"" << m_name << "\"" << endl; #endif return; } #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Unlocking " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Unlocking " << &m_condition << " \"" << m_name << "\"" << endl; #endif m_locked = false; pthread_mutex_unlock(&m_mutex); @@ -222,11 +224,12 @@ Condition::unlock() void Condition::wait(int us) { - lock(); + if (!m_locked) lock(); + if (us == 0) { #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Waiting on " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Waiting on " << &m_condition << " \"" << m_name << "\"" << endl; #endif pthread_cond_wait(&m_condition, &m_mutex); @@ -246,13 +249,13 @@ Condition::wait(int us) timeout.tv_nsec = now.tv_usec * 1000; #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Timed waiting on " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Timed waiting on " << &m_condition << " \"" << m_name << "\"" << endl; #endif pthread_cond_timedwait(&m_condition, &m_mutex, &timeout); } #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Wait done on " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Wait done on " << &m_condition << " \"" << m_name << "\"" << endl; #endif pthread_mutex_unlock(&m_mutex); m_locked = false; @@ -262,7 +265,7 @@ void Condition::signal() { #ifdef DEBUG_CONDITION - cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Signalling " << &m_condition << endl; + cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Signalling " << &m_condition << " \"" << m_name << "\"" << endl; #endif pthread_cond_signal(&m_condition); } diff --git a/src/Thread.h b/src/Thread.h index 282992d..d419d85 100644 --- a/src/Thread.h +++ b/src/Thread.h @@ -17,6 +17,8 @@ #include +#include + namespace RubberBand { @@ -72,7 +74,7 @@ private: class Condition { public: - Condition(); + Condition(std::string name); ~Condition(); void lock(); @@ -84,6 +86,7 @@ private: pthread_mutex_t m_mutex; bool m_locked; pthread_cond_t m_condition; + std::string m_name; }; }