Skip to content

Commit

Permalink
Cleanup DeviceReadBuffer start()/stop() handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-kristjansson committed Aug 5, 2011
1 parent 89ad944 commit 1c2b33e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
47 changes: 21 additions & 26 deletions mythtv/libs/libmythtv/DeviceReadBuffer.cpp
Expand Up @@ -23,7 +23,7 @@ DeviceReadBuffer::DeviceReadBuffer(DeviceReaderCB *cb, bool use_poll)
readerCB(cb),

// Data for managing the device ringbuffer
dorun(false), running(false),
dorun(false),
eof(false), error(false),
request_pause(false), paused(false),
using_poll(use_poll), max_poll_wait(2500 /*ms*/),
Expand Down Expand Up @@ -58,10 +58,12 @@ DeviceReadBuffer::DeviceReadBuffer(DeviceReaderCB *cb, bool use_poll)

DeviceReadBuffer::~DeviceReadBuffer()
{
Stop();
if (buffer)
{
delete[] buffer;
if (isRunning() || dorun)
Stop();
buffer = NULL;
}
}

bool DeviceReadBuffer::Setup(const QString &streamName, int streamfd,
Expand Down Expand Up @@ -125,28 +127,25 @@ void DeviceReadBuffer::Start(void)
{
LOG(VB_RECORD, LOG_INFO, LOC + "Start() -- begin");

QMutexLocker locker(&lock);
if (isRunning() || dorun)
{
{
QMutexLocker locker(&lock);
dorun = false;
}
dorun = false;
locker.unlock();
WakePoll();
wait();
locker.relock();
}

{
QMutexLocker locker(&lock);
error = false;
eof = false;
}
dorun = true;
error = false;
eof = false;

start();

LOG(VB_RECORD, LOG_INFO, LOC + "Start() -- middle");

QMutexLocker locker(&lock);
while (dorun && !running)
while (dorun && !isRunning())
runWait.wait(locker.mutex(), 100);

LOG(VB_RECORD, LOG_INFO, LOC + "Start() -- end");
Expand All @@ -170,15 +169,14 @@ void DeviceReadBuffer::Reset(const QString &streamName, int streamfd)
void DeviceReadBuffer::Stop(void)
{
LOG(VB_RECORD, LOG_INFO, LOC + "Stop() -- begin");
QMutexLocker locker(&lock);
if (isRunning() || dorun)
{
QMutexLocker locker(&lock);
dorun = false;
locker.unlock();
WakePoll();
wait();
}

WakePoll();
LOG(VB_RECORD, LOG_INFO, LOC + "Stop() -- middle");

wait();
LOG(VB_RECORD, LOG_INFO, LOC + "Stop() -- end");
}

Expand All @@ -205,7 +203,7 @@ void DeviceReadBuffer::WakePoll(void) const
char buf[1];
buf[0] = '0';
ssize_t wret = 0;
while (running && (wret <= 0) && (wake_pipe[1] >= 0))
while (isRunning() && (wret <= 0) && (wake_pipe[1] >= 0))
{
wret = ::write(wake_pipe[1], &buf, 1);
if ((wret < 0) && (EAGAIN != errno) && (EINTR != errno))
Expand Down Expand Up @@ -277,7 +275,7 @@ bool DeviceReadBuffer::IsEOF(void) const
bool DeviceReadBuffer::IsRunning(void) const
{
QMutexLocker locker(&lock);
return running;
return isRunning();
}

uint DeviceReadBuffer::GetUnused(void) const
Expand Down Expand Up @@ -325,8 +323,6 @@ void DeviceReadBuffer::run(void)

threadRegister("DeviceReadBuffer");
lock.lock();
dorun = true;
running = true;
runWait.wakeAll();
lock.unlock();

Expand Down Expand Up @@ -382,7 +378,6 @@ void DeviceReadBuffer::run(void)
ClosePipes();

lock.lock();
running = false;
eof = true;
runWait.wakeAll();
dataWait.wakeAll();
Expand Down Expand Up @@ -675,7 +670,7 @@ uint DeviceReadBuffer::WaitForUsed(uint needed, uint max_wait) const

QMutexLocker locker(&lock);
size_t avail = used;
while ((needed > avail) && running &&
while ((needed > avail) && isRunning() &&
!request_pause && !error && !eof &&
(timer.elapsed() < (int)max_wait))
{
Expand Down
3 changes: 1 addition & 2 deletions mythtv/libs/libmythtv/DeviceReadBuffer.h
Expand Up @@ -89,8 +89,7 @@ class DeviceReadBuffer : protected QThread

// Data for managing the device ringbuffer
mutable QMutex lock;
bool dorun;
bool running;
volatile bool dorun;
bool eof;
mutable bool error;
bool request_pause;
Expand Down

0 comments on commit 1c2b33e

Please sign in to comment.