[webkit-changes] [WebKit/WebKit] 4b51a0: Introduce ThreadSafeWeakPtr

EWS noreply at github.com
Fri Nov 18 12:42:27 PST 2022


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: 4b51a0df4d5cf653afd99c08091c1887dce58913
      https://github.com/WebKit/WebKit/commit/4b51a0df4d5cf653afd99c08091c1887dce58913
  Author: Alex Christensen <achristensen at webkit.org>
  Date:   2022-11-18 (Fri, 18 Nov 2022)

  Changed paths:
    M Source/JavaScriptCore/wasm/WasmInstance.h
    M Source/JavaScriptCore/wasm/WasmMemory.cpp
    M Source/JavaScriptCore/wasm/WasmMemory.h
    M Source/WTF/WTF.xcodeproj/project.pbxproj
    M Source/WTF/wtf/CMakeLists.txt
    M Source/WTF/wtf/MainThread.h
    M Source/WTF/wtf/ThreadSafeRefCounted.h
    A Source/WTF/wtf/ThreadSafeWeakPtr.h
    M Source/WebGPU/WebGPU/Device.h
    M Source/WebGPU/WebGPU/Device.mm
    M Source/WebKit/NetworkProcess/cocoa/NetworkDataTaskCocoa.mm
    M Source/WebKit/NetworkProcess/storage/OriginStorageManager.cpp
    M Source/WebKit/NetworkProcess/storage/QuotaManager.h
    M Source/WebKit/UIProcess/Cocoa/SOAuthorization/NavigationSOAuthorizationSession.h
    M Source/WebKit/UIProcess/Cocoa/SOAuthorization/PopUpSOAuthorizationSession.mm
    M Source/WebKit/UIProcess/Cocoa/SOAuthorization/SOAuthorizationSession.h
    M Source/WebKit/UIProcess/Cocoa/SOAuthorization/SOAuthorizationSession.mm
    M Source/WebKit/UIProcess/Cocoa/SOAuthorization/SubFrameSOAuthorizationSession.h
    M Source/WebKit/UIProcess/Launcher/ProcessLauncher.h
    M Source/WebKit/UIProcess/Launcher/cocoa/ProcessLauncherCocoa.mm
    M Source/WebKit/UIProcess/Notifications/WebNotification.h
    M Source/WebKit/UIProcess/Notifications/WebNotificationManagerProxy.cpp
    M Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp

  Log Message:
  -----------
  Introduce ThreadSafeWeakPtr
https://bugs.webkit.org/show_bug.cgi?id=248051
rdar://102483501

Reviewed by Chris Dumez.

ThreadSafeWeakPtr is similar to std::weak_ptr, but with a few key differences to make it fit into WebKit better.
The first is that sizeof(ThreadSafeWeakPtr<T>) is sizeof(void*) and, at least in libc++'s implementation,
sizeof(std::weak_ptr<T>) is 2*sizeof(void*).  That is because each std::weak_ptr contains a pointer to the object
and a pointer to the control block containing the strong and weak reference counts.  Instead of that, we give the
object and the control blocks pointers to each other.  By doing that, we get the ability to make a Ref/RefPtr
from only a pointer to the object, so we can continue to use Ref/RefPtr with types that have thread safe weak pointers.

Another difference between ThreadSafeWeakPtr and std::weak_ptr is that ThreadSafeWeakPtr allows you to create a weak
pointer from a raw pointer, which is often used to make a ThreadSafeWeakPtr from *this without an unnecessary
ref/deref cycle.  It does this safely by release asserting that the object is still alive when this happens,
which has half the cost of a ref/deref cycle.

We also support DestructionThread like we do in ThreadSafeRefCounted, and we have double-delete protection like
have have in ThreadSafeRefCountedBase::derefBase by setting ThreadSafeWeakPtrControlBlock.m_object to null when
we delete the object, so that if something makes a Ref to the object during its destructor execution, it won't
free the memory twice.

This is just the initial introduction of this class.  I still need to make WeakHashSet work with ThreadSafeWeakPtr
and update the 17 more classes that inherit from both ThreadSafeRefCounted and CanMakeWeakPtr.

ThreadSafeWeakPtr is a different kind of smart pointer than WeakPtr because all you can do with it is get a RefPtr,
which requires that the types be reference counted.  We have many types that inherit from CanMakeWeakPtr that are not
reference counted, and we don't want to change that with this PR.  I could use some crazy SFINAE to make WeakPtr
just return a different type when it's thread safe, but that is a lot of work for little benefit to get a pointer type
that has the same name but still behaves quite differently internally and in its API, so I decided to make a different
smart pointer type.

* Source/JavaScriptCore/wasm/WasmInstance.h:
(JSC::Wasm::Instance::setMemory):
* Source/JavaScriptCore/wasm/WasmMemory.cpp:
(JSC::Wasm::Memory::growShared):
(JSC::Wasm::Memory::registerInstance):
* Source/JavaScriptCore/wasm/WasmMemory.h:
* Source/WTF/WTF.xcodeproj/project.pbxproj:
* Source/WTF/wtf/CMakeLists.txt:
* Source/WTF/wtf/MainThread.h:
* Source/WTF/wtf/ThreadSafeRefCounted.h:
* Source/WTF/wtf/ThreadSafeWeakPtr.h: Added.
(WTF::ThreadSafeWeakPtrControlBlock::ref const):
(WTF::ThreadSafeWeakPtrControlBlock::deref const):
(WTF::ThreadSafeWeakPtrControlBlock::strongRef const):
(WTF::ThreadSafeWeakPtrControlBlock::strongDeref const):
(WTF::ThreadSafeWeakPtrControlBlock::makeStrongReferenceIfPossible const):
(WTF::ThreadSafeWeakPtrControlBlock::refCount const):
(WTF::ThreadSafeWeakPtrControlBlock::ThreadSafeWeakPtrControlBlock):
(WTF::ThreadSafeWeakPtrControlBlock::WTF_GUARDED_BY_LOCK):
(WTF::ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr::ref const):
(WTF::ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr::deref const):
(WTF::ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr::refCount const):
(WTF::ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr::ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr):
(WTF::ThreadSafeWeakPtr::ThreadSafeWeakPtr):
(WTF::ThreadSafeWeakPtr::operator=):
(WTF::ThreadSafeWeakPtr::get const):
* Source/WebGPU/WebGPU/Device.h:
* Source/WebGPU/WebGPU/Device.mm:
(WebGPU::Device::Device):
* Source/WebKit/NetworkProcess/cocoa/NetworkDataTaskCocoa.mm:
(WebKit::NetworkDataTaskCocoa::willPerformHTTPRedirection):
* Source/WebKit/NetworkProcess/storage/OriginStorageManager.cpp:
(WebKit::OriginStorageManager::idbStorageManager):
* Source/WebKit/NetworkProcess/storage/QuotaManager.h:
* Source/WebKit/UIProcess/Cocoa/SOAuthorization/NavigationSOAuthorizationSession.h:
* Source/WebKit/UIProcess/Cocoa/SOAuthorization/PopUpSOAuthorizationSession.mm:
(-[WKSOSecretDelegate initWithSession:]):
(-[WKSOSecretDelegate webViewDidClose:]):
(-[WKSOSecretDelegate webView:didFinishNavigation:]):
(WebKit::PopUpSOAuthorizationSession::initSecretWebView):
* Source/WebKit/UIProcess/Cocoa/SOAuthorization/SOAuthorizationSession.h:
* Source/WebKit/UIProcess/Cocoa/SOAuthorization/SOAuthorizationSession.mm:
(WebKit::SOAuthorizationSession::start):
(WebKit::SOAuthorizationSession::continueStartAfterGetAuthorizationHints):
(WebKit::SOAuthorizationSession::complete):
(WebKit::SOAuthorizationSession::presentViewController):
* Source/WebKit/UIProcess/Cocoa/SOAuthorization/SubFrameSOAuthorizationSession.h:
* Source/WebKit/UIProcess/Launcher/ProcessLauncher.h:
* Source/WebKit/UIProcess/Launcher/cocoa/ProcessLauncherCocoa.mm:
(WebKit::ProcessLauncher::launchProcess):
* Source/WebKit/UIProcess/Notifications/WebNotification.h:
(WebKit::WebNotification::sourceConnection const):
* Source/WebKit/UIProcess/Notifications/WebNotificationManagerProxy.cpp:
(WebKit::WebNotificationManagerProxy::providerDidShowNotification):
(WebKit::dispatchDidClickNotification):
(WebKit::WebNotificationManagerProxy::providerDidCloseNotifications):
* Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp:
(TestWebKitAPI::ThreadSafeInstanceCounter::ThreadSafeInstanceCounter):
(TestWebKitAPI::ThreadSafeInstanceCounter::~ThreadSafeInstanceCounter):
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/256848@main




More information about the webkit-changes mailing list