mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:57:44 +00:00
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they can safely be used in the Kernel. This includes obtaining a strong reference from a weak reference, which now requires an explicit call to WeakPtr::strong_ref(). Another major change is that Weakable::make_weak_ref() may require the explicit target type. Previously we used reinterpret_cast in WeakPtr, assuming that it can be properly converted. But WeakPtr does not necessarily have the knowledge to be able to do this. Instead, we now ask the class itself to deliver a WeakPtr to the type that we want. Also, WeakLink is no longer specific to a target type. The reason for this is that we want to be able to safely convert e.g. WeakPtr<T> to WeakPtr<U>, and before this we just reinterpret_cast the internal WeakLink<T> to WeakLink<U>, which is a bold assumption that it would actually produce the correct code. Instead, WeakLink now operates on just a raw pointer and we only make those constructors/operators available if we can verify that it can be safely cast. In order to guarantee thread safety, we now use the least significant bit in the pointer for locking purposes. This also means that only properly aligned pointers can be used.
This commit is contained in:
parent
3c1ef744f6
commit
75f61fe3d9
50 changed files with 819 additions and 322 deletions
|
@ -40,4 +40,32 @@ ChildEvent::~ChildEvent()
|
|||
{
|
||||
}
|
||||
|
||||
Object* ChildEvent::child()
|
||||
{
|
||||
if (auto ref = m_child.strong_ref())
|
||||
return ref.ptr();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const Object* ChildEvent::child() const
|
||||
{
|
||||
if (auto ref = m_child.strong_ref())
|
||||
return ref.ptr();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Object* ChildEvent::insertion_before_child()
|
||||
{
|
||||
if (auto ref = m_insertion_before_child.strong_ref())
|
||||
return ref.ptr();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const Object* ChildEvent::insertion_before_child() const
|
||||
{
|
||||
if (auto ref = m_insertion_before_child.strong_ref())
|
||||
return ref.ptr();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -130,11 +130,11 @@ public:
|
|||
ChildEvent(Type, Object& child, Object* insertion_before_child = nullptr);
|
||||
~ChildEvent();
|
||||
|
||||
Object* child() { return m_child.ptr(); }
|
||||
const Object* child() const { return m_child.ptr(); }
|
||||
Object* child();
|
||||
const Object* child() const;
|
||||
|
||||
Object* insertion_before_child() { return m_insertion_before_child.ptr(); }
|
||||
const Object* insertion_before_child() const { return m_insertion_before_child.ptr(); }
|
||||
Object* insertion_before_child();
|
||||
const Object* insertion_before_child() const;
|
||||
|
||||
private:
|
||||
WeakPtr<Object> m_child;
|
||||
|
|
|
@ -122,8 +122,8 @@ public:
|
|||
}
|
||||
virtual ~RPCClient() override
|
||||
{
|
||||
if (m_inspected_object)
|
||||
m_inspected_object->decrement_inspector_count({});
|
||||
if (auto inspected_object = m_inspected_object.strong_ref())
|
||||
inspected_object->decrement_inspector_count({});
|
||||
}
|
||||
|
||||
void send_response(const JsonObject& response)
|
||||
|
@ -177,10 +177,10 @@ public:
|
|||
auto address = request.get("address").to_number<FlatPtr>();
|
||||
for (auto& object : Object::all_objects()) {
|
||||
if ((FlatPtr)&object == address) {
|
||||
if (m_inspected_object)
|
||||
m_inspected_object->decrement_inspector_count({});
|
||||
m_inspected_object = object.make_weak_ptr();
|
||||
m_inspected_object->increment_inspector_count({});
|
||||
if (auto inspected_object = m_inspected_object.strong_ref())
|
||||
inspected_object->decrement_inspector_count({});
|
||||
m_inspected_object = object;
|
||||
object.increment_inspector_count({});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -364,7 +364,7 @@ void EventLoop::pump(WaitMode mode)
|
|||
|
||||
for (size_t i = 0; i < events.size(); ++i) {
|
||||
auto& queued_event = events.at(i);
|
||||
auto* receiver = queued_event.receiver.ptr();
|
||||
auto receiver = queued_event.receiver.strong_ref();
|
||||
auto& event = *queued_event.event;
|
||||
#ifdef EVENTLOOP_DEBUG
|
||||
if (receiver)
|
||||
|
@ -639,15 +639,16 @@ try_select_again:
|
|||
auto& timer = *it.value;
|
||||
if (!timer.has_expired(now))
|
||||
continue;
|
||||
if (it.value->fire_when_not_visible == TimerShouldFireWhenNotVisible::No
|
||||
&& it.value->owner
|
||||
&& !it.value->owner->is_visible_for_timer_purposes()) {
|
||||
auto owner = timer.owner.strong_ref();
|
||||
if (timer.fire_when_not_visible == TimerShouldFireWhenNotVisible::No
|
||||
&& owner && !owner->is_visible_for_timer_purposes()) {
|
||||
continue;
|
||||
}
|
||||
#ifdef EVENTLOOP_DEBUG
|
||||
dbgln("Core::EventLoop: Timer {} has expired, sending Core::TimerEvent to {}", timer.timer_id, timer.owner);
|
||||
dbgln("Core::EventLoop: Timer {} has expired, sending Core::TimerEvent to {}", timer.timer_id, *owner);
|
||||
#endif
|
||||
post_event(*timer.owner, make<TimerEvent>(timer.timer_id));
|
||||
if (owner)
|
||||
post_event(*owner, make<TimerEvent>(timer.timer_id));
|
||||
if (timer.should_reload) {
|
||||
timer.reload(now);
|
||||
} else {
|
||||
|
@ -688,9 +689,9 @@ Optional<struct timeval> EventLoop::get_next_timer_expiration()
|
|||
Optional<struct timeval> soonest {};
|
||||
for (auto& it : *s_timers) {
|
||||
auto& fire_time = it.value->fire_time;
|
||||
auto owner = it.value->owner.strong_ref();
|
||||
if (it.value->fire_when_not_visible == TimerShouldFireWhenNotVisible::No
|
||||
&& it.value->owner
|
||||
&& !it.value->owner->is_visible_for_timer_purposes()) {
|
||||
&& owner && !owner->is_visible_for_timer_purposes()) {
|
||||
continue;
|
||||
}
|
||||
if (!soonest.has_value() || fire_time.tv_sec < soonest.value().tv_sec || (fire_time.tv_sec == soonest.value().tv_sec && fire_time.tv_usec < soonest.value().tv_usec))
|
||||
|
@ -703,7 +704,7 @@ int EventLoop::register_timer(Object& object, int milliseconds, bool should_relo
|
|||
{
|
||||
ASSERT(milliseconds >= 0);
|
||||
auto timer = make<EventLoopTimer>();
|
||||
timer->owner = object.make_weak_ptr();
|
||||
timer->owner = object;
|
||||
timer->interval = milliseconds;
|
||||
timeval now;
|
||||
timespec now_spec;
|
||||
|
@ -750,7 +751,7 @@ void EventLoop::wake()
|
|||
}
|
||||
|
||||
EventLoop::QueuedEvent::QueuedEvent(Object& receiver, NonnullOwnPtr<Event> event)
|
||||
: receiver(receiver.make_weak_ptr())
|
||||
: receiver(receiver)
|
||||
, event(move(event))
|
||||
{
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ Application* Application::the()
|
|||
Application::Application(int argc, char** argv)
|
||||
{
|
||||
ASSERT(!*s_the);
|
||||
*s_the = make_weak_ptr();
|
||||
*s_the = *this;
|
||||
m_event_loop = make<Core::EventLoop>();
|
||||
WindowServerConnection::the();
|
||||
Clipboard::initialize({});
|
||||
|
|
|
@ -129,7 +129,7 @@ void Button::context_menu_event(ContextMenuEvent& context_menu_event)
|
|||
|
||||
void Button::set_action(Action& action)
|
||||
{
|
||||
m_action = action.make_weak_ptr();
|
||||
m_action = action;
|
||||
action.register_button({}, *this);
|
||||
set_enabled(action.is_enabled());
|
||||
set_checkable(action.is_checkable());
|
||||
|
|
|
@ -82,7 +82,7 @@ void Layout::notify_adopted(Badge<Widget>, Widget& widget)
|
|||
{
|
||||
if (m_owner == &widget)
|
||||
return;
|
||||
m_owner = widget.make_weak_ptr();
|
||||
m_owner = widget;
|
||||
}
|
||||
|
||||
void Layout::notify_disowned(Badge<Widget>, Widget& widget)
|
||||
|
@ -117,7 +117,7 @@ void Layout::add_widget(Widget& widget)
|
|||
{
|
||||
Entry entry;
|
||||
entry.type = Entry::Type::Widget;
|
||||
entry.widget = widget.make_weak_ptr();
|
||||
entry.widget = widget;
|
||||
add_entry(move(entry));
|
||||
}
|
||||
|
||||
|
@ -125,7 +125,7 @@ void Layout::insert_widget_before(Widget& widget, Widget& before_widget)
|
|||
{
|
||||
Entry entry;
|
||||
entry.type = Entry::Type::Widget;
|
||||
entry.widget = widget.make_weak_ptr();
|
||||
entry.widget = widget;
|
||||
m_entries.insert_before_matching(move(entry), [&](auto& existing_entry) {
|
||||
return existing_entry.type == Entry::Type::Widget && existing_entry.widget.ptr() == &before_widget;
|
||||
});
|
||||
|
|
|
@ -162,7 +162,7 @@ int Menu::realize_menu(RefPtr<Action> default_action)
|
|||
}
|
||||
}
|
||||
all_menus().set(m_menu_id, this);
|
||||
m_last_default_action = default_action ? default_action->make_weak_ptr() : nullptr;
|
||||
m_last_default_action = default_action;
|
||||
return m_menu_id;
|
||||
}
|
||||
|
||||
|
|
|
@ -124,8 +124,8 @@ void Splitter::mousedown_event(MouseEvent& event)
|
|||
if (!get_resize_candidates_at(event.position(), first, second))
|
||||
return;
|
||||
|
||||
m_first_resizee = first->make_weak_ptr();
|
||||
m_second_resizee = second->make_weak_ptr();
|
||||
m_first_resizee = *first;
|
||||
m_second_resizee = *second;
|
||||
m_first_resizee_start_size = first->size();
|
||||
m_second_resizee_start_size = second->size();
|
||||
m_resize_origin = event.position();
|
||||
|
|
|
@ -128,7 +128,7 @@ void SyntaxHighlighter::highlight_matching_token_pair()
|
|||
void SyntaxHighlighter::attach(TextEditor& editor)
|
||||
{
|
||||
ASSERT(!m_editor);
|
||||
m_editor = editor.make_weak_ptr();
|
||||
m_editor = editor;
|
||||
}
|
||||
|
||||
void SyntaxHighlighter::detach()
|
||||
|
|
|
@ -533,10 +533,7 @@ void Widget::set_focus_proxy(Widget* proxy)
|
|||
if (m_focus_proxy == proxy)
|
||||
return;
|
||||
|
||||
if (proxy)
|
||||
m_focus_proxy = proxy->make_weak_ptr();
|
||||
else
|
||||
m_focus_proxy = nullptr;
|
||||
m_focus_proxy = proxy;
|
||||
}
|
||||
|
||||
FocusPolicy Widget::focus_policy() const
|
||||
|
|
|
@ -297,7 +297,7 @@ void Window::handle_mouse_event(MouseEvent& event)
|
|||
ASSERT(result.widget);
|
||||
set_hovered_widget(result.widget);
|
||||
if (event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
|
||||
m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr();
|
||||
m_automatic_cursor_tracking_widget = *result.widget;
|
||||
if (result.widget != m_global_cursor_tracking_widget.ptr())
|
||||
return result.widget->dispatch_event(*local_event, this);
|
||||
return;
|
||||
|
@ -562,7 +562,7 @@ void Window::set_focused_widget(Widget* widget, FocusSource source)
|
|||
Core::EventLoop::current().post_event(*m_focused_widget, make<FocusEvent>(Event::FocusOut, source));
|
||||
m_focused_widget->update();
|
||||
}
|
||||
m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
|
||||
m_focused_widget = widget;
|
||||
if (m_focused_widget) {
|
||||
Core::EventLoop::current().post_event(*m_focused_widget, make<FocusEvent>(Event::FocusIn, source));
|
||||
m_focused_widget->update();
|
||||
|
@ -573,14 +573,14 @@ void Window::set_global_cursor_tracking_widget(Widget* widget)
|
|||
{
|
||||
if (widget == m_global_cursor_tracking_widget)
|
||||
return;
|
||||
m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
|
||||
m_global_cursor_tracking_widget = widget;
|
||||
}
|
||||
|
||||
void Window::set_automatic_cursor_tracking_widget(Widget* widget)
|
||||
{
|
||||
if (widget == m_automatic_cursor_tracking_widget)
|
||||
return;
|
||||
m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
|
||||
m_automatic_cursor_tracking_widget = widget;
|
||||
}
|
||||
|
||||
void Window::set_has_alpha_channel(bool value)
|
||||
|
@ -621,7 +621,7 @@ void Window::set_hovered_widget(Widget* widget)
|
|||
if (m_hovered_widget)
|
||||
Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave));
|
||||
|
||||
m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
|
||||
m_hovered_widget = widget;
|
||||
|
||||
if (m_hovered_widget)
|
||||
Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter));
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
namespace Protocol {
|
||||
|
||||
Download::Download(Client& client, i32 download_id)
|
||||
: m_client(client.make_weak_ptr())
|
||||
: m_client(client)
|
||||
, m_download_id(download_id)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -290,7 +290,7 @@ Color IdentifierStyleValue::to_color(const DOM::Document& document) const
|
|||
ImageStyleValue::ImageStyleValue(const URL& url, DOM::Document& document)
|
||||
: StyleValue(Type::Image)
|
||||
, m_url(url)
|
||||
, m_document(document.make_weak_ptr())
|
||||
, m_document(document)
|
||||
{
|
||||
LoadRequest request;
|
||||
request.set_url(url);
|
||||
|
|
|
@ -234,7 +234,7 @@ String Document::title() const
|
|||
|
||||
void Document::attach_to_frame(Badge<Frame>, Frame& frame)
|
||||
{
|
||||
m_frame = frame.make_weak_ptr();
|
||||
m_frame = frame;
|
||||
for_each_in_subtree([&](auto& node) {
|
||||
node.document_did_attach_to_frame(frame);
|
||||
return IterationDecision::Continue;
|
||||
|
@ -584,10 +584,7 @@ void Document::set_focused_element(Element* element)
|
|||
if (m_focused_element == element)
|
||||
return;
|
||||
|
||||
if (element)
|
||||
m_focused_element = element->make_weak_ptr();
|
||||
else
|
||||
m_focused_element = nullptr;
|
||||
m_focused_element = element;
|
||||
|
||||
if (m_layout_root)
|
||||
m_layout_root->set_needs_display();
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
namespace Web::HTML {
|
||||
|
||||
CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement& element)
|
||||
: m_element(element.make_weak_ptr())
|
||||
: m_element(element)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ HTMLScriptElement::~HTMLScriptElement()
|
|||
|
||||
void HTMLScriptElement::set_parser_document(Badge<HTMLDocumentParser>, DOM::Document& document)
|
||||
{
|
||||
m_parser_document = document.make_weak_ptr();
|
||||
m_parser_document = document;
|
||||
}
|
||||
|
||||
void HTMLScriptElement::set_non_blocking(Badge<HTMLDocumentParser>, bool non_blocking)
|
||||
|
@ -82,12 +82,12 @@ void HTMLScriptElement::prepare_script(Badge<HTMLDocumentParser>)
|
|||
// FIXME: Check the "type" and "language" attributes
|
||||
|
||||
if (parser_document) {
|
||||
m_parser_document = parser_document->make_weak_ptr();
|
||||
m_parser_document = *parser_document;
|
||||
m_non_blocking = false;
|
||||
}
|
||||
|
||||
m_already_started = true;
|
||||
m_preparation_time_document = document().make_weak_ptr();
|
||||
m_preparation_time_document = document();
|
||||
|
||||
if (parser_document && parser_document.ptr() != m_preparation_time_document.ptr()) {
|
||||
return;
|
||||
|
|
|
@ -40,7 +40,7 @@ Frame::Frame(DOM::Element& host_element, Frame& main_frame)
|
|||
, m_main_frame(main_frame)
|
||||
, m_loader(*this)
|
||||
, m_event_handler({}, *this)
|
||||
, m_host_element(host_element.make_weak_ptr())
|
||||
, m_host_element(host_element)
|
||||
{
|
||||
setup();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue