Mantid
Loading...
Searching...
No Matches
FindFilesThreadPoolManager.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
15
16#include <Poco/File.h>
17#include <QCoreApplication>
18#include <QSharedPointer>
19#include <boost/algorithm/string.hpp>
20#include <utility>
21
22using namespace Mantid::Kernel;
23using namespace Mantid::API;
24using namespace MantidQt::API;
25
26// Thread pool to run file finder workers on
27static std::unique_ptr<QThreadPool> tp = {nullptr};
28
33const std::unique_ptr<QThreadPool> &FindFilesThreadPoolManager::poolInstance() const {
34 if (!tp)
35 tp = std::make_unique<QThreadPool>();
36 return tp;
37}
38
48
50 : m_workerAllocator([](const FindFilesSearchParameters &parameters) { return new FindFilesWorker(parameters); }) {}
51
61
62void FindFilesThreadPoolManager::createWorker(const QObject *parent, const FindFilesSearchParameters &parameters) {
64
65 // if parent is null then don't do anything as there will be no
66 // object listening for the search result
67 if (!parent)
68 return;
69
70 auto worker = m_workerAllocator(parameters);
71 connectWorker(parent, worker);
72
73 // pass ownership to the thread pool
74 // we do not need to worry about deleting worker
75 poolInstance()->start(worker);
76}
77
88void FindFilesThreadPoolManager::connectWorker(const QObject *parent, const FindFilesWorker *worker) {
89 parent->connect(worker, SIGNAL(finished(const FindFilesSearchResults &)), parent,
90 SLOT(inspectThreadResult(const FindFilesSearchResults &)), Qt::QueuedConnection);
91
92 parent->connect(worker, SIGNAL(finished(const FindFilesSearchResults &)), parent, SIGNAL(fileFindingFinished()),
93 Qt::QueuedConnection);
94
95 this->connect(this, SIGNAL(disconnectWorkers()), worker, SLOT(disconnectWorker()), Qt::QueuedConnection);
96}
97
104 // Just disconnect any signals from the worker. We leave the worker to
105 // continue running in the background because 1) terminating it directly
106 // is dangerous (we have no idea what it's currently doing from here) and 2)
107 // waiting for it to finish before starting a new thread locks up the GUI
108 // event loop.
109 emit disconnectWorkers();
110 QCoreApplication::sendPostedEvents();
111}
112
117bool FindFilesThreadPoolManager::isSearchRunning() const { return poolInstance()->activeThreadCount() > 0; }
118
static std::unique_ptr< QThreadPool > tp
std::function< FindFilesWorker *(const FindFilesSearchParameters &)> ThreadAllocator
void connectWorker(const QObject *parent, const FindFilesWorker *worker)
Connect worker to relevant signals/slots.
static void destroyThreadPool()
Destroy the static thread pool instance.
FindFilesThreadPoolManager()
Create a new thread pool manager for finding files.
const std::unique_ptr< QThreadPool > & poolInstance() const
Get a handle to the static file finder thread pool instance.
void setAllocator(ThreadAllocator allocator)
Set the worker object allocator for this thread pool.
void waitForDone()
Block execution and wait for all threads to finish processing.
ThreadAllocator m_workerAllocator
Handle to the allocator function for creating new worker threads.
void createWorker(const QObject *parent, const FindFilesSearchParameters &parameters)
Create a new worker thread. This will cancel any currently running threads.
bool isSearchRunning() const
Check if a search is already in progress.
void cancelWorker()
Cancel the currently running worker.
A class to allow the asynchronous finding of files.
POD struct to hold details about the parameters of a file search.
POD struct to hold details about the results of a file search.