Improve the use of threads and sleep when not in use

This commit is contained in:
jfmcarreira committed 2021-09-07 02:00:30 +01:00
1 parent bf5235352b
commit ffe1bf688d
15 files changed
+164 -129

No files matched your search

+1
View File
@@ -9,6 +9,7 @@ INCLUDE_DIRECTORIES(
OPTION(USE_QTDBUS "Build with QtDBus" ON)
OPTION(USE_FERVOR "Add Fervor support" OFF)
OPTION(USE_RESOURCES "Handle streams in separate threads" ON)
ADD_SUBDIRECTORY(external)
-1
View File
@@ -225,7 +225,6 @@ void CalypAppModuleIf::destroy()
}
if( m_pcModuleStream )
{
m_pcModuleStream->close();
delete m_pcModuleStream;
m_pcModuleStream = NULL;
}
+50 -35
View File
@@ -44,13 +44,29 @@ void ResourceWorker::stop()
wait();
}
void ResourceWorker::wake()
{
m_Mutex.lock();
m_ResourceIdle.wakeAll();
m_Mutex.unlock();
}
void ResourceWorker::run()
{
// Loop forever
while( !m_bStop )
{
m_pcStream->readNextFrameFillRGBBuffer();
if( !m_pcStream->hasWritingSlot() )
{
m_Mutex.lock();
m_ResourceIdle.wait( &m_Mutex );
m_Mutex.unlock();
}
while( !m_bStop && !m_pcStream->hasWritingSlot() )
; // Keep
{
// Wait here
}
}
}
@@ -58,73 +74,72 @@ ResourceHandle::ResourceHandle()
{
}
auto ResourceHandle::addResource() -> CalypStream*
auto ResourceHandle::addResource() -> std::size_t
{
auto newStreamResource = std::make_shared<CalypStream>();
auto newStreamResourceWorker = std::make_unique<ResourceWorker>( newStreamResource );
CalypStream* ptr = newStreamResource.get();
m_apcStreamResourcesList.push_back( newStreamResource );
m_apcStreamResourcesWorkersList.push_back( std::move( newStreamResourceWorker ) );
return ptr;
return m_apcStreamResourcesList.size() - 1;
}
auto ResourceHandle::getResource( CalypStream* ptr ) -> CalypStream*
auto ResourceHandle::getResource( CalypStream* ptr ) -> std::size_t
{
if( ptr != nullptr )
{
for( auto& resource_ptr : m_apcStreamResourcesList )
for( std::size_t i = 0; i < m_apcStreamResourcesList.size(); i++ )
{
if( resource_ptr.get() == ptr )
return resource_ptr.get();
if( m_apcStreamResourcesList[i].get() == ptr )
return i;
}
}
return addResource();
}
void ResourceHandle::removeResource( CalypStream* ptr )
auto ResourceHandle::getResourceAsset( std::size_t id ) -> CalypStream*
{
std::optional<std::size_t> posToRemove;
for( std::size_t i = 0; i < m_apcStreamResourcesList.size(); i++ )
if( id < m_apcStreamResourcesList.size() )
{
if( m_apcStreamResourcesList[i].get() == ptr )
{
posToRemove = i;
break;
}
return m_apcStreamResourcesList[id].get();
}
if( !posToRemove.has_value() )
return nullptr;
}
void ResourceHandle::removeResource( std::size_t id )
{
if( id >= m_apcStreamResourcesList.size() )
{
assert( false );
return;
}
// Remove worker first
m_apcStreamResourcesWorkersList[*posToRemove]->stop();
m_apcStreamResourcesWorkersList.erase( m_apcStreamResourcesWorkersList.begin() + *posToRemove );
m_apcStreamResourcesWorkersList[id]->stop();
m_apcStreamResourcesWorkersList.erase( m_apcStreamResourcesWorkersList.begin() + id );
// Then the underlying stream
m_apcStreamResourcesList.erase( m_apcStreamResourcesList.begin() + *posToRemove );
m_apcStreamResourcesList.erase( m_apcStreamResourcesList.begin() + id );
}
void ResourceHandle::startResourceWorker( CalypStream* ptr )
void ResourceHandle::startResourceWorker( std::size_t id )
{
std::optional<std::size_t> position;
for( std::size_t i = 0; i < m_apcStreamResourcesList.size(); i++ )
{
if( m_apcStreamResourcesList[i].get() == ptr )
{
position = i;
break;
}
}
if( !position.has_value() )
if( id >= m_apcStreamResourcesList.size() )
{
assert( false );
return;
}
m_apcStreamResourcesWorkersList[*position]->setObjectName(
m_apcStreamResourcesWorkersList[id]->setObjectName(
"RW-" +
QFileInfo( QString::fromStdString( m_apcStreamResourcesList[*position]->getFileName() ) ).completeBaseName() );
m_apcStreamResourcesWorkersList[*position]->start();
QFileInfo( QString::fromStdString( m_apcStreamResourcesList[id]->getFileName() ) ).completeBaseName() );
m_apcStreamResourcesWorkersList[id]->start();
}
void ResourceHandle::wakeResourceWorker( std::size_t id )
{
if( id >= m_apcStreamResourcesList.size() )
{
assert( false );
return;
}
m_apcStreamResourcesWorkersList[id]->wake();
}
+11 -4
View File
@@ -25,8 +25,10 @@
#ifndef __RESOURCEHANDLE_H__
#define __RESOURCEHANDLE_H__
#include <QMutex>
#include <QThread>
#include <QVector>
#include <QWaitCondition>
#include "CommonDefs.h"
#include "lib/CalypStream.h"
@@ -34,12 +36,15 @@
class ResourceWorker : public QThread
{
private:
QMutex m_Mutex;
QWaitCondition m_ResourceIdle;
std::shared_ptr<CalypStream> m_pcStream;
bool m_bStop{ false };
public:
ResourceWorker( std::shared_ptr<CalypStream> stream );
void stop();
void wake();
void run();
};
@@ -49,12 +54,14 @@ public:
ResourceHandle();
~ResourceHandle() = default;
auto getResource( CalypStream* ptr ) -> CalypStream*;
void removeResource( CalypStream* ptr );
void startResourceWorker( CalypStream* ptr );
auto getResource( CalypStream* ptr ) -> std::size_t;
auto getResourceAsset( std::size_t id ) -> CalypStream*;
void removeResource( std::size_t id );
void startResourceWorker( std::size_t id );
void wakeResourceWorker( std::size_t id );
private:
auto addResource() -> CalypStream*;
auto addResource() -> std::size_t;
private:
std::vector<std::shared_ptr<CalypStream>> m_apcStreamResourcesList;
+3 -3
View File
@@ -326,8 +326,8 @@ void VideoHandle::update()
{
if( m_pcCurrentVideoSubWindow )
{
CalypStream* pcStream = m_pcCurrentVideoSubWindow->getInputStream();
CalypFrame* pcFrame = m_pcCurrentVideoSubWindow->getCurrFrame();
const CalypStream* pcStream = m_pcCurrentVideoSubWindow->getInputStream();
const CalypFrame* pcFrame = m_pcCurrentVideoSubWindow->getCurrFrame();
m_pcVideoFormatLabel->setText( m_pcCurrentVideoSubWindow->getStreamInformation() );
@@ -364,7 +364,7 @@ void VideoHandle::update()
}
m_pcResolutionLabel->setText( resolution );
m_pcFramePropertiesSideBar->setFrame( pcFrame, m_pcCurrentVideoSubWindow->isPlaying() );
m_pcFramePropertiesSideBar->setFrame( m_pcCurrentVideoSubWindow->getCurrFrameAsset(), m_pcCurrentVideoSubWindow->isPlaying() );
m_pcFrameNumInfo->setTotalFrameNum( total_frame_num );
m_pcFrameNumInfo->setCurrFrameNum( frame_num );
+33 -29
View File
@@ -35,8 +35,7 @@
#include "ResourceHandle.h"
#include "SubWindowAbstract.h"
//#define QT_NO_CONCURRENT
//#define USE_CALYPSTREAM_RETRIEVE
#define QT_NO_CONCURRENT
/**
* \brief Functions to control data stream from stream information
@@ -200,7 +199,7 @@ VideoSubWindow::VideoSubWindow( enum VideoSubWindowCategories category, QWidget*
VideoSubWindow::~VideoSubWindow()
{
disableModule();
m_pcResourceManager->removeResource( m_pCurrStream );
m_pcResourceManager->removeResource( m_uiResourceId );
//delete m_cViewArea;
delete m_pcUpdateTimer;
}
@@ -331,7 +330,9 @@ bool VideoSubWindow::loadFile( QString cFilename, bool bForceDialog )
if( m_pCurrStream )
m_pCurrStream->getFormat( Width, Height, InputFormat, BitsPel, Endianness, FrameRate );
else
m_pCurrStream = m_pcResourceManager->getResource( nullptr );
m_uiResourceId = m_pcResourceManager->getResource( nullptr );
m_pCurrStream = m_pcResourceManager->getResourceAsset( m_uiResourceId );
bool bConfig = true;
if( !bForceDialog )
@@ -384,8 +385,8 @@ bool VideoSubWindow::loadFile( QString cFilename, bool bForceDialog )
var.setValue<unsigned int>( BitsPel );
appSettings.setValue( "VideoSubWindow/LastBitsPerPixel", var );
#ifdef USE_CALYPSTREAM_RETRIEVE
m_pcResourceManager->startResourceWorker( m_pCurrStream );
#ifdef USE_RESOURCES
m_pcResourceManager->startResourceWorker( m_uiResourceId );
#endif
QApplication::restoreOverrideCursor();
@@ -404,7 +405,8 @@ bool VideoSubWindow::loadFile( CalypFileInfo* streamInfo )
{
assert( m_pcResourceManager != nullptr );
m_pCurrStream = m_pcResourceManager->getResource( m_pCurrStream );
m_uiResourceId = m_pcResourceManager->getResource( m_pCurrStream );
m_pCurrStream = m_pcResourceManager->getResourceAsset( m_uiResourceId );
if( !m_pCurrStream->open( streamInfo->m_cFilename.toStdString(), streamInfo->m_uiWidth, streamInfo->m_uiHeight,
streamInfo->m_iPelFormat, streamInfo->m_uiBitsPelPixel, streamInfo->m_iEndianness,
@@ -415,8 +417,8 @@ bool VideoSubWindow::loadFile( CalypFileInfo* streamInfo )
m_sStreamInfo = *streamInfo;
#ifdef USE_CALYPSTREAM_RETRIEVE
m_pcResourceManager->startResourceWorker( m_pCurrStream );
#ifdef USE_RESOURCES
m_pcResourceManager->startResourceWorker( m_uiResourceId );
#endif
QApplication::restoreOverrideCursor();
@@ -728,16 +730,22 @@ void VideoSubWindow::setCurrFrame( CalypFrame* pcCurrFrame )
m_cViewArea->setImage( m_pcCurrFrame );
}
void VideoSubWindow::setCurrFrame( std::shared_ptr<CalypFrame> pcCurrFrame )
{
m_pcCurrDisplayFrame = pcCurrFrame;
m_cViewArea->setImage( m_pcCurrDisplayFrame.get() );
}
void VideoSubWindow::refreshFrameOperation()
{
bool bSetFrame = false;
if( m_pcCurrStreamFrame != nullptr )
{
m_pcCurrFrame = m_pcCurrStreamFrame.get();
}
else if( m_pCurrStream )
if( m_pCurrStream )
{
#ifdef USE_RESOURCES
m_pcCurrFrame = m_pCurrStream->getCurrFrameAsset().get();
#else
m_pcCurrFrame = m_pCurrStream->getCurrFrame();
#endif
}
bSetFrame = m_pcCurrFrame ? true : false;
if( m_pcCurrentDisplayModule )
@@ -773,12 +781,13 @@ void VideoSubWindow::refreshFrame( bool bThreaded )
bool VideoSubWindow::goToNextFrame( bool bThreaded )
{
#ifdef USE_CALYPSTREAM_RETRIEVE
m_pcCurrStreamFrame = m_pCurrStream->retrieveNextFrame();
if( m_pcCurrStreamFrame == nullptr )
return true;
refreshFrame();
return false;
#ifdef USE_RESOURCES
bool bEndOfSeq = m_pCurrStream->retrieveNextFrame();
m_pcResourceManager->wakeResourceWorker( m_uiResourceId );
if( !bEndOfSeq )
{
refreshFrame();
}
#else
#ifndef QT_NO_CONCURRENT
m_cRefreshResult.waitForFinished();
@@ -801,8 +810,8 @@ bool VideoSubWindow::goToNextFrame( bool bThreaded )
m_pCurrStream->readNextFrameFillRGBBuffer();
}
}
return bEndOfSeq;
#endif
return bEndOfSeq;
}
bool VideoSubWindow::save( QString filename )
@@ -887,7 +896,7 @@ void VideoSubWindow::pause()
void VideoSubWindow::seekAbsoluteEvent( unsigned int new_frame_num )
{
m_pcCurrStreamFrame = nullptr;
m_pcResourceManager->wakeResourceWorker( m_uiResourceId );
if( m_pCurrStream )
if( m_pCurrStream->seekInput( new_frame_num ) )
refreshFrame();
@@ -904,14 +913,9 @@ void VideoSubWindow::seekRelativeEvent( bool bIsFoward )
}
else
{
m_pcCurrStreamFrame = nullptr;
bRefresh = m_pCurrStream->seekInputRelative( bIsFoward );
#ifdef USE_CALYPSTREAM_RETRIEVE
if( bRefresh )
{
m_pcCurrStreamFrame = m_pCurrStream->retrieveNextFrame();
bRefresh &= m_pcCurrStreamFrame != nullptr;
}
#ifdef USE_RESOURCES
m_pcResourceManager->wakeResourceWorker( m_uiResourceId );
#endif
}
}
+6 -3
View File
@@ -82,8 +82,9 @@ private:
QString m_cFilename;
QString m_cStreamInformation;
CalypFileInfo m_sStreamInfo;
std::size_t m_uiResourceId;
CalypStream* m_pCurrStream;
std::shared_ptr<CalypFrame> m_pcCurrStreamFrame;
std::shared_ptr<CalypFrame> m_pcCurrDisplayFrame;
CalypFrame* m_pcCurrFrame;
QRect m_cSelectedArea;
@@ -139,12 +140,14 @@ public:
void seekRelativeEvent( bool bIsFoward );
void setCurrFrame( CalypFrame* pcCurrFrame );
void setCurrFrame( std::shared_ptr<CalypFrame> pcCurrFrame );
CalypFileInfo getStreamInfo() { return m_sStreamInfo; }
QString getStreamInformation() { return m_cStreamInformation; }
CalypStream* getInputStream() { return m_pCurrStream; }
CalypStream* getCurrStream() { return m_pCurrStream; }
const CalypStream* getInputStream() { return m_pCurrStream; }
const CalypStream* getCurrStream() { return m_pCurrStream; }
CalypFrame* getCurrFrame() { return m_pcCurrFrame; }
std::shared_ptr<CalypFrame> getCurrFrameAsset() { return m_pCurrStream->getCurrFrameAsset(); };
ViewArea* getViewArea() { return m_cViewArea; }
void setRefSubWindow( VideoSubWindow* subWindow )
{
+1 -1
View File
@@ -33,7 +33,7 @@
#include "lib/CalypStream.h"
SeekStreamDialog::SeekStreamDialog( CalypStream* pcCurrStream, QWidget* parent )
SeekStreamDialog::SeekStreamDialog( const CalypStream* pcCurrStream, QWidget* parent )
: QDialog( parent, Qt::Dialog | Qt::WindowTitleHint ), m_iSelectedFrameNum()
{
m_iSelectedFrameNum = pcCurrStream->getCurrFrameNum();
+1 -1
View File
@@ -42,7 +42,7 @@ class SeekStreamDialog : public QDialog
Q_OBJECT
public:
SeekStreamDialog( CalypStream* pcCurrStream, QWidget* parent = 0 );
SeekStreamDialog( const CalypStream* pcCurrStream, QWidget* parent = 0 );
int runDialog();
private:
+2 -2
View File
@@ -297,7 +297,7 @@ void FramePropertiesDock::reset()
setEnabled( false );
}
void FramePropertiesDock::setFrame( CalypFrame* pcFrame, bool isPlaying )
void FramePropertiesDock::setFrame( std::shared_ptr<CalypFrame> pcFrame, bool isPlaying )
{
int colorSpace = pcFrame->getColorSpace();
if( m_iLastFrameType != colorSpace )
@@ -365,7 +365,7 @@ void FramePropertiesDock::setFrame( CalypFrame* pcFrame, bool isPlaying )
if( pcFrame )
{
m_bHasFrame = true;
m_pcFrame = std::make_shared<CalypFrame>( *pcFrame );
m_pcFrame = pcFrame;
}
else
{
+1 -1
View File
@@ -43,7 +43,7 @@ public:
QSize sizeHint() const;
void setFrame( CalypFrame* m_pcFrame, bool isPlaying );
void setFrame( std::shared_ptr<CalypFrame> m_pcFrame, bool isPlaying );
void setSelection( const QRect& selectionArea );
void reset();
void stopHistogram();