diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am index 7189085617..2c71730d78 100644 --- a/modules/gui/qt/Makefile.am +++ b/modules/gui/qt/Makefile.am @@ -277,6 +277,7 @@ libqt_plugin_la_SOURCES = \ util/asynctask.hpp \ util/audio_device_model.cpp \ util/audio_device_model.hpp \ + util/workerthreadset.hpp util/workerthreadset.cpp \ util/base_model.hpp util/base_model_p.hpp util/base_model.cpp \ util/color_scheme_model.cpp util/color_scheme_model.hpp \ util/color_svg_image_provider.cpp util/color_svg_image_provider.hpp \ @@ -443,6 +444,7 @@ nodist_libqt_plugin_la_SOURCES = \ playlist/playlist_model.moc.cpp \ util/asynctask.moc.cpp \ util/audio_device_model.moc.cpp \ + util/workerthreadset.moc.cpp \ util/base_model.moc.cpp \ util/color_scheme_model.moc.cpp \ util/color_svg_image_provider.moc.cpp \ diff --git a/modules/gui/qt/meson.build b/modules/gui/qt/meson.build index 738cfa9a6f..8de53833bf 100644 --- a/modules/gui/qt/meson.build +++ b/modules/gui/qt/meson.build @@ -121,6 +121,7 @@ moc_headers = files( 'playlist/playlist_model.hpp', 'util/asynctask.hpp', 'util/audio_device_model.hpp', + 'util/workerthreadset.hpp', 'util/base_model.hpp', 'util/color_scheme_model.hpp', 'util/color_svg_image_provider.hpp', @@ -408,6 +409,8 @@ some_sources = files( 'util/asynctask.hpp', 'util/audio_device_model.cpp', 'util/audio_device_model.hpp', + 'util/workerthreadset.cpp', + 'util/workerthreadset.hpp', 'util/base_model.cpp', 'util/base_model.hpp', 'util/base_model_p.hpp', diff --git a/modules/gui/qt/util/workerthreadset.cpp b/modules/gui/qt/util/workerthreadset.cpp new file mode 100644 index 0000000000..664b817131 --- /dev/null +++ b/modules/gui/qt/util/workerthreadset.cpp @@ -0,0 +1,103 @@ +/***************************************************************************** + * Copyright (C) 2024 VLC authors and VideoLAN + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * ( at your option ) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#include "workerthreadset.hpp" + +#include + +WorkerThreadSet::~WorkerThreadSet() +{ + for (auto worker : workers) + { + if (!worker.thread) + continue; + + finish( worker ); + } +} + +void WorkerThreadSet::assignToWorkerThread(QObject *obj) +{ + auto worker = reserve(); + obj->moveToThread( worker ); + + QObject::connect(obj, &QObject::destroyed, worker, [this, worker]() + { + unreserve( worker ); + }); +} + +void WorkerThreadSet::finish(Worker &worker) +{ + worker.thread->quit(); + worker.thread->wait(); + delete worker.thread; +} + +QThread *WorkerThreadSet::reserve() +{ + auto itr = std::min_element(std::begin(workers) + , std::end(workers) + , [](const Worker &l, const Worker &r) + { + return l.load < r.load; + }); + + assert(itr != std::end(workers)); + + if (!itr->thread) + { + itr->thread = new QThread; + itr->thread->start(); + } + + itr->load++; + itr->inactiveTime.invalidate(); + return itr->thread; +} + +void WorkerThreadSet::unreserve(QThread *thread) +{ + auto itr = std::find_if(std::begin(workers) + , std::end(workers) + , [thread](const Worker &i) { return i.thread == thread; }); + + if (itr == std::end(workers)) return; // impossible? + + const int load = --itr->load; + if (load != 0) + return; + + itr->inactiveTime.start(); + QTimer::singleShot(CLEANUP_TIMEOUT, this, &WorkerThreadSet::cleanupInactiveWorker); +} + +void WorkerThreadSet::cleanupInactiveWorker() +{ + for (auto &worker : workers) + { + if ((worker.load == 0) + && worker.inactiveTime.hasExpired(MAX_INACTIVE_TIME) + && worker.thread) + { + finish( worker ); + + worker.thread = nullptr; + } + } +} diff --git a/modules/gui/qt/util/workerthreadset.hpp b/modules/gui/qt/util/workerthreadset.hpp new file mode 100644 index 0000000000..a374574a84 --- /dev/null +++ b/modules/gui/qt/util/workerthreadset.hpp @@ -0,0 +1,63 @@ +/***************************************************************************** + * Copyright (C) 2024 VLC authors and VideoLAN + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * ( at your option ) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#pragma once + +#include +#include + +// maintains a set of reusable worker threads +// class is not thread safe and must be accessed from Main thread +class WorkerThreadSet : public QObject +{ + Q_OBJECT +public: + using QObject::QObject; + + ~WorkerThreadSet(); + + // changes the thread affinity of 'obj' to a worker thread + void assignToWorkerThread(QObject *obj); + +private: + struct Worker + { + QThread *thread = nullptr; + int load = 0; + QElapsedTimer inactiveTime; + }; + + const static int MAX_WORKER = 2; + const static int CLEANUP_TIMEOUT = 10000; // 10seconds + const static int MAX_INACTIVE_TIME = 6000; // 6seconds + + void finish(Worker &worker); + + // returns a worker thread after increasing it's load + QThread *reserve(); + + // reduces load of the previously allocated 'thread' + // and makes it available for future operations + // threads are automatically freed when they remain + // inactive for extended amount of time + void unreserve(QThread *thread); + + void cleanupInactiveWorker(); + + Worker workers[MAX_WORKER] {}; +};