Use QThreadPool for torrent creation

The change is in preparation for adding the possibility to create
torrent files via the API.

Rework TorrentCreatorThread to be a more lightweight QRunnable class.
The parameters are now defined on construction time and are fixed
throughout the lifecycle of the TorrentCreator. The lifecycle of the
object is not bound to the one of QDialog anymore; it is now handled
by the QThreadPool. This will enable easier queueing of multiple torrent
creation jobs without risk of spawning many threads.

PR #19500.
This commit is contained in:
rcarpa
2023-08-26 18:27:11 +02:00
committed by GitHub
parent 9ab8203c8a
commit bbac94cc95
5 changed files with 50 additions and 40 deletions

View File

@@ -35,7 +35,7 @@ add_library(qbt_base STATIC
bittorrent/torrent.h
bittorrent/torrentcontenthandler.h
bittorrent/torrentcontentlayout.h
bittorrent/torrentcreatorthread.h
bittorrent/torrentcreator.h
bittorrent/torrentdescriptor.h
bittorrent/torrentimpl.h
bittorrent/torrentinfo.h
@@ -132,7 +132,7 @@ add_library(qbt_base STATIC
bittorrent/speedmonitor.cpp
bittorrent/torrent.cpp
bittorrent/torrentcontenthandler.cpp
bittorrent/torrentcreatorthread.cpp
bittorrent/torrentcreator.cpp
bittorrent/torrentdescriptor.cpp
bittorrent/torrentimpl.cpp
bittorrent/torrentinfo.cpp

View File

@@ -26,7 +26,7 @@
* exception statement from your version.
*/
#include "torrentcreatorthread.h"
#include "torrentcreator.h"
#include <fstream>
@@ -74,35 +74,34 @@ namespace
using namespace BitTorrent;
TorrentCreatorThread::TorrentCreatorThread(QObject *parent)
: QThread(parent)
TorrentCreator::TorrentCreator(const TorrentCreatorParams &params, QObject *parent)
: QObject(parent)
, m_params {params}
{
}
TorrentCreatorThread::~TorrentCreatorThread()
{
requestInterruption();
wait();
}
void TorrentCreatorThread::create(const TorrentCreatorParams &params)
{
m_params = params;
start();
}
void TorrentCreatorThread::sendProgressSignal(int currentPieceIdx, int totalPieces)
void TorrentCreator::sendProgressSignal(int currentPieceIdx, int totalPieces)
{
emit updateProgress(static_cast<int>((currentPieceIdx * 100.) / totalPieces));
}
void TorrentCreatorThread::checkInterruptionRequested() const
void TorrentCreator::checkInterruptionRequested() const
{
if (isInterruptionRequested())
throw RuntimeError(tr("Operation aborted"));
}
void TorrentCreatorThread::run()
void TorrentCreator::requestInterruption()
{
m_interruptionRequested.storeRelaxed(1);
}
bool TorrentCreator::isInterruptionRequested() const
{
return m_interruptionRequested.loadRelaxed() != 0;
}
void TorrentCreator::run()
{
emit updateProgress(0);
@@ -225,9 +224,9 @@ void TorrentCreatorThread::run()
}
#ifdef QBT_USES_LIBTORRENT2
int TorrentCreatorThread::calculateTotalPieces(const Path &inputPath, const int pieceSize, const TorrentFormat torrentFormat)
int TorrentCreator::calculateTotalPieces(const Path &inputPath, const int pieceSize, const TorrentFormat torrentFormat)
#else
int TorrentCreatorThread::calculateTotalPieces(const Path &inputPath, const int pieceSize, const bool isAlignmentOptimized, const int paddedFileSizeLimit)
int TorrentCreator::calculateTotalPieces(const Path &inputPath, const int pieceSize, const bool isAlignmentOptimized, const int paddedFileSizeLimit)
#endif
{
if (inputPath.isEmpty())

View File

@@ -28,8 +28,10 @@
#pragma once
#include <QAtomicInt>
#include <QObject>
#include <QRunnable>
#include <QStringList>
#include <QThread>
#include "base/path.h"
@@ -62,16 +64,20 @@ namespace BitTorrent
QStringList urlSeeds;
};
class TorrentCreatorThread final : public QThread
class TorrentCreator final : public QObject, public QRunnable
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(TorrentCreatorThread)
Q_DISABLE_COPY_MOVE(TorrentCreator)
public:
explicit TorrentCreatorThread(QObject *parent = nullptr);
~TorrentCreatorThread() override;
explicit TorrentCreator(const TorrentCreatorParams &params, QObject *parent = nullptr);
void create(const TorrentCreatorParams &params);
void run() override;
bool isInterruptionRequested() const;
public slots:
void requestInterruption();
#ifdef QBT_USES_LIBTORRENT2
static int calculateTotalPieces(const Path &inputPath, int pieceSize, TorrentFormat torrentFormat);
@@ -86,10 +92,10 @@ namespace BitTorrent
void updateProgress(int progress);
private:
void run() override;
void sendProgressSignal(int currentPieceIdx, int totalPieces);
void checkInterruptionRequested() const;
TorrentCreatorParams m_params;
QAtomicInt m_interruptionRequested;
};
}