Make garbage collection of MessagePort objects be asynchronous.
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 21 Jan 2018 07:07:49 +0000 (07:07 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 21 Jan 2018 07:07:49 +0000 (07:07 +0000)
commit88f4e430b67b4730df141c4445f979f8cec03e2a
treef69c4a7f6e38a9abdec69e746238f86d0e1533e1
parent74e43757de95e9c69a221aa8bfd061919b2b066f
Make garbage collection of MessagePort objects be asynchronous.
https://bugs.webkit.org/show_bug.cgi?id=181910

Reviewed by Andy Estes.

No new tests (Covered by existing tests, including GC-specific ones).

The basic premise here is as follows:
- You can *always* GC a MessagePort that is closed
- You can *always* GC a MessagePort that has no onmessage handler, as incoming messages cannot
  possibly revive it.
- You can GC a MessagePort, even if it has a message handler, as long as there are no messages
  in flight between it and the remote port, and as long as the remote port is "maybe eligible for GC."

A MessagePort is considered "maybe eligible for GC" once hasPendingActivity is asked once.

A MessagePort loses "maybe eligible for GC" status once it is used for sending or receiving a message.

The changes to MessagePort.cpp implement the above with a tiny little bool-driven state machine.
* dom/MessagePort.cpp:
(WebCore::MessagePort::postMessage):
(WebCore::MessagePort::disentangle):
(WebCore::MessagePort::registerLocalActivity):
(WebCore::MessagePort::start):
(WebCore::MessagePort::close):
(WebCore::MessagePort::contextDestroyed):
(WebCore::MessagePort::dispatchMessages):
(WebCore::MessagePort::hasPendingActivity const):
(WebCore::MessagePort::isLocallyReachable const):
(WebCore::MessagePort::addEventListener):
(WebCore::MessagePort::removeEventListener):
* dom/MessagePort.h:

- Remove the lock and any background-thread code paths
- Add ASSERT(isMainThread())s throughout
* dom/messageports/MessagePortChannel.cpp:
(WebCore::MessagePortChannel::MessagePortChannel):
(WebCore::MessagePortChannel::includesPort):
(WebCore::MessagePortChannel::entanglePortWithProcess):
(WebCore::MessagePortChannel::disentanglePort):
(WebCore::MessagePortChannel::closePort):
(WebCore::MessagePortChannel::postMessageToRemote):
(WebCore::MessagePortChannel::takeAllMessagesForPort):
(WebCore::MessagePortChannel::checkRemotePortForActivity):
(WebCore::MessagePortChannel::hasAnyMessagesPendingOrInFlight const):
* dom/messageports/MessagePortChannel.h:

Add a callback for a MessagePortChannel to go ask the remote MessagePort object about local activity:
* dom/messageports/MessagePortChannelProvider.h:
* dom/messageports/MessagePortChannelProviderImpl.cpp:
(WebCore::MessagePortChannelProviderImpl::checkRemotePortForActivity):
(WebCore::MessagePortChannelProviderImpl::checkProcessLocalPortForActivity):
(WebCore::MessagePortChannelProviderImpl::hasMessagesForPorts_temporarySync): Deleted.
* dom/messageports/MessagePortChannelProviderImpl.h:

- Remove the lock and any background-thread code paths
- Add ASSERT(isMainThread())s throughout
* dom/messageports/MessagePortChannelRegistry.cpp:
(WebCore::MessagePortChannelRegistry::messagePortChannelCreated):
(WebCore::MessagePortChannelRegistry::messagePortChannelDestroyed):
(WebCore::MessagePortChannelRegistry::didEntangleLocalToRemote):
(WebCore::MessagePortChannelRegistry::didDisentangleMessagePort):
(WebCore::MessagePortChannelRegistry::didCloseMessagePort):
(WebCore::MessagePortChannelRegistry::didPostMessageToRemote):
(WebCore::MessagePortChannelRegistry::takeAllMessagesForPort):
(WebCore::MessagePortChannelRegistry::checkRemotePortForActivity):
(WebCore::MessagePortChannelRegistry::existingChannelContainingPort):
(WebCore::MessagePortChannelRegistry::hasMessagesForPorts_temporarySync): Deleted.
* dom/messageports/MessagePortChannelRegistry.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@227275 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Source/WebCore/ChangeLog
Source/WebCore/dom/MessagePort.cpp
Source/WebCore/dom/MessagePort.h
Source/WebCore/dom/messageports/MessagePortChannel.cpp
Source/WebCore/dom/messageports/MessagePortChannel.h
Source/WebCore/dom/messageports/MessagePortChannelProvider.h
Source/WebCore/dom/messageports/MessagePortChannelProviderImpl.cpp
Source/WebCore/dom/messageports/MessagePortChannelProviderImpl.h
Source/WebCore/dom/messageports/MessagePortChannelRegistry.cpp
Source/WebCore/dom/messageports/MessagePortChannelRegistry.h