Title: [278393] trunk/Source/WebCore
Revision
278393
Author
sihui_...@apple.com
Date
2021-06-02 21:57:45 -0700 (Wed, 02 Jun 2021)

Log Message

Introduce a shared IDB serialization thread
https://bugs.webkit.org/show_bug.cgi?id=226228
<rdar://problem/78725516>

Reviewed by Chris Dumez.

Currently each WebIDBServer has its own thread, and does serialization/deserialization on that thread. To
prepare for making WebIDBServer use WorkQueue, like the other storage manager classes, let's create a shared
thread for handling all serialization/deserialization operations, which cannot be done on WorkQueue as VM needs
to access thread-specific data. Now IDB tasks will call callOnIDBSerializationThreadAndWait to perform
serialization/deserialization operations synchronously.

* Modules/indexeddb/server/IDBBackingStore.h:
* Modules/indexeddb/server/IDBSerializationContext.cpp: Removed. Moved to IDBBindingUtilities.cpp as
IDBSerializationContext is only used by IDBSerialization thread now.
* Modules/indexeddb/server/IDBSerializationContext.h: Removed.
* Modules/indexeddb/server/MemoryIDBBackingStore.cpp:
(WebCore::IDBServer::MemoryIDBBackingStore::MemoryIDBBackingStore):
(WebCore::IDBServer::MemoryIDBBackingStore::serializationContext): Deleted.
* Modules/indexeddb/server/MemoryIDBBackingStore.h:
* Modules/indexeddb/server/MemoryObjectStore.cpp:
(WebCore::IDBServer::MemoryObjectStore::MemoryObjectStore):
(WebCore::IDBServer::MemoryObjectStore::addRecord):
(WebCore::IDBServer::MemoryObjectStore::populateIndexWithExistingRecords):
* Modules/indexeddb/server/MemoryObjectStore.h:
* Modules/indexeddb/server/SQLiteIDBBackingStore.cpp:
(WebCore::IDBServer::SQLiteIDBBackingStore::SQLiteIDBBackingStore):
(WebCore::IDBServer::SQLiteIDBBackingStore::updateOneIndexForAddRecord):
(WebCore::IDBServer::SQLiteIDBBackingStore::serializationContext): Deleted.
* Modules/indexeddb/server/SQLiteIDBBackingStore.h:
* Modules/indexeddb/server/UniqueIDBDatabase.cpp:
(WebCore::IDBServer::UniqueIDBDatabase::putOrAdd):
* Sources.txt:
* bindings/js/IDBBindingUtilities.cpp:
(WebCore::IDBSerializationContext::IDBSerializationContext):
(WebCore::IDBSerializationContext::~IDBSerializationContext):
(WebCore::IDBSerializationContext::globalObject):
(WebCore::IDBSerializationContext::initializeVM):
(WebCore::callOnIDBSerializationThreadAndWait): Create a IDBSerialization thread if not exists, and dispatch a
synchronous task to it.
* bindings/js/IDBBindingUtilities.h:

Modified Paths

Removed Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (278392 => 278393)


--- trunk/Source/WebCore/ChangeLog	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/ChangeLog	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,3 +1,47 @@
+2021-06-02  Sihui Liu  <sihui_...@apple.com>
+
+        Introduce a shared IDB serialization thread
+        https://bugs.webkit.org/show_bug.cgi?id=226228
+        <rdar://problem/78725516>
+
+        Reviewed by Chris Dumez.
+
+        Currently each WebIDBServer has its own thread, and does serialization/deserialization on that thread. To
+        prepare for making WebIDBServer use WorkQueue, like the other storage manager classes, let's create a shared
+        thread for handling all serialization/deserialization operations, which cannot be done on WorkQueue as VM needs 
+        to access thread-specific data. Now IDB tasks will call callOnIDBSerializationThreadAndWait to perform 
+        serialization/deserialization operations synchronously.
+
+        * Modules/indexeddb/server/IDBBackingStore.h:
+        * Modules/indexeddb/server/IDBSerializationContext.cpp: Removed. Moved to IDBBindingUtilities.cpp as 
+        IDBSerializationContext is only used by IDBSerialization thread now.
+        * Modules/indexeddb/server/IDBSerializationContext.h: Removed.
+        * Modules/indexeddb/server/MemoryIDBBackingStore.cpp:
+        (WebCore::IDBServer::MemoryIDBBackingStore::MemoryIDBBackingStore):
+        (WebCore::IDBServer::MemoryIDBBackingStore::serializationContext): Deleted.
+        * Modules/indexeddb/server/MemoryIDBBackingStore.h:
+        * Modules/indexeddb/server/MemoryObjectStore.cpp:
+        (WebCore::IDBServer::MemoryObjectStore::MemoryObjectStore):
+        (WebCore::IDBServer::MemoryObjectStore::addRecord):
+        (WebCore::IDBServer::MemoryObjectStore::populateIndexWithExistingRecords):
+        * Modules/indexeddb/server/MemoryObjectStore.h:
+        * Modules/indexeddb/server/SQLiteIDBBackingStore.cpp:
+        (WebCore::IDBServer::SQLiteIDBBackingStore::SQLiteIDBBackingStore):
+        (WebCore::IDBServer::SQLiteIDBBackingStore::updateOneIndexForAddRecord):
+        (WebCore::IDBServer::SQLiteIDBBackingStore::serializationContext): Deleted.
+        * Modules/indexeddb/server/SQLiteIDBBackingStore.h:
+        * Modules/indexeddb/server/UniqueIDBDatabase.cpp:
+        (WebCore::IDBServer::UniqueIDBDatabase::putOrAdd):
+        * Sources.txt:
+        * bindings/js/IDBBindingUtilities.cpp:
+        (WebCore::IDBSerializationContext::IDBSerializationContext):
+        (WebCore::IDBSerializationContext::~IDBSerializationContext):
+        (WebCore::IDBSerializationContext::globalObject):
+        (WebCore::IDBSerializationContext::initializeVM):
+        (WebCore::callOnIDBSerializationThreadAndWait): Create a IDBSerialization thread if not exists, and dispatch a 
+        synchronous task to it.
+        * bindings/js/IDBBindingUtilities.h:
+
 2021-06-02  Chris Dumez  <cdu...@apple.com>
 
         Port media code to the HTML event loop

Modified: trunk/Source/WebCore/Modules/indexeddb/server/IDBBackingStore.h (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/IDBBackingStore.h	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/IDBBackingStore.h	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2015-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -55,8 +55,6 @@
 
 namespace IDBServer {
 
-class IDBSerializationContext;
-
 class IDBBackingStore {
 public:
     virtual ~IDBBackingStore() { RELEASE_ASSERT(!isMainThread()); }
@@ -88,8 +86,6 @@
     virtual IDBError openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo&, IDBGetResult& outResult) = 0;
     virtual IDBError iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBIterateCursorData&, IDBGetResult& outResult) = 0;
 
-    virtual IDBSerializationContext& serializationContext() = 0;
-
     virtual IDBObjectStoreInfo* infoForObjectStore(uint64_t objectStoreIdentifier) = 0;
     virtual void deleteBackingStore() = 0;
 

Deleted: trunk/Source/WebCore/Modules/indexeddb/server/IDBSerializationContext.cpp (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/IDBSerializationContext.cpp	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/IDBSerializationContext.cpp	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,109 +0,0 @@
-/*
- * Copyright (C) 2019 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "config.h"
-#include "IDBSerializationContext.h"
-
-#include "DOMWrapperWorld.h"
-#include "WebCoreJSClientData.h"
-#include <_javascript_Core/JSObjectInlines.h>
-#include <wtf/Lock.h>
-
-namespace WebCore {
-
-namespace IDBServer {
-
-static Lock serializationContextMapLock;
-
-static HashMap<Thread*, IDBSerializationContext*>& serializationContextMap() WTF_REQUIRES_LOCK(serializationContextMapLock)
-{
-    static NeverDestroyed<HashMap<Thread*, IDBSerializationContext*>> map;
-    return map;
-}
-
-Ref<IDBSerializationContext> IDBSerializationContext::getOrCreateForCurrentThread()
-{
-    auto& thread = Thread::current();
-    Locker locker { serializationContextMapLock };
-    auto[iter, isNewEntry] = serializationContextMap().add(&thread, nullptr);
-    if (isNewEntry) {
-        Ref<IDBSerializationContext> protectedContext = adoptRef(*new IDBSerializationContext(thread));
-        iter->value = protectedContext.ptr();
-        return protectedContext;
-    }
-
-    return *iter->value;
-}
-
-IDBSerializationContext::~IDBSerializationContext()
-{
-    Locker locker { serializationContextMapLock };
-    ASSERT(this == serializationContextMap().get(&m_thread));
-
-    if (m_vm) {
-        JSC::JSLockHolder lock(*m_vm);
-        m_globalObject.clear();
-        m_vm = nullptr;
-    }
-    serializationContextMap().remove(&m_thread);
-}
-
-void IDBSerializationContext::initializeVM()
-{
-    if (m_vm)
-        return;
-
-    ASSERT(!m_globalObject);
-    m_vm = JSC::VM::create();
-    m_vm->heap.acquireAccess();
-    JSVMClientData::initNormalWorld(m_vm.get(), WorkerThreadType::Worklet);
-
-    JSC::JSLockHolder locker(m_vm.get());
-    m_globalObject.set(*m_vm, JSIDBSerializationGlobalObject::create(*m_vm, JSIDBSerializationGlobalObject::createStructure(*m_vm, JSC::jsNull()), normalWorld(*m_vm)));
-}
-
-JSC::VM& IDBSerializationContext::vm()
-{
-    ASSERT(&m_thread == &Thread::current());
-
-    initializeVM();
-    return *m_vm;
-}
-
-JSC::JSGlobalObject& IDBSerializationContext::globalObject()
-{
-    ASSERT(&m_thread == &Thread::current());
-
-    initializeVM();
-    return *m_globalObject.get();
-}
-
-IDBSerializationContext::IDBSerializationContext(Thread& thread)
-    : m_thread(thread)
-{
-}
-
-} // namespace IDBServer
-} // namespace WebCore

Deleted: trunk/Source/WebCore/Modules/indexeddb/server/IDBSerializationContext.h (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/IDBSerializationContext.h	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/IDBSerializationContext.h	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,60 +0,0 @@
-/*
- * Copyright (C) 2019 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include "JSIDBSerializationGlobalObject.h"
-#include <_javascript_Core/StrongInlines.h>
-#include <_javascript_Core/StructureInlines.h>
-
-namespace JSC {
-class CallFrame;
-class VM;
-}
-
-namespace WebCore {
-
-namespace IDBServer {
-
-class IDBSerializationContext : public RefCounted<IDBSerializationContext> {
-public:
-    static Ref<IDBSerializationContext> getOrCreateForCurrentThread();
-
-    ~IDBSerializationContext();
-
-    JSC::VM& vm();
-    JSC::JSGlobalObject& globalObject();
-
-private:
-    explicit IDBSerializationContext(Thread&);
-    void initializeVM();
-
-    RefPtr<JSC::VM> m_vm;
-    JSC::Strong<JSIDBSerializationGlobalObject> m_globalObject;
-    Thread& m_thread;
-};
-
-} // namespace IDBServer
-} // namespace WebCore

Modified: trunk/Source/WebCore/Modules/indexeddb/server/MemoryIDBBackingStore.cpp (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/MemoryIDBBackingStore.cpp	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/MemoryIDBBackingStore.cpp	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2015-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -33,7 +33,6 @@
 #include "IDBIndexInfo.h"
 #include "IDBIterateCursorData.h"
 #include "IDBKeyRangeData.h"
-#include "IDBSerializationContext.h"
 #include "Logging.h"
 #include "MemoryIndexCursor.h"
 #include "MemoryObjectStore.h"
@@ -48,7 +47,6 @@
 MemoryIDBBackingStore::MemoryIDBBackingStore(PAL::SessionID sessionID, const IDBDatabaseIdentifier& identifier)
     : m_identifier(identifier)
     , m_sessionID(sessionID)
-    , m_serializationContext(IDBSerializationContext::getOrCreateForCurrentThread())
 {
 }
 
@@ -616,11 +614,6 @@
     return objectStoreByIdentifier;
 }
 
-IDBSerializationContext& MemoryIDBBackingStore::serializationContext()
-{
-    return m_serializationContext.get();
-}
-
 IDBObjectStoreInfo* MemoryIDBBackingStore::infoForObjectStore(uint64_t objectStoreIdentifier)
 {
     ASSERT(m_databaseInfo);

Modified: trunk/Source/WebCore/Modules/indexeddb/server/MemoryIDBBackingStore.h (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/MemoryIDBBackingStore.h	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/MemoryIDBBackingStore.h	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2015-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -36,7 +36,6 @@
 namespace WebCore {
 namespace IDBServer {
 
-class IDBSerializationContext;
 class MemoryObjectStore;
 
 class MemoryIDBBackingStore final : public IDBBackingStore {
@@ -52,8 +51,6 @@
     void removeObjectStoreForVersionChangeAbort(MemoryObjectStore&);
     void restoreObjectStoreForVersionChangeAbort(Ref<MemoryObjectStore>&&);
 
-    IDBSerializationContext& serializationContext() final;
-
 private:
     IDBError beginTransaction(const IDBTransactionInfo&) final;
     IDBError abortTransaction(const IDBResourceIdentifier& transactionIdentifier) final;
@@ -95,7 +92,6 @@
 
     IDBDatabaseIdentifier m_identifier;
     PAL::SessionID m_sessionID;
-    Ref<IDBSerializationContext> m_serializationContext;
     std::unique_ptr<IDBDatabaseInfo> m_databaseInfo;
 
     HashMap<IDBResourceIdentifier, std::unique_ptr<MemoryBackingStoreTransaction>> m_transactions;

Modified: trunk/Source/WebCore/Modules/indexeddb/server/MemoryObjectStore.cpp (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/MemoryObjectStore.cpp	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/MemoryObjectStore.cpp	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2015-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,7 +30,6 @@
 #include "IDBError.h"
 #include "IDBGetAllResult.h"
 #include "IDBKeyRangeData.h"
-#include "IDBSerializationContext.h"
 #include "IDBValue.h"
 #include "IndexKey.h"
 #include "Logging.h"
@@ -52,7 +51,6 @@
 
 MemoryObjectStore::MemoryObjectStore(PAL::SessionID, const IDBObjectStoreInfo& info)
     : m_info(info)
-    , m_serializationContext(IDBSerializationContext::getOrCreateForCurrentThread())
 {
 }
 
@@ -251,7 +249,10 @@
 
 IDBError MemoryObjectStore::addRecord(MemoryBackingStoreTransaction& transaction, const IDBKeyData& keyData, const IDBValue& value)
 {
-    auto indexKeys = generateIndexKeyMapForValue(m_serializationContext->globalObject(), m_info, keyData, value);
+    IndexIDToIndexKeyMap indexKeys;
+    callOnIDBSerializationThreadAndWait([info = m_info.isolatedCopy(), keyData = keyData.isolatedCopy(), value = value.isolatedCopy(), &indexKeys](auto& globalObject) {
+        indexKeys = generateIndexKeyMapForValue(globalObject, info, keyData, value);
+    });
     return addRecord(transaction, keyData, indexKeys, value);
 }
 
@@ -338,20 +339,25 @@
     if (!m_keyValueStore)
         return IDBError { };
 
-    JSLockHolder locker(m_serializationContext->vm());
+    for (const auto& iterator : *m_keyValueStore) {
+        std::optional<IndexKey> resultIndexKey;
+        callOnIDBSerializationThreadAndWait([key = iterator.key.isolatedCopy(), value = iterator.value, info = m_info.isolatedCopy(), indexInfo = index.info().isolatedCopy(), &resultIndexKey](auto& globalObject) {
+            auto jsValue = deserializeIDBValueToJSValue(globalObject, value);
+            if (jsValue.isUndefinedOrNull())
+                return;
 
-    for (const auto& iterator : *m_keyValueStore) {
-        auto jsValue = deserializeIDBValueToJSValue(m_serializationContext->globalObject(), iterator.value);
-        if (jsValue.isUndefinedOrNull())
+            IndexKey indexKey;
+            generateIndexKeyForValue(globalObject, indexInfo, jsValue, indexKey, info.keyPath(), key);
+            resultIndexKey = indexKey.isolatedCopy();
+        });
+
+        if (!resultIndexKey)
             return IDBError { };
 
-        IndexKey indexKey;
-        generateIndexKeyForValue(m_serializationContext->globalObject(), index.info(), jsValue, indexKey, m_info.keyPath(), iterator.key);
-
-        if (indexKey.isNull())
+        if (resultIndexKey->isNull())
             continue;
 
-        IDBError error = index.putIndexKey(iterator.key, indexKey);
+        IDBError error = index.putIndexKey(iterator.key, *resultIndexKey);
         if (!error.isNull())
             return error;
     }

Modified: trunk/Source/WebCore/Modules/indexeddb/server/MemoryObjectStore.h (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/MemoryObjectStore.h	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/MemoryObjectStore.h	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2015-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -55,7 +55,6 @@
 
 namespace IDBServer {
 
-class IDBSerializationContext;
 class MemoryBackingStoreTransaction;
 
 typedef HashMap<IDBKeyData, ThreadSafeDataBuffer, IDBKeyDataHash, IDBKeyDataHashTraits> KeyValueMap;
@@ -133,8 +132,6 @@
     HashMap<uint64_t, RefPtr<MemoryIndex>> m_indexesByIdentifier;
     HashMap<String, RefPtr<MemoryIndex>> m_indexesByName;
     HashMap<IDBResourceIdentifier, std::unique_ptr<MemoryObjectStoreCursor>> m_cursors;
-
-    Ref<IDBSerializationContext> m_serializationContext;
 };
 
 } // namespace IDBServer

Modified: trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -36,7 +36,6 @@
 #include "IDBKeyData.h"
 #include "IDBObjectStoreInfo.h"
 #include "IDBSerialization.h"
-#include "IDBSerializationContext.h"
 #include "IDBTransactionInfo.h"
 #include "IDBValue.h"
 #include "IndexKey.h"
@@ -241,7 +240,6 @@
     : m_sessionID(sessionID)
     , m_identifier(identifier)
     , m_databaseRootDirectory(databaseRootDirectory)
-    , m_serializationContext(IDBSerializationContext::getOrCreateForCurrentThread())
 {
     m_databaseDirectory = fullDatabaseDirectoryWithUpgrade();
 }
@@ -1886,19 +1884,24 @@
 
 IDBError SQLiteIDBBackingStore::updateOneIndexForAddRecord(IDBObjectStoreInfo& objectStoreInfo, const IDBIndexInfo& info, const IDBKeyData& key, const ThreadSafeDataBuffer& value, int64_t recordID)
 {
-    JSLockHolder locker(m_serializationContext->vm());
+    std::optional<IndexKey> resultIndexKey;
+    callOnIDBSerializationThreadAndWait([objectStoreInfo = objectStoreInfo.isolatedCopy(), info = info.isolatedCopy(), key = key.isolatedCopy(), value, &resultIndexKey](auto& globalObject) {
+        auto jsValue = deserializeIDBValueToJSValue(globalObject, value);
+        if (jsValue.isUndefinedOrNull())
+            return;
 
-    auto jsValue = deserializeIDBValueToJSValue(m_serializationContext->globalObject(), value);
-    if (jsValue.isUndefinedOrNull())
+        IndexKey indexKey;
+        generateIndexKeyForValue(globalObject, info, jsValue, indexKey, objectStoreInfo.keyPath(), key);
+        resultIndexKey = indexKey.isolatedCopy();
+    });
+
+    if (!resultIndexKey)
         return IDBError { };
 
-    IndexKey indexKey;
-    generateIndexKeyForValue(m_serializationContext->globalObject(), info, jsValue, indexKey, objectStoreInfo.keyPath(), key);
-
-    if (indexKey.isNull())
+    if (resultIndexKey->isNull())
         return IDBError { };
 
-    return uncheckedPutIndexKey(info, key, indexKey, recordID);
+    return uncheckedPutIndexKey(info, key, *resultIndexKey, recordID);
 }
 
 IDBError SQLiteIDBBackingStore::updateAllIndexesForAddRecord(const IDBObjectStoreInfo& info, const IDBKeyData& key, const IndexIDToIndexKeyMap& indexKeys, int64_t recordID)
@@ -2745,11 +2748,6 @@
     return IDBError { };
 }
 
-IDBSerializationContext& SQLiteIDBBackingStore::serializationContext()
-{
-    return m_serializationContext.get();
-}
-
 IDBObjectStoreInfo* SQLiteIDBBackingStore::infoForObjectStore(uint64_t objectStoreIdentifier)
 {
     ASSERT(m_databaseInfo);

Modified: trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -46,7 +46,6 @@
 
 enum class IsSchemaUpgraded : bool { No, Yes };
 
-class IDBSerializationContext;
 class SQLiteIDBCursor;
 
 class SQLiteIDBBackingStore final : public IDBBackingStore {
@@ -82,8 +81,6 @@
     IDBError openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo&, IDBGetResult& outResult) final;
     IDBError iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBIterateCursorData&, IDBGetResult& outResult) final;
 
-    IDBSerializationContext& serializationContext() final;
-
     IDBObjectStoreInfo* infoForObjectStore(uint64_t objectStoreIdentifier) final;
     void deleteBackingStore() final;
 
@@ -221,8 +218,6 @@
 
     String m_databaseRootDirectory;
     String m_databaseDirectory;
-
-    Ref<IDBSerializationContext> m_serializationContext;
 };
 
 } // namespace IDBServer

Modified: trunk/Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.cpp (278392 => 278393)


--- trunk/Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.cpp	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.cpp	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015, 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2015-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -34,7 +34,7 @@
 #include "IDBIterateCursorData.h"
 #include "IDBKeyRangeData.h"
 #include "IDBResultData.h"
-#include "IDBSerializationContext.h"
+#include "IDBSerialization.h"
 #include "IDBServer.h"
 #include "IDBTransactionInfo.h"
 #include "IDBValue.h"
@@ -744,7 +744,10 @@
         usedKey = keyData;
 
     // Generate index keys up front for more accurate quota check.
-    auto indexKeys = generateIndexKeyMapForValue(m_backingStore->serializationContext().globalObject(), *objectStoreInfo, usedKey, value);
+    IndexIDToIndexKeyMap indexKeys;
+    callOnIDBSerializationThreadAndWait([objectStoreInfo = objectStoreInfo->isolatedCopy(), key = usedKey.isolatedCopy(), value = value.isolatedCopy(), &indexKeys](auto& globalObject) {
+        indexKeys = generateIndexKeyMapForValue(globalObject, objectStoreInfo, key, value);
+    });
 
     if (overwriteMode == IndexedDB::ObjectStoreOverwriteMode::NoOverwrite) {
         bool keyExists;

Modified: trunk/Source/WebCore/Sources.txt (278392 => 278393)


--- trunk/Source/WebCore/Sources.txt	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/Sources.txt	2021-06-03 04:57:45 UTC (rev 278393)
@@ -94,7 +94,6 @@
 Modules/indexeddb/client/TransactionOperation.cpp
 Modules/indexeddb/server/IDBConnectionToClient.cpp
 Modules/indexeddb/server/IDBSerialization.cpp
-Modules/indexeddb/server/IDBSerializationContext.cpp
 Modules/indexeddb/server/IDBServer.cpp
 Modules/indexeddb/server/IndexValueEntry.cpp
 Modules/indexeddb/server/IndexValueStore.cpp

Modified: trunk/Source/WebCore/bindings/js/IDBBindingUtilities.cpp (278392 => 278393)


--- trunk/Source/WebCore/bindings/js/IDBBindingUtilities.cpp	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/bindings/js/IDBBindingUtilities.cpp	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,7 +1,7 @@
 /*
  * Copyright (C) 2010 Google Inc. All rights reserved.
  * Copyright (C) 2012 Michael Pruett <mich...@68k.org>
- * Copyright (C) 2014-2020 Apple Inc. All rights reserved.
+ * Copyright (C) 2014-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -43,6 +43,7 @@
 #include "JSDOMConvertNullable.h"
 #include "JSDOMExceptionHandling.h"
 #include "JSFile.h"
+#include "JSIDBSerializationGlobalObject.h"
 #include "Logging.h"
 #include "MessagePort.h"
 #include "ScriptExecutionContext.h"
@@ -49,9 +50,14 @@
 #include "SerializedScriptValue.h"
 #include "SharedBuffer.h"
 #include "ThreadSafeDataBuffer.h"
+#include "WebCoreJSClientData.h"
 #include <_javascript_Core/ArrayBuffer.h>
 #include <_javascript_Core/DateInstance.h>
 #include <_javascript_Core/ObjectConstructor.h>
+#include <_javascript_Core/StrongInlines.h>
+#include <wtf/AutodrainedPool.h>
+#include <wtf/MessageQueue.h>
+#include <wtf/threads/BinarySemaphore.h>
 
 namespace WebCore {
 using namespace JSC;
@@ -519,4 +525,74 @@
     return jsValue;
 }
 
+class IDBSerializationContext {
+public:
+    IDBSerializationContext()
+        : m_thread(Thread::current())
+    {
+    }
+
+    ~IDBSerializationContext()
+    {
+        ASSERT(&m_thread == &Thread::current());
+        if (!m_vm)
+            return;
+
+        JSC::JSLockHolder lock(*m_vm);
+        m_globalObject.clear();
+        m_vm = nullptr;
+    }
+
+    JSC::JSGlobalObject& globalObject()
+    {
+        ASSERT(&m_thread == &Thread::current());
+
+        initializeVM();
+        return *m_globalObject.get();
+    }
+
+private:
+    void initializeVM()
+    {
+        if (m_vm)
+            return;
+
+        ASSERT(!m_globalObject);
+        m_vm = JSC::VM::create();
+        m_vm->heap.acquireAccess();
+        JSVMClientData::initNormalWorld(m_vm.get(), WorkerThreadType::Worklet);
+
+        JSC::JSLockHolder locker(m_vm.get());
+        m_globalObject.set(*m_vm, JSIDBSerializationGlobalObject::create(*m_vm, JSIDBSerializationGlobalObject::createStructure(*m_vm, JSC::jsNull()), normalWorld(*m_vm)));
+    }
+
+    RefPtr<JSC::VM> m_vm;
+    JSC::Strong<JSIDBSerializationGlobalObject> m_globalObject;
+    Thread& m_thread;
+};
+
+void callOnIDBSerializationThreadAndWait(Function<void(JSC::JSGlobalObject&)>&& function)
+{
+    static NeverDestroyed<MessageQueue<Function<void(JSC::JSGlobalObject&)>>> queue;
+    static std::once_flag createThread;
+
+    std::call_once(createThread, [] {
+        Thread::create("IndexedDB Serialization", [] {
+            IDBSerializationContext serializationContext;
+            while (auto function = queue->waitForMessage()) {
+                AutodrainedPool pool;
+                (*function)(serializationContext.globalObject());
+            }
+        });
+    });
+
+    BinarySemaphore semaphore;
+    auto newFuntion = [&semaphore, function = WTFMove(function)](JSC::JSGlobalObject& globalObject) {
+        function(globalObject);
+        semaphore.signal();
+    };
+    queue->append(makeUnique<Function<void(JSC::JSGlobalObject&)>>(WTFMove(newFuntion)));
+    semaphore.wait();
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/bindings/js/IDBBindingUtilities.h (278392 => 278393)


--- trunk/Source/WebCore/bindings/js/IDBBindingUtilities.h	2021-06-03 01:54:35 UTC (rev 278392)
+++ trunk/Source/WebCore/bindings/js/IDBBindingUtilities.h	2021-06-03 04:57:45 UTC (rev 278393)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2010 Google Inc. All rights reserved.
- * Copyright (C) 2014, 2015, 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2014-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -63,4 +63,7 @@
 JSC::JSValue toJS(JSC::JSGlobalObject*, JSDOMGlobalObject*, const IDBKeyData&);
 
 std::optional<JSC::JSValue> deserializeIDBValueWithKeyInjection(JSC::JSGlobalObject&, const IDBValue&, const IDBKeyData&, const std::optional<IDBKeyPath>&);
+
+void callOnIDBSerializationThreadAndWait(Function<void(JSC::JSGlobalObject&)>&&);
+
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to