Commit 0d921692 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

ParserService: Add support for multithreading

parent 6eca6d55
......@@ -468,3 +468,13 @@ const char* MetadataParser::name() const
{
return "Metadata";
}
uint8_t MetadataParser::nbThreads() const
{
// auto nbProcs = std::thread::hardware_concurrency();
// if ( nbProcs == 0 )
// return 1;
// return nbProcs;
// Let's make this code thread-safe first :)
return 1;
}
......@@ -35,6 +35,7 @@ protected:
virtual bool initialize() override;
virtual parser::Task::Status run( parser::Task& task ) override;
virtual const char* name() const override;
virtual uint8_t nbThreads() const override;
std::shared_ptr<Album> findAlbum(File& file, VLC::Media& vlcMedia, const std::string& title, Artist* albumArtist ) const;
bool parseAudioFile(Media& media, File& file, VLC::Media &vlcMedia ) const;
......
......@@ -71,3 +71,8 @@ const char* VLCMetadataService::name() const
return "VLC";
}
uint8_t VLCMetadataService::nbThreads() const
{
return 1;
}
......@@ -35,8 +35,10 @@ class VLCMetadataService : public ParserService
public:
explicit VLCMetadataService(const VLC::Instance& vlc);
private:
virtual parser::Task::Status run( parser::Task& task ) override;
virtual const char* name() const override;
virtual uint8_t nbThreads() const override;
private:
VLC::Instance m_instance;
......
......@@ -364,3 +364,8 @@ const char*VLCThumbnailer::name() const
{
return "Thumbnailer";
}
uint8_t VLCThumbnailer::nbThreads() const
{
return 1;
}
......@@ -58,6 +58,7 @@ private:
parser::Task::Status compress( std::shared_ptr<Media> media, std::shared_ptr<File> file );
virtual const char* name() const;
virtual uint8_t nbThreads() const override;
private:
// Force a base width, let height be computed depending on A/R
......
......@@ -31,23 +31,32 @@ ParserService::ParserService()
ParserService::~ParserService()
{
if ( m_thread.joinable() )
// Alert first:
for ( auto& t : m_threads )
{
if ( t.joinable() )
{
std::lock_guard<std::mutex> lock( m_lock );
m_cond.notify_all();
m_stopParser = true;
{
std::lock_guard<std::mutex> lock( m_lock );
m_cond.notify_all();
m_stopParser = true;
}
}
m_thread.join();
}
// Join afterward:
for ( auto& t : m_threads )
{
if ( t.joinable() )
t.join();
}
}
void ParserService::start()
{
// Ensure we don't start multiple times.
assert( m_thread.joinable() == false );
m_thread = std::thread{ &ParserService::mainloop, this };
assert( m_threads.size() == 0 );
for ( auto i = 0u; i < nbThreads(); ++i )
m_threads.emplace_back( &ParserService::mainloop, this );
}
void ParserService::pause()
......
......@@ -51,6 +51,7 @@ protected:
virtual bool initialize();
virtual parser::Task::Status run( parser::Task& task ) = 0;
virtual const char* name() const = 0;
virtual uint8_t nbThreads() const = 0;
private:
// Thread(s) entry point
......@@ -63,6 +64,6 @@ private:
bool m_paused;
std::condition_variable m_cond;
std::queue<std::unique_ptr<parser::Task>> m_tasks;
std::thread m_thread;
std::vector<std::thread> m_threads;
std::mutex m_lock;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment