MythTV  master
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends | List of all members
MThread Class Reference

This is a wrapper around QThread that does several additional things. More...

#include <libmythbase/mthread.h>

Inheritance diagram for MThread:
[legend]
Collaboration diagram for MThread:
[legend]

Public Member Functions

 MThread (const QString &objectName)
 Standard constructor. More...
 
 MThread (const QString &objectName, QRunnable *runnable)
 Use this constructor if you want the default run() method to run the QRunnable's run() method instead of entering the Qt event loop. More...
 
virtual ~MThread ()
 
 MThread (const MThread &)=delete
 
MThreadoperator= (const MThread &)=delete
 
void RunProlog (void)
 Sets up a thread, call this if you reimplement run(). More...
 
void RunEpilog (void)
 Cleans up a thread's resources, call this if you reimplement run(). More...
 
QThread * qthread (void)
 Returns the thread, this will always return the same pointer no matter how often you restart the thread. More...
 
void setObjectName (const QString &name)
 
QString objectName (void) const
 
void setPriority (QThread::Priority priority)
 
QThread::Priority priority (void) const
 
bool isFinished (void) const
 
bool isRunning (void) const
 
void setStackSize (uint stackSize)
 
uint stackSize (void) const
 
void exit (int retcode=0)
 Use this to exit from the thread if you are using a Qt event loop. More...
 
void start (QThread::Priority p=QThread::InheritPriority)
 Tell MThread to start running the thread in the near future. More...
 
void terminate (void)
 Kill a thread unsafely. More...
 
void quit (void)
 calls exit(0) More...
 
bool wait (std::chrono::milliseconds time=std::chrono::milliseconds::max())
 Wait for the MThread to exit, with a maximum timeout. More...
 

Static Public Member Functions

static void ThreadSetup (const QString &name)
 This is to be called on startup in those few threads that haven't been ported to MThread. More...
 
static void ThreadCleanup (void)
 This is to be called on exit in those few threads that haven't been ported to MThread. More...
 
static void Cleanup (void)
 This will print out all the running threads, call exit(1) on each and then wait up to 5 seconds total for all the threads to exit. More...
 
static void GetAllThreadNames (QStringList &list)
 
static void GetAllRunningThreadNames (QStringList &list)
 

Protected Member Functions

virtual void run (void)
 Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead. More...
 
int exec (void)
 Enters the qt event loop. call exit or quit to exit thread. More...
 

Static Protected Member Functions

static void setTerminationEnabled (bool enabled=true)
 
static void usleep (std::chrono::microseconds time)
 
template<typename R , typename P >
static std::enable_if_t< std::chrono::treat_as_floating_point< R >::value, void > usleep (std::chrono::duration< R, P > time)
 

Protected Attributes

MThreadInternalm_thread {nullptr}
 
QRunnable * m_runnable {nullptr}
 
bool m_prologExecuted {true}
 
bool m_epilogExecuted {true}
 

Friends

class MThreadInternal
 

Detailed Description

This is a wrapper around QThread that does several additional things.

First it requires that you set the thread's name which is used for debugging.

It adds RunProlog() and RunEpilog() which are called automatically when you don't override run(), but must be called explicitly if you do. These take care of setting up logging and take care of dealing with QSqlConnections which are per-thread variables.

It also adds some sanity checking to the destructor so a thread can not be deleted while it still running.

Optionally it can be given a QRunnable to run in MThread::run() instead of calling QThread::run() (which starts an event loop.) When you override MThread::run() or use a QRunnable you are responsible for stopping the thread, MThread::exit() will not work.

Warning: Do not statically initialize MThreads. C++ itself doesn't allow you to specify the order of static initializations. See: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15

Definition at line 48 of file mthread.h.

Constructor & Destructor Documentation

◆ MThread() [1/3]

MThread::MThread ( const QString &  objectName)
explicit

Standard constructor.

Definition at line 99 of file mthread.cpp.

◆ MThread() [2/3]

MThread::MThread ( const QString &  objectName,
QRunnable *  runnable 
)
explicit

Use this constructor if you want the default run() method to run the QRunnable's run() method instead of entering the Qt event loop.

Unlike MThreadPool, MThread will not delete a runnable with the autoDelete property set.

Definition at line 107 of file mthread.cpp.

◆ ~MThread()

MThread::~MThread ( )
virtual

Definition at line 116 of file mthread.cpp.

◆ MThread() [3/3]

MThread::MThread ( const MThread )
delete

Member Function Documentation

◆ operator=()

MThread& MThread::operator= ( const MThread )
delete

◆ RunProlog()

void MThread::RunProlog ( void  )

Sets up a thread, call this if you reimplement run().

Definition at line 196 of file mthread.cpp.

Referenced by MonitorThreadDarwin::run(), MythDecoderThread::run(), MythHTTPThread::run(), HLSPlaylistWorker::run(), HLSStreamWorker::run(), DeleteThread::run(), MythFIFOThread::run(), PlaylistLoadingThread::run(), AlarmNotifyThread::run(), TFWWriteThread::run(), FirewireTableMonitorThread::run(), HTTPTSStreamHandler::run(), CDScannerThread::run(), HLSStreamHandler::run(), avfDecoder::run(), TFWSyncThread::run(), V4L2encStreamHandler::run(), MonitorThread::run(), FileScannerThread::run(), GameScannerThread::run(), MythSystemLegacyIOHandler::run(), ShellThread::run(), GetRecordingListThread::run(), LIRC::run(), CDEjectorThread::run(), ExpireThread::run(), SatIPStreamHandler::run(), FileCopyThread::run(), GrabberScript::run(), DVBStreamHandler::run(), CdDecoder::run(), NVRWriteThread::run(), MetadataDownload::run(), AppleRemote::run(), MythDownloadManager::run(), NVRAudioThread::run(), PreviewGenerator::run(), ASIStreamHandler::run(), DeviceReadBuffer::run(), VideoScannerThread::run(), MythSystemLegacyManager::run(), TVBrowseHelper::run(), CDRipperThread::run(), TransferThread::run(), VBIThread::run(), HDHRStreamHandler::run(), ExternalStreamHandler::run(), MythSystemLegacySignalManager::run(), LogForwardThread::run(), IPTVStreamHandler::run(), TaskQueue::run(), MetadataImageDownload::run(), JoystickMenuThread::run(), SSDP::run(), MPoolThread::run(), SignalMonitor::run(), run(), Scheduler::run(), GrabberDownloadThread::run(), HouseKeepingThread::run(), MythMediaBuffer::run(), AudioOutputBase::run(), LoggerThread::run(), AudioTestThread::run(), RecordingExtender::run(), AlbumArtScannerThread::run(), StreamWorker::run(), PlaylistWorker::run(), and CopyImageThread::run().

◆ RunEpilog()

void MThread::RunEpilog ( void  )

Cleans up a thread's resources, call this if you reimplement run().

Definition at line 209 of file mthread.cpp.

Referenced by MonitorThreadDarwin::run(), MythDecoderThread::run(), MythHTTPThread::run(), HLSPlaylistWorker::run(), HLSStreamWorker::run(), DeleteThread::run(), MythFIFOThread::run(), PlaylistLoadingThread::run(), AlarmNotifyThread::run(), TFWWriteThread::run(), FirewireTableMonitorThread::run(), HTTPTSStreamHandler::run(), CDScannerThread::run(), HLSStreamHandler::run(), avfDecoder::run(), TFWSyncThread::run(), V4L2encStreamHandler::run(), MonitorThread::run(), FileScannerThread::run(), GameScannerThread::run(), MythSystemLegacyIOHandler::run(), ShellThread::run(), GetRecordingListThread::run(), LIRC::run(), CDEjectorThread::run(), ExpireThread::run(), SatIPStreamHandler::run(), FileCopyThread::run(), GrabberScript::run(), DVBStreamHandler::run(), CdDecoder::run(), NVRWriteThread::run(), MetadataDownload::run(), AppleRemote::run(), MythDownloadManager::run(), NVRAudioThread::run(), PreviewGenerator::run(), ASIStreamHandler::run(), DeviceReadBuffer::run(), VideoScannerThread::run(), MythSystemLegacyManager::run(), TVBrowseHelper::run(), CDRipperThread::run(), TransferThread::run(), VBIThread::run(), HDHRStreamHandler::run(), ExternalStreamHandler::run(), MythSystemLegacySignalManager::run(), LogForwardThread::run(), IPTVStreamHandler::run(), TaskQueue::run(), MetadataImageDownload::run(), JoystickMenuThread::run(), SSDP::run(), MPoolThread::run(), SignalMonitor::run(), run(), Scheduler::run(), GrabberDownloadThread::run(), HouseKeepingThread::run(), MythMediaBuffer::run(), AudioOutputBase::run(), LoggerThread::run(), AudioTestThread::run(), RecordingExtender::run(), AlbumArtScannerThread::run(), StreamWorker::run(), PlaylistWorker::run(), and CopyImageThread::run().

◆ qthread()

QThread * MThread::qthread ( void  )

◆ setObjectName()

void MThread::setObjectName ( const QString &  name)

◆ objectName()

QString MThread::objectName ( void  ) const

Definition at line 243 of file mthread.cpp.

Referenced by MThread(), TransferThread::run(), MPoolThread::run(), and ~MThread().

◆ setPriority()

void MThread::setPriority ( QThread::Priority  priority)

Definition at line 248 of file mthread.cpp.

◆ priority()

QThread::Priority MThread::priority ( void  ) const

Definition at line 253 of file mthread.cpp.

Referenced by DVBStreamHandler::CycleFiltersByPriority(), and setPriority().

◆ isFinished()

bool MThread::isFinished ( void  ) const

◆ isRunning()

bool MThread::isRunning ( void  ) const

◆ setStackSize()

void MThread::setStackSize ( uint  stackSize)

Definition at line 268 of file mthread.cpp.

◆ stackSize()

uint MThread::stackSize ( void  ) const

Definition at line 273 of file mthread.cpp.

Referenced by setStackSize().

◆ exit()

void MThread::exit ( int  retcode = 0)

◆ start()

void MThread::start ( QThread::Priority  p = QThread::InheritPriority)

Tell MThread to start running the thread in the near future.

Definition at line 283 of file mthread.cpp.

Referenced by MetadataImageDownload::addDownloads(), MetadataDownload::addLookup(), MetadataImageDownload::addThumb(), AutoExpire::AutoExpire(), TVBrowseHelper::BrowseInit(), MythRAOPDevice::Create(), MythAirplayServer::Create(), RecordingExtender::create(), MusicPlayer::decoderHandlerReady(), MythPlayer::DecoderStart(), VideoScanner::doScan(), GameScanner::doScan(), GrabberManager::doUpdate(), EITScanner::EITScanner(), MythFIFOWriter::FIFOInit(), FirewireTableMonitorThread::FirewireTableMonitorThread(), MythSystemLegacyUnix::Fork(), MythBDBuffer::GetChapterStartTime(), MythBDBuffer::GetChapterStartTimeMs(), CdDecoder::getMetadata(), GetRecordingListThread::GetRecordingListThread(), TVRec::Init(), UPNPScanner::Instance(), JobQueue::JobQueue(), MythSystemLegacyWindows::JumpAbort(), MythSystemLegacyUnix::JumpAbort(), logForwardStart(), logStart(), main(), MythSystemLegacyWindows::Manage(), MythHTTPInstance::MythHTTPInstance(), mythplugin_init(), MythSocket::MythSocket(), MythSystemLegacyUnix::MythSystemLegacyUnix(), MythSystemLegacyWindows::MythSystemLegacyWindows(), MythUDP::MythUDP(), NetworkControl::NetworkControl(), HLSReader::Open(), ThreadedFileWriter::Open(), MythDVDStream::OpenFile(), HLSRingBuffer::OpenFile(), LinuxFirewireDevice::OpenPort(), PlaybackBoxHelper::PlaybackBoxHelper(), PlaylistContainer::PlaylistContainer(), MetadataDownload::prependLookup(), PreviewGeneratorQueue::PreviewGeneratorQueue(), TV::ProcessNetworkControlCommand(), GrabberDownloadThread::refreshAll(), MHIContext::Restart(), CDRipperThread::ripTrack(), NuppelVideoRecorder::run(), HLSRingBuffer::SanitizeStreams(), ExternRecChannelScanner::Scan(), VBoxChannelFetcher::Scan(), IPTVChannelFetcher::Scan(), HDHRChannelFetcher::Scan(), Scheduler::SchedNewFirstPass(), Scheduler::SchedNewRetryPass(), Scheduler::Scheduler(), ChannelScannerWeb::setupScan(), SSDP::SSDP(), MythInputDeviceHandler::Start(), DVBCam::Start(), LIRC::start(), DeviceReadBuffer::Start(), SignalMonitor::Start(), StreamHandler::Start(), MythMediaBuffer::Start(), Ripper::startEjectCD(), MediaMonitorDarwin::StartMonitoring(), MediaMonitor::StartMonitoring(), AudioOutputBase::StartOutputThread(), RipStatus::startRip(), Ripper::startScanCD(), ChannelScanSM::StartScanner(), TaskQueue::TaskQueue(), AudioTest::toggle(), AudioSetupWizard::toggleSpeakers(), TVRec::TuningNewRecorder(), VBIThread::VBIThread(), MetadataFactory::VideoScan(), and WaitUntilDone().

◆ terminate()

void MThread::terminate ( void  )

Kill a thread unsafely.

This should never be called on a thread while it holds a mutex or semaphore, since those locks will never be unlocked. Use the static setTerminationEnabled(true) to tell MThread when it is safe to terminate the thread and setTerminationEnabled(false) to tell it that termination is not safe again.

Definition at line 290 of file mthread.cpp.

◆ quit()

void MThread::quit ( void  )

◆ ThreadSetup()

void MThread::ThreadSetup ( const QString &  name)
static

◆ ThreadCleanup()

void MThread::ThreadCleanup ( void  )
static

This is to be called on exit in those few threads that haven't been ported to MThread.

Definition at line 226 of file mthread.cpp.

Referenced by dfd_controller_thunk(), JobQueue::FlagCommercialsThread(), JobQueue::MetadataLookupThread(), MPEG2fixup::ReplexStart(), RunEpilog(), JobQueue::TranscodeThread(), and JobQueue::UserJobThread().

◆ wait()

bool MThread::wait ( std::chrono::milliseconds  time = std::chrono::milliseconds::max())

Wait for the MThread to exit, with a maximum timeout.

Parameters
timeMaximum time to wait for MThread to exit, in ms

Definition at line 300 of file mthread.cpp.

Referenced by TVBrowseHelper::BrowseWait(), HLSPlaylistWorker::Cancel(), HLSStreamWorker::Cancel(), StreamWorker::Cancel(), PlaylistWorker::Cancel(), PlaylistContainer::cleanOutThreads(), cleanup(), MythRAOPDevice::Cleanup(), MythAirplayServer::Cleanup(), LinuxFirewireDevice::ClosePort(), RipStatus::customEvent(), MythPlayer::DecoderEnd(), MusicPlayer::decoderHandlerReady(), MThreadPool::DeletePoolThreads(), DVBSignalMonitor::DVBSignalMonitor(), VBoxChannelFetcher::GetChannels(), IPTVChannelFetcher::GetChannels(), HDHRChannelFetcher::GetChannels(), UPNPScanner::Instance(), NuppelVideoRecorder::KillChildren(), MythMediaBuffer::KillReadAheadThread(), logStop(), AudioTest::prepareTest(), MpegRecorder::run(), PlaylistWorker::run(), SatIPSignalMonitor::SatIPSignalMonitor(), ShutdownMythSystemLegacy(), DeviceReadBuffer::Start(), StreamHandler::Start(), ChannelScanSM::StartScanner(), AlarmNotifyThread::stop(), MythInputDeviceHandler::Stop(), DVBCam::Stop(), ExternRecChannelScanner::Stop(), FirewireSignalMonitor::Stop(), SignalMonitor::Stop(), DeviceReadBuffer::Stop(), VBoxChannelFetcher::Stop(), IPTVChannelFetcher::Stop(), StreamHandler::Stop(), DecoderHandler::stop(), HDHRChannelFetcher::Stop(), MHIContext::StopEngine(), MythHTTPInstance::StopHTTPService(), ChannelScannerWeb::stopMon(), MediaMonitor::StopMonitoring(), AudioOutputBase::StopOutputThread(), V4LRecorder::StopRecording(), ChannelScanSM::StopScanner(), MythUDP::StopUDPListener(), LIRC::TeardownAll(), EITScanner::TeardownAll(), PreviewGeneratorQueue::TeardownPreviewGeneratorQueue(), TVRec::TeardownRecorder(), AudioTest::toggle(), AudioSetupWizard::toggleSpeakers(), Scheduler::Wait(), AudioSetupWizard::~AudioSetupWizard(), AudioTest::~AudioTest(), AudioTestThread::~AudioTestThread(), AutoExpire::~AutoExpire(), CDRipperThread::~CDRipperThread(), ExpireThread::~ExpireThread(), FirewireTableMonitorThread::~FirewireTableMonitorThread(), GameScanner::~GameScanner(), GrabberDownloadThread::~GrabberDownloadThread(), GrabberScript::~GrabberScript(), HouseKeeper::~HouseKeeper(), JobQueue::~JobQueue(), LFDPriv::~LFDPriv(), LogForwardThread::~LogForwardThread(), LoggerThread::~LoggerThread(), MetadataDownload::~MetadataDownload(), MetadataFactory::~MetadataFactory(), MetadataImageDownload::~MetadataImageDownload(), MonitorThread::~MonitorThread(), MythDecoderThread::~MythDecoderThread(), MythDownloadManager::~MythDownloadManager(), MythFIFOThread::~MythFIFOThread(), MythHTTPInstance::~MythHTTPInstance(), MythMediaBuffer::~MythMediaBuffer(), MythSocket::~MythSocket(), MythSystemLegacyIOHandler::~MythSystemLegacyIOHandler(), MythSystemLegacyManager::~MythSystemLegacyManager(), MythSystemLegacySignalManager::~MythSystemLegacySignalManager(), MythUDP::~MythUDP(), NetworkControl::~NetworkControl(), NVRAudioThread::~NVRAudioThread(), NVRWriteThread::~NVRWriteThread(), PlaybackBoxHelper::~PlaybackBoxHelper(), PlaylistContainer::~PlaylistContainer(), PreviewGenerator::~PreviewGenerator(), PreviewGeneratorQueue::~PreviewGeneratorQueue(), Scheduler::~Scheduler(), SignalMonitor::~SignalMonitor(), SSDP::~SSDP(), TaskQueue::~TaskQueue(), TFWSyncThread::~TFWSyncThread(), TFWWriteThread::~TFWWriteThread(), ThreadedFileWriter::~ThreadedFileWriter(), TVRec::~TVRec(), V4LRecorder::~V4LRecorder(), VBIThread::~VBIThread(), and VideoScanner::~VideoScanner().

◆ Cleanup()

void MThread::Cleanup ( void  )
static

This will print out all the running threads, call exit(1) on each and then wait up to 5 seconds total for all the threads to exit.

Definition at line 142 of file mthread.cpp.

Referenced by MythCoreContextPrivate::~MythCoreContextPrivate().

◆ GetAllThreadNames()

void MThread::GetAllThreadNames ( QStringList &  list)
static

Definition at line 179 of file mthread.cpp.

◆ GetAllRunningThreadNames()

void MThread::GetAllRunningThreadNames ( QStringList &  list)
static

Definition at line 186 of file mthread.cpp.

◆ run()

void MThread::run ( void  )
protectedvirtual

Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.

Note
If you override this method you must call RunProlog before you do any work and RunEpilog before you exit the run method.

Reimplemented in PlaylistWorker, StreamWorker, RecordingExtender, LoggerThread, MythMediaBuffer, HouseKeepingThread, Scheduler, SignalMonitor, MPoolThread, IPTVStreamHandler, LogForwardThread, MythSystemLegacySignalManager, ExternalStreamHandler, DeleteThread, MythSystemLegacySignalManager, HDHRStreamHandler, VBIThread, CDRipperThread, MythSystemLegacyManager, PreviewGenerator, ASIStreamHandler, DeviceReadBuffer, NVRAudioThread, MythDownloadManager, MythSystemLegacyManager, anonymous_namespace{volumebase.cpp}::VolumeWriteBackThread, NVRWriteThread, DVBStreamHandler, GrabberScript, SatIPStreamHandler, ExpireThread, LIRC, GetRecordingListThread, MythSystemLegacyIOHandler, GameScannerThread, TFWSyncThread, HLSStreamHandler, V4L2encStreamHandler, MonitorThread, MythSystemLegacyIOHandler, FirewireTableMonitorThread, HTTPTSStreamHandler, TFWWriteThread, AlarmNotifyThread, MythFIFOThread, MonitorThreadDarwin, CopyImageThread, AlbumArtScannerThread, MetadataLoadingThread, AudioTestThread, AudioOutputBase, GrabberDownloadThread, SSDP, ThumbThread< DBFS >, ThumbThread< ImageDbLocal >, ThumbThread< ImageDbSg >, JoystickMenuThread, MetadataImageDownload, TaskQueue, TransferThread, TVBrowseHelper, VideoScannerThread, AppleRemote, MetadataDownload, CdDecoder, FileCopyThread, CDEjectorThread, ShellThread, ImageScanThread< DBFS >, ImageScanThread< ImageDbLocal >, ImageScanThread< ImageDbSg >, FileScannerThread, avfDecoder, CDScannerThread, PlaylistLoadingThread, DeleteThread, HLSStreamWorker, MythHTTPThread, HLSPlaylistWorker, and MythDecoderThread.

Definition at line 315 of file mthread.cpp.

Referenced by MThreadInternal::run().

◆ exec()

int MThread::exec ( void  )
protected

Enters the qt event loop. call exit or quit to exit thread.

Definition at line 325 of file mthread.cpp.

Referenced by MythHTTPThread::run(), AppleRemote::run(), and IPTVStreamHandler::run().

◆ setTerminationEnabled()

void MThread::setTerminationEnabled ( bool  enabled = true)
staticprotected

Definition at line 330 of file mthread.cpp.

Referenced by RunProlog().

◆ usleep() [1/2]

void MThread::usleep ( std::chrono::microseconds  time)
staticprotected

◆ usleep() [2/2]

template<typename R , typename P >
static std::enable_if_t<std::chrono::treat_as_floating_point<R>::value, void> MThread::usleep ( std::chrono::duration< R, P >  time)
inlinestaticprotected

Definition at line 133 of file mthread.h.

Referenced by usleep().

Friends And Related Function Documentation

◆ MThreadInternal

friend class MThreadInternal
friend

Definition at line 50 of file mthread.h.

Member Data Documentation

◆ m_thread

MThreadInternal* MThread::m_thread {nullptr}
protected

◆ m_runnable

QRunnable* MThread::m_runnable {nullptr}
protected

Definition at line 136 of file mthread.h.

Referenced by MPoolThread::run(), run(), and MPoolThread::SetRunnable().

◆ m_prologExecuted

bool MThread::m_prologExecuted {true}
protected

Definition at line 137 of file mthread.h.

Referenced by RunProlog(), start(), and ~MThread().

◆ m_epilogExecuted

bool MThread::m_epilogExecuted {true}
protected

Definition at line 138 of file mthread.h.

Referenced by RunEpilog(), start(), and ~MThread().


The documentation for this class was generated from the following files: