mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-21 05:09:34 -06:00
QtUtils/RunOnObject: Make safe under object destruction
Co-Authored-By: cancel <cancel@cancel.fm>
This commit is contained in:
@ -4,7 +4,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QEvent>
|
||||
#include <QPointer>
|
||||
#include <QThread>
|
||||
#include <optional>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
@ -15,21 +19,53 @@ class QObject;
|
||||
|
||||
// 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.
|
||||
//
|
||||
// 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>
|
||||
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,
|
||||
// which means waiting for it to finish will never complete. Instead, run it immediately.
|
||||
if (object->thread() == QThread::currentThread())
|
||||
return functor();
|
||||
return OptionalResultT(functor());
|
||||
|
||||
Common::Event event;
|
||||
std::result_of_t<F()> result;
|
||||
QueueOnObject(object, [&event, &result, functor = std::forward<F>(functor) ] {
|
||||
result = functor();
|
||||
event.Set();
|
||||
});
|
||||
class FnInvokeEvent : public QEvent
|
||||
{
|
||||
public:
|
||||
FnInvokeEvent(F&& functor, QObject* obj, Common::Event& event, OptionalResultT& result)
|
||||
: 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();
|
||||
return result;
|
||||
}
|
||||
|
Reference in New Issue
Block a user