Merge pull request #6934 from riking/runonobject
QtUtils/RunOnObject: Make safe under object destruction
This commit is contained in:
commit
302093d777
|
@ -34,7 +34,7 @@
|
||||||
|
|
||||||
static bool QtMsgAlertHandler(const char* caption, const char* text, bool yes_no, MsgType style)
|
static bool QtMsgAlertHandler(const char* caption, const char* text, bool yes_no, MsgType style)
|
||||||
{
|
{
|
||||||
return RunOnObject(QApplication::instance(), [&] {
|
std::optional<bool> r = RunOnObject(QApplication::instance(), [&] {
|
||||||
QMessageBox message_box(QApplication::activeWindow());
|
QMessageBox message_box(QApplication::activeWindow());
|
||||||
message_box.setWindowTitle(QString::fromUtf8(caption));
|
message_box.setWindowTitle(QString::fromUtf8(caption));
|
||||||
message_box.setText(QString::fromUtf8(text));
|
message_box.setText(QString::fromUtf8(text));
|
||||||
|
@ -68,6 +68,9 @@ static bool QtMsgAlertHandler(const char* caption, const char* text, bool yes_no
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
if (r.has_value())
|
||||||
|
return *r;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// N.B. On Windows, this should be called from WinMain. Link against qtmain and specify
|
// N.B. On Windows, this should be called from WinMain. Link against qtmain and specify
|
||||||
|
|
|
@ -1196,13 +1196,16 @@ void MainWindow::OnImportNANDBackup()
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[this] {
|
[this] {
|
||||||
return RunOnObject(this, [this] {
|
std::optional<std::string> keys_file = RunOnObject(this, [this] {
|
||||||
return QFileDialog::getOpenFileName(this, tr("Select the keys file (OTP/SEEPROM dump)"),
|
return QFileDialog::getOpenFileName(this, tr("Select the keys file (OTP/SEEPROM dump)"),
|
||||||
QDir::currentPath(),
|
QDir::currentPath(),
|
||||||
tr("BootMii keys file (*.bin);;"
|
tr("BootMii keys file (*.bin);;"
|
||||||
"All Files (*)"))
|
"All Files (*)"))
|
||||||
.toStdString();
|
.toStdString();
|
||||||
});
|
});
|
||||||
|
if (keys_file)
|
||||||
|
return *keys_file;
|
||||||
|
return std::string("");
|
||||||
});
|
});
|
||||||
QueueOnObject(dialog, &QProgressDialog::close);
|
QueueOnObject(dialog, &QProgressDialog::close);
|
||||||
});
|
});
|
||||||
|
|
|
@ -580,12 +580,15 @@ void NetPlayDialog::OnTraversalError(TraversalClient::FailureReason error)
|
||||||
|
|
||||||
bool NetPlayDialog::IsRecording()
|
bool NetPlayDialog::IsRecording()
|
||||||
{
|
{
|
||||||
return RunOnObject(m_record_input_box, &QCheckBox::isChecked);
|
std::optional<bool> is_recording = RunOnObject(m_record_input_box, &QCheckBox::isChecked);
|
||||||
|
if (is_recording)
|
||||||
|
return *is_recording;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string NetPlayDialog::FindGame(const std::string& game)
|
std::string NetPlayDialog::FindGame(const std::string& game)
|
||||||
{
|
{
|
||||||
return RunOnObject(this, [this, game] {
|
std::optional<std::string> path = RunOnObject(this, [this, game] {
|
||||||
for (int i = 0; i < m_game_list_model->rowCount(QModelIndex()); i++)
|
for (int i = 0; i < m_game_list_model->rowCount(QModelIndex()); i++)
|
||||||
{
|
{
|
||||||
if (m_game_list_model->GetUniqueIdentifier(i).toStdString() == game)
|
if (m_game_list_model->GetUniqueIdentifier(i).toStdString() == game)
|
||||||
|
@ -593,6 +596,9 @@ std::string NetPlayDialog::FindGame(const std::string& game)
|
||||||
}
|
}
|
||||||
return std::string("");
|
return std::string("");
|
||||||
});
|
});
|
||||||
|
if (path)
|
||||||
|
return *path;
|
||||||
|
return std::string("");
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetPlayDialog::ShowMD5Dialog(const std::string& file_identifier)
|
void NetPlayDialog::ShowMD5Dialog(const std::string& file_identifier)
|
||||||
|
|
|
@ -4,7 +4,11 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QEvent>
|
||||||
|
#include <QPointer>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <optional>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
@ -15,21 +19,53 @@ class QObject;
|
||||||
|
|
||||||
// QWidget and subclasses are not thread-safe! This helper takes arbitrary code from any thread,
|
// QWidget and subclasses are not thread-safe! This helper takes arbitrary code from any thread,
|
||||||
// safely runs it on the appropriate GUI thread, waits for it to finish, and returns the result.
|
// safely runs it on the appropriate GUI thread, waits for it to finish, and returns the result.
|
||||||
|
//
|
||||||
|
// If the target object is destructed before the code gets to run, the QPointer will be nulled and
|
||||||
|
// the function will return nullopt.
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
auto RunOnObject(QObject* object, F&& functor)
|
auto RunOnObject(QObject* object, F&& functor)
|
||||||
{
|
{
|
||||||
|
using OptionalResultT = std::optional<std::result_of_t<F()>>;
|
||||||
|
|
||||||
// If we queue up a functor on the current thread, it won't run until we return to the event loop,
|
// If we queue up a functor on the current thread, it won't run until we return to the event loop,
|
||||||
// which means waiting for it to finish will never complete. Instead, run it immediately.
|
// which means waiting for it to finish will never complete. Instead, run it immediately.
|
||||||
if (object->thread() == QThread::currentThread())
|
if (object->thread() == QThread::currentThread())
|
||||||
return functor();
|
return OptionalResultT(functor());
|
||||||
|
|
||||||
Common::Event event;
|
class FnInvokeEvent : public QEvent
|
||||||
std::result_of_t<F()> result;
|
{
|
||||||
QueueOnObject(object, [&event, &result, functor = std::forward<F>(functor) ] {
|
public:
|
||||||
result = functor();
|
FnInvokeEvent(F&& functor, QObject* obj, Common::Event& event, OptionalResultT& result)
|
||||||
event.Set();
|
: QEvent(QEvent::None), m_func(std::move(functor)), m_obj(obj), m_event(event),
|
||||||
});
|
m_result(result)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~FnInvokeEvent()
|
||||||
|
{
|
||||||
|
if (m_obj)
|
||||||
|
{
|
||||||
|
(*m_result) = m_func();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// is already nullopt
|
||||||
|
}
|
||||||
|
m_event.Set();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
F m_func;
|
||||||
|
QPointer<QObject> m_obj;
|
||||||
|
Common::Event& m_event;
|
||||||
|
OptionalResultT& m_result;
|
||||||
|
};
|
||||||
|
|
||||||
|
Common::Event event{};
|
||||||
|
OptionalResultT result = std::nullopt;
|
||||||
|
QCoreApplication::postEvent(object,
|
||||||
|
new FnInvokeEvent(std::forward<F>(functor), object, event, result));
|
||||||
event.Wait();
|
event.Wait();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ void Updater::OnUpdateAvailable(const NewVersionInformation& info)
|
||||||
{
|
{
|
||||||
bool later = false;
|
bool later = false;
|
||||||
|
|
||||||
int choice = RunOnObject(m_parent, [&] {
|
std::optional<int> choice = RunOnObject(m_parent, [&] {
|
||||||
QDialog* dialog = new QDialog(m_parent);
|
QDialog* dialog = new QDialog(m_parent);
|
||||||
dialog->setWindowTitle(tr("Update available"));
|
dialog->setWindowTitle(tr("Update available"));
|
||||||
dialog->setWindowFlags(dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
dialog->setWindowFlags(dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||||
|
@ -79,7 +79,7 @@ void Updater::OnUpdateAvailable(const NewVersionInformation& info)
|
||||||
return dialog->exec();
|
return dialog->exec();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (choice == QDialog::Accepted)
|
if (choice && *choice == QDialog::Accepted)
|
||||||
{
|
{
|
||||||
TriggerUpdate(info, later ? AutoUpdateChecker::RestartMode::NO_RESTART_AFTER_UPDATE :
|
TriggerUpdate(info, later ? AutoUpdateChecker::RestartMode::NO_RESTART_AFTER_UPDATE :
|
||||||
AutoUpdateChecker::RestartMode::RESTART_AFTER_UPDATE);
|
AutoUpdateChecker::RestartMode::RESTART_AFTER_UPDATE);
|
||||||
|
|
Loading…
Reference in New Issue