mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-12-16 04:09:39 +00:00
DolphinQt/QtUtils: Simplify RunOnObject and eliminate Common::Event race.
This commit is contained in:
parent
d25742fe8b
commit
5a6fce31b2
@ -3,74 +3,34 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <concepts>
|
||||||
#include <QEvent>
|
#include <functional>
|
||||||
#include <QPointer>
|
|
||||||
#include <QThread>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <type_traits>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "Common/Event.h"
|
#include <QMetaObject>
|
||||||
#include "DolphinQt/QtUtils/QueueOnObject.h"
|
|
||||||
|
|
||||||
class QObject;
|
#include "Common/OneShotEvent.h"
|
||||||
|
|
||||||
// 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
|
// If the target object is destructed before the code gets to run the function will return nullopt.
|
||||||
// the function will return nullopt.
|
|
||||||
|
|
||||||
template <typename F>
|
auto RunOnObject(QObject* object, std::invocable<> auto&& functor)
|
||||||
auto RunOnObject(QObject* object, F&& functor)
|
|
||||||
{
|
{
|
||||||
using OptionalResultT = std::optional<std::invoke_result_t<F>>;
|
Common::OneShotEvent task_complete;
|
||||||
|
std::optional<decltype(functor())> result;
|
||||||
|
|
||||||
// If we queue up a functor on the current thread, it won't run until we return to the event loop,
|
QMetaObject::invokeMethod(
|
||||||
// which means waiting for it to finish will never complete. Instead, run it immediately.
|
object, [&, guard = Common::ScopedSetter{&task_complete}] { result = functor(); });
|
||||||
if (object->thread() == QThread::currentThread())
|
|
||||||
return OptionalResultT(functor());
|
|
||||||
|
|
||||||
class FnInvokeEvent : public QEvent
|
// Wait for the lambda to go out of scope. The result may or may not have been assigned.
|
||||||
{
|
task_complete.Wait();
|
||||||
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Base, typename Type, typename Receiver>
|
template <std::derived_from<QObject> Receiver>
|
||||||
auto RunOnObject(Receiver* obj, Type Base::* func)
|
auto RunOnObject(Receiver* obj, auto Receiver::* func)
|
||||||
{
|
{
|
||||||
return RunOnObject(obj, [obj, func] { return (obj->*func)(); });
|
return RunOnObject(obj, std::bind(func, obj));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user