From 75f61fe3d9055b4e65deecc796401ea2f9e827b2 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 29 Sep 2020 16:26:13 -0600 Subject: 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 to WeakPtr, and before this we just reinterpret_cast the internal WeakLink to WeakLink, 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. --- Libraries/LibGUI/Window.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Libraries/LibGUI/Window.cpp') diff --git a/Libraries/LibGUI/Window.cpp b/Libraries/LibGUI/Window.cpp index 50e44912bb..6faaa5a08a 100644 --- a/Libraries/LibGUI/Window.cpp +++ b/Libraries/LibGUI/Window.cpp @@ -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(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(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::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::Enter)); -- cgit v1.2.3