Skip to content

Commit

Permalink
Rework of [75d935], converts pthread to QThread.
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-kristjansson committed Apr 25, 2011
1 parent 7c118b8 commit 85e6bc4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
55 changes: 32 additions & 23 deletions mythtv/libs/libmythtv/channelscan/channelscan_sm.cpp
Expand Up @@ -28,7 +28,6 @@
*/

// C includes
#include <pthread.h>
#include <unistd.h>

// C++ includes
Expand Down Expand Up @@ -62,6 +61,15 @@ using namespace std;
#include "hdhrchannel.h"
#include "v4lchannel.h"

/** \fn ScannerThread::run(void)
* \brief Thunk that allows scannerThread thread to
* call ChannelScanSM::RunScanner().
*/
void ScannerThread::run(void)
{
m_parent->RunScanner();
}

/// SDT's should be sent every 2 seconds and NIT's every
/// 10 seconds, so lets wait at least 30 seconds, in
/// case of bad transmitter or lost packets.
Expand Down Expand Up @@ -165,7 +173,7 @@ ChannelScanSM::ChannelScanSM(
channelsFound(999),
currentInfo(NULL),
analogSignalHandler(new AnalogSignalHandler(this)),
scanner_thread_running(false)
scannerThread(NULL)
{
inputname.detach();

Expand Down Expand Up @@ -1401,23 +1409,23 @@ V4LChannel *ChannelScanSM::GetV4LChannel(void)
#endif
}

/** \fn ChannelScanSM::SpawnScanner(void*)
* \brief Thunk that allows scanner_thread pthread to
* call ChannelScanSM::RunScanner().
*/
void *ChannelScanSM::SpawnScanner(void *param)
{
ChannelScanSM *scanner = (ChannelScanSM *)param;
scanner->RunScanner();
return NULL;
}

/** \fn ChannelScanSM::StartScanner(void)
* \brief Starts the ChannelScanSM event loop.
*/
void ChannelScanSM::StartScanner(void)
{
pthread_create(&scanner_thread, NULL, SpawnScanner, this);
while (scannerThread)
{
threadExit = true;
if (scannerThread->wait(1000))
{
delete scannerThread;
scannerThread = NULL;
}
}
threadExit = false;
scannerThread = new ScannerThread(this);
scannerThread->start();
}

/** \fn ChannelScanSM::RunScanner(void)
Expand All @@ -1427,17 +1435,13 @@ void ChannelScanSM::RunScanner(void)
{
VERBOSE(VB_CHANSCAN, LOC + "ChannelScanSM::RunScanner -- begin");

scanner_thread_running = true;
threadExit = false;

while (!threadExit)
{
if (scanning)
HandleActiveScan();

usleep(250);
usleep(10 * 1000);
}
scanner_thread_running = false;

VERBOSE(VB_CHANSCAN, LOC + "ChannelScanSM::RunScanner -- end");
}
Expand Down Expand Up @@ -1667,10 +1671,15 @@ void ChannelScanSM::StopScanner(void)
{
VERBOSE(VB_CHANSCAN, LOC + "ChannelScanSM::StopScanner");

threadExit = true;

if (scanner_thread_running)
pthread_join(scanner_thread, NULL);
while (scannerThread)
{
threadExit = true;
if (scannerThread->wait(1000))
{
delete scannerThread;
scannerThread = NULL;
}
}

if (signalMonitor)
signalMonitor->Stop();
Expand Down
22 changes: 15 additions & 7 deletions mythtv/libs/libmythtv/channelscan/channelscan_sm.h
Expand Up @@ -30,10 +30,9 @@
#ifndef SISCAN_H
#define SISCAN_H

#include <pthread.h>

// Qt includes
#include <QString>
#include <QThread>
#include <QList>
#include <QPair>
#include <QMap>
Expand Down Expand Up @@ -80,12 +79,24 @@ class AnalogSignalHandler : public SignalMonitorListener
ChannelScanSM *siscan;
};

class ScannerThread : public QThread
{
Q_OBJECT
public:
ScannerThread(ChannelScanSM *parent) : m_parent(parent) {}
~ScannerThread() { wait(); m_parent = NULL; }
void run(void);
private:
ChannelScanSM *m_parent;
};

class ChannelScanSM : public MPEGStreamListener,
public ATSCMainStreamListener,
public DVBMainStreamListener,
public DVBOtherStreamListener
{
friend class AnalogSignalHandler;
friend class ScannerThread;

public:
ChannelScanSM(
Expand Down Expand Up @@ -163,8 +174,6 @@ class ChannelScanSM : public MPEGStreamListener,

/// \brief Called by SpawnScanner to run scanning thread
void RunScanner(void);
/// \brief Thunk to call RunScanner from pthread
static void *SpawnScanner(void *param);

bool HasTimedOut(void);
void HandleActiveScan(void);
Expand Down Expand Up @@ -221,7 +230,7 @@ class ChannelScanSM : public MPEGStreamListener,

// State
bool scanning;
bool threadExit;
volatile bool threadExit;
bool waitingForTables;
QTime timer;

Expand All @@ -246,8 +255,7 @@ class ChannelScanSM : public MPEGStreamListener,
AnalogSignalHandler *analogSignalHandler;

/// Scanner thread, runs ChannelScanSM::StartScanner()
pthread_t scanner_thread;
bool scanner_thread_running;
ScannerThread *scannerThread;
};

inline void ChannelScanSM::UpdateScanPercentCompleted(void)
Expand Down

0 comments on commit 85e6bc4

Please sign in to comment.