* Named conditions; one data condition per channel

This commit is contained in:
Chris Cannam
2007-11-26 21:51:05 +00:00
parent d58531e2ca
commit ceec06a697
5 changed files with 45 additions and 24 deletions

View File

@@ -67,6 +67,7 @@ RubberBandStretcher::Impl::Impl(RubberBandStretcher *stretcher,
m_mode(JustCreated), m_mode(JustCreated),
m_window(0), m_window(0),
m_studyFFT(0), m_studyFFT(0),
m_spaceAvailable("space"),
m_inputDuration(0), m_inputDuration(0),
m_lastProcessOutputIncrements(16), m_lastProcessOutputIncrements(16),
m_lastProcessPhaseResetDf(16), m_lastProcessPhaseResetDf(16),
@@ -962,12 +963,12 @@ RubberBandStretcher::Impl::process(const float *const *input, size_t samples, bo
} }
if (m_threaded) { if (m_threaded) {
m_dataAvailable.signal(); for (ThreadSet::iterator i = m_threadSet.begin();
m_spaceAvailable.lock(); i != m_threadSet.end(); ++i) {
(*i)->signalDataAvailable();
}
if (!allConsumed) { if (!allConsumed) {
m_spaceAvailable.wait(500); m_spaceAvailable.wait(500);
} else {
m_spaceAvailable.unlock();
} }
/* /*
} else { } else {

View File

@@ -137,21 +137,23 @@ protected:
Window<float> *m_window; Window<float> *m_window;
FFT *m_studyFFT; FFT *m_studyFFT;
Condition m_dataAvailable;
Condition m_spaceAvailable; Condition m_spaceAvailable;
class ProcessThread : public Thread class ProcessThread : public Thread
{ {
public: public:
ProcessThread(Impl *s, size_t c) : m_s(s), m_channel(c) { } ProcessThread(Impl *s, size_t c);
void run(); void run();
void signalDataAvailable();
private: private:
Impl *m_s; Impl *m_s;
size_t m_channel; size_t m_channel;
Condition m_dataAvailable;
}; };
mutable Mutex m_threadSetMutex; mutable Mutex m_threadSetMutex;
std::set<ProcessThread *> m_threadSet; typedef std::set<ProcessThread *> ThreadSet;
ThreadSet m_threadSet;
size_t m_inputDuration; size_t m_inputDuration;

View File

@@ -30,6 +30,12 @@ using std::endl;
namespace RubberBand { 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 void
RubberBandStretcher::Impl::ProcessThread::run() RubberBandStretcher::Impl::ProcessThread::run()
{ {
@@ -54,11 +60,11 @@ RubberBandStretcher::Impl::ProcessThread::run()
if (any) m_s->m_spaceAvailable.signal(); if (any) m_s->m_spaceAvailable.signal();
m_s->m_dataAvailable.lock(); m_dataAvailable.lock();
if (!m_s->testInbufReadSpace(m_channel)) { if (!m_s->testInbufReadSpace(m_channel)) {
m_s->m_dataAvailable.wait(500); m_dataAvailable.wait();
} else { } 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 void
RubberBandStretcher::Impl::processChunks(size_t c, bool &any, bool &last) RubberBandStretcher::Impl::processChunks(size_t c, bool &any, bool &last)
{ {

View File

@@ -25,6 +25,7 @@
using std::cerr; using std::cerr;
using std::endl; using std::endl;
using std::string;
namespace RubberBand namespace RubberBand
{ {
@@ -164,20 +165,21 @@ Mutex::trylock()
} }
} }
Condition::Condition() Condition::Condition(string name) :
m_name(name)
{ {
pthread_mutex_init(&m_mutex, 0); pthread_mutex_init(&m_mutex, 0);
m_locked = false; m_locked = false;
pthread_cond_init(&m_condition, 0); pthread_cond_init(&m_condition, 0);
#ifdef DEBUG_CONDITION #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 #endif
} }
Condition::~Condition() Condition::~Condition()
{ {
#ifdef DEBUG_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 #endif
if (m_locked) pthread_mutex_unlock(&m_mutex); if (m_locked) pthread_mutex_unlock(&m_mutex);
pthread_cond_destroy(&m_condition); pthread_cond_destroy(&m_condition);
@@ -189,17 +191,17 @@ Condition::lock()
{ {
if (m_locked) { if (m_locked) {
#ifdef DEBUG_CONDITION #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 #endif
return; return;
} }
#ifdef DEBUG_CONDITION #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 #endif
pthread_mutex_lock(&m_mutex); pthread_mutex_lock(&m_mutex);
m_locked = true; m_locked = true;
#ifdef DEBUG_CONDITION #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 #endif
} }
@@ -208,12 +210,12 @@ Condition::unlock()
{ {
if (!m_locked) { if (!m_locked) {
#ifdef DEBUG_CONDITION #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 #endif
return; return;
} }
#ifdef DEBUG_CONDITION #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 #endif
m_locked = false; m_locked = false;
pthread_mutex_unlock(&m_mutex); pthread_mutex_unlock(&m_mutex);
@@ -222,11 +224,12 @@ Condition::unlock()
void void
Condition::wait(int us) Condition::wait(int us)
{ {
lock(); if (!m_locked) lock();
if (us == 0) { if (us == 0) {
#ifdef DEBUG_CONDITION #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 #endif
pthread_cond_wait(&m_condition, &m_mutex); pthread_cond_wait(&m_condition, &m_mutex);
@@ -246,13 +249,13 @@ Condition::wait(int us)
timeout.tv_nsec = now.tv_usec * 1000; timeout.tv_nsec = now.tv_usec * 1000;
#ifdef DEBUG_CONDITION #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 #endif
pthread_cond_timedwait(&m_condition, &m_mutex, &timeout); pthread_cond_timedwait(&m_condition, &m_mutex, &timeout);
} }
#ifdef DEBUG_CONDITION #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 #endif
pthread_mutex_unlock(&m_mutex); pthread_mutex_unlock(&m_mutex);
m_locked = false; m_locked = false;
@@ -262,7 +265,7 @@ void
Condition::signal() Condition::signal()
{ {
#ifdef DEBUG_CONDITION #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 #endif
pthread_cond_signal(&m_condition); pthread_cond_signal(&m_condition);
} }

View File

@@ -17,6 +17,8 @@
#include <pthread.h> #include <pthread.h>
#include <string>
namespace RubberBand namespace RubberBand
{ {
@@ -72,7 +74,7 @@ private:
class Condition class Condition
{ {
public: public:
Condition(); Condition(std::string name);
~Condition(); ~Condition();
void lock(); void lock();
@@ -84,6 +86,7 @@ private:
pthread_mutex_t m_mutex; pthread_mutex_t m_mutex;
bool m_locked; bool m_locked;
pthread_cond_t m_condition; pthread_cond_t m_condition;
std::string m_name;
}; };
} }