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
45 if (tp)
46 tp.reset();
47}
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 m_searchIsRunning = true;
77}
78
89void FindFilesThreadPoolManager::connectWorker(const QObject *parent, const FindFilesWorker *worker) {
90 parent->connect(worker, SIGNAL(finished(const FindFilesSearchResults &)), parent,
91 SLOT(inspectThreadResult(const FindFilesSearchResults &)), Qt::QueuedConnection);
92
93 parent->connect(worker, SIGNAL(finished(const FindFilesSearchResults &)), parent, SIGNAL(fileFindingFinished()),
94 Qt::QueuedConnection);
95
96 this->connect(worker, SIGNAL(finished(const FindFilesSearchResults &)), this, SLOT(searchFinished()),
97 Qt::QueuedConnection);
98
99 this->connect(this, SIGNAL(disconnectWorkers()), worker, SLOT(disconnectWorker()), Qt::QueuedConnection);
100}
101
108 // Just disconnect any signals from the worker. We leave the worker to
109 // continue running in the background because 1) terminating it directly
110 // is dangerous (we have no idea what it's currently doing from here) and 2)
111 // waiting for it to finish before starting a new thread locks up the GUI
112 // event loop.
113 emit disconnectWorkers();
114 m_searchIsRunning = false;
115 QCoreApplication::sendPostedEvents();
116}
117
123
131
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.
bool m_searchIsRunning
Flag set if a search is currently running.
void cancelWorker()
Cancel the currently running worker.
void searchFinished()
Mark the search as being finished.
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.