* 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_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 {

View File

@@ -137,21 +137,23 @@ protected:
Window<float> *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<ProcessThread *> m_threadSet;
typedef std::set<ProcessThread *> ThreadSet;
ThreadSet m_threadSet;
size_t m_inputDuration;

View File

@@ -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)
{

View File

@@ -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);
}

View File

@@ -17,6 +17,8 @@
#include <pthread.h>
#include <string>
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;
};
}