2020-06-11 02:14:54 +00:00
|
|
|
#ifndef FUNCTIONTHREAD_H
|
|
|
|
#define FUNCTIONTHREAD_H
|
|
|
|
|
2020-06-17 02:33:50 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QEvent>
|
2020-06-11 02:14:54 +00:00
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The LambdaThread class
|
|
|
|
*
|
2020-06-17 02:33:50 +00:00
|
|
|
* Provides a QThread to which lambdas can be posted.
|
2020-06-11 02:14:54 +00:00
|
|
|
*
|
|
|
|
* Disclaimer: this might be a crutch that reveals a misunderstanding of the Qt
|
|
|
|
* threading infrastructure. We'll see.
|
|
|
|
*/
|
|
|
|
class FunctionThread: public QThread {
|
|
|
|
public:
|
2020-06-20 00:17:47 +00:00
|
|
|
~FunctionThread() {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2020-06-11 02:14:54 +00:00
|
|
|
void run() override {
|
2020-06-17 02:33:50 +00:00
|
|
|
// Gymnastics here: events posted directly to the QThread will occur on the thread
|
|
|
|
// that created the QThread. To have events occur within a QThread, they have to be
|
|
|
|
// posted to an object created on that thread. FunctionPerformer fills that role.
|
2020-07-01 22:55:42 +00:00
|
|
|
if(!performer) performer = std::make_unique<FunctionPerformer>();
|
2020-06-17 03:12:58 +00:00
|
|
|
performerFlag.clear();
|
2020-06-11 02:14:54 +00:00
|
|
|
exec();
|
|
|
|
}
|
|
|
|
|
2020-06-15 03:38:44 +00:00
|
|
|
void stop() {
|
2020-06-20 00:17:47 +00:00
|
|
|
if(isRunning()) {
|
|
|
|
performAsync([this] {
|
|
|
|
this->quit();
|
|
|
|
});
|
|
|
|
}
|
2020-06-15 04:00:44 +00:00
|
|
|
wait();
|
2020-06-15 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 22:55:42 +00:00
|
|
|
void start() {
|
|
|
|
if(isRunning()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: I've assumed a race condition here with the creation of performer; if QThread
|
|
|
|
// blocks on completion of `run` when starting then this is redundant.
|
|
|
|
performerFlag.test_and_set();
|
|
|
|
QThread::start();
|
|
|
|
while(performerFlag.test_and_set());
|
|
|
|
}
|
|
|
|
|
2020-06-17 02:33:50 +00:00
|
|
|
/*!
|
|
|
|
* \brief Schedules a function to be performed on this thread. Control
|
|
|
|
* must return to the main event loop for the function to be performed;
|
|
|
|
* use QCoreApplication::sendPostedEvents() to ensure the function is
|
|
|
|
* performed before then, if required.
|
|
|
|
*
|
|
|
|
* \param function The function to perform.
|
|
|
|
*/
|
|
|
|
void performAsync(const std::function<void(void)> &function) {
|
|
|
|
QApplication::instance()->postEvent(performer.get(), new FunctionEvent(function));
|
|
|
|
QCoreApplication::sendPostedEvents();
|
|
|
|
}
|
|
|
|
|
2020-06-11 02:14:54 +00:00
|
|
|
private:
|
2020-06-17 02:33:50 +00:00
|
|
|
struct FunctionEvent: public QEvent {
|
|
|
|
FunctionEvent(const std::function<void(void)> &function) : QEvent(QEvent::Type::User), function(function) {}
|
|
|
|
std::function<void(void)> function;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionPerformer: public QObject {
|
|
|
|
FunctionPerformer(): QObject() {}
|
|
|
|
|
|
|
|
bool event(QEvent *event) override {
|
|
|
|
if(event->type() == QEvent::Type::User) {
|
|
|
|
const auto functionEvent = dynamic_cast<FunctionEvent *>(event);
|
|
|
|
if(functionEvent) {
|
|
|
|
functionEvent->function();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QObject::event(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::unique_ptr<FunctionPerformer> performer;
|
2020-06-17 03:12:58 +00:00
|
|
|
std::atomic_flag performerFlag;
|
2020-06-11 02:14:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // FUNCTIONTHREAD_H
|