mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-08 12:19:37 +03:00
bc319d9e88
Okay, I've spent a whole day on this now, and it finally kinda works! With this patch, CObject and all of its derived classes are reference counted instead of tree-owned. The previous, Qt-like model was nice and familiar, but ultimately also outdated and difficult to reason about. CObject-derived types should now be stored in RefPtr/NonnullRefPtr and each class can be constructed using the forwarding construct() helper: auto widget = GWidget::construct(parent_widget); Note that construct() simply forwards all arguments to an existing constructor. It is inserted into each class by the C_OBJECT macro, see CObject.h to understand how that works. CObject::delete_later() disappears in this patch, as there is no longer a single logical owner of a CObject.
72 lines
1.8 KiB
C++
72 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <AK/Function.h>
|
|
#include <AK/NonnullRefPtr.h>
|
|
#include <AK/Optional.h>
|
|
#include <AK/Queue.h>
|
|
#include <LibCore/CEventLoop.h>
|
|
#include <LibCore/CObject.h>
|
|
#include <LibThread/Lock.h>
|
|
#include <LibThread/Thread.h>
|
|
|
|
namespace LibThread {
|
|
|
|
template<typename Result>
|
|
class BackgroundAction;
|
|
|
|
class BackgroundActionBase {
|
|
template<typename Result>
|
|
friend class BackgroundAction;
|
|
|
|
private:
|
|
BackgroundActionBase() {}
|
|
|
|
static Lockable<Queue<Function<void()>>>& all_actions();
|
|
static Thread& background_thread();
|
|
};
|
|
|
|
template<typename Result>
|
|
class BackgroundAction final : public CObject
|
|
, private BackgroundActionBase {
|
|
C_OBJECT(BackgroundAction);
|
|
public:
|
|
static NonnullRefPtr<BackgroundAction<Result>> create(
|
|
Function<Result()> action,
|
|
Function<void(Result)> on_complete = nullptr
|
|
)
|
|
{
|
|
return adopt(*new BackgroundAction(move(action), move(on_complete)));
|
|
}
|
|
|
|
virtual ~BackgroundAction() {}
|
|
|
|
private:
|
|
|
|
BackgroundAction(Function<Result()> action, Function<void(Result)> on_complete)
|
|
: CObject(&background_thread())
|
|
, m_action(move(action))
|
|
, m_on_complete(move(on_complete))
|
|
{
|
|
LOCKER(all_actions().lock());
|
|
|
|
this->ref();
|
|
all_actions().resource().enqueue([this] {
|
|
m_result = m_action();
|
|
if (m_on_complete) {
|
|
CEventLoop::main().post_event(*this, make<CDeferredInvocationEvent>([this](CObject&) {
|
|
m_on_complete(m_result.release_value());
|
|
this->deref();
|
|
}));
|
|
CEventLoop::main().wake();
|
|
} else
|
|
this->deref();
|
|
});
|
|
}
|
|
|
|
Function<Result()> m_action;
|
|
Function<void(Result)> m_on_complete;
|
|
Optional<Result> m_result;
|
|
};
|
|
|
|
}
|