Factor out the "databaseTaskQueue" parts of IDBServer into something reusable.
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 2 Dec 2017 18:00:19 +0000 (18:00 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 2 Dec 2017 18:00:19 +0000 (18:00 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180298

Reviewed by Chris Dumez.

Source/WebCore:

No new tests (Refactor only).

* Modules/indexeddb/server/IDBServer.cpp:
(WebCore::IDBServer::IDBServer::IDBServer):
(WebCore::IDBServer::IDBServer::postDatabaseTask):
(WebCore::IDBServer::IDBServer::postDatabaseTaskReply):
(WebCore::IDBServer::IDBServer::databaseRunLoop): Deleted.
(WebCore::IDBServer::IDBServer::handleTaskRepliesOnMainThread): Deleted.
* Modules/indexeddb/server/IDBServer.h:
(): Deleted.

Source/WTF:

* WTF.xcodeproj/project.pbxproj:
* wtf/CMakeLists.txt:

* wtf/CrossThreadTaskHandler.cpp: Added.
(WTF::CrossThreadTaskHandler::CrossThreadTaskHandler):
(WTF::CrossThreadTaskHandler::~CrossThreadTaskHandler):
(WTF::CrossThreadTaskHandler::postTask):
(WTF::CrossThreadTaskHandler::postTaskReply):
(WTF::CrossThreadTaskHandler::taskRunLoop):
(WTF::CrossThreadTaskHandler::handleTaskRepliesOnMainThread):
* wtf/CrossThreadTaskHandler.h: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@225450 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/CMakeLists.txt
Source/WTF/wtf/CrossThreadTaskHandler.cpp [new file with mode: 0644]
Source/WTF/wtf/CrossThreadTaskHandler.h [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/Modules/indexeddb/server/IDBServer.cpp
Source/WebCore/Modules/indexeddb/server/IDBServer.h

index 517918a..09be4c4 100644 (file)
@@ -1,3 +1,22 @@
+2017-12-02  Brady Eidson  <beidson@apple.com>
+
+        Factor out the "databaseTaskQueue" parts of IDBServer into something reusable.
+        https://bugs.webkit.org/show_bug.cgi?id=180298
+
+        Reviewed by Chris Dumez.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/CMakeLists.txt:
+
+        * wtf/CrossThreadTaskHandler.cpp: Added.
+        (WTF::CrossThreadTaskHandler::CrossThreadTaskHandler):
+        (WTF::CrossThreadTaskHandler::~CrossThreadTaskHandler):
+        (WTF::CrossThreadTaskHandler::postTask):
+        (WTF::CrossThreadTaskHandler::postTaskReply):
+        (WTF::CrossThreadTaskHandler::taskRunLoop):
+        (WTF::CrossThreadTaskHandler::handleTaskRepliesOnMainThread):
+        * wtf/CrossThreadTaskHandler.h: Added.
+
 2017-12-01  Mark Lam  <mark.lam@apple.com>
 
         Let's scramble ClassInfo pointers in cells.
index 350ba42..2dc2448 100644 (file)
@@ -65,6 +65,7 @@
                2CDED0F318115C85004DBA70 /* RunLoop.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2CDED0F118115C85004DBA70 /* RunLoop.cpp */; };
                3337DB9CE743410FAF076E17 /* StackTrace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 313EDEC9778E49C9BEA91CFC /* StackTrace.cpp */; };
                515F794E1CFC9F4A00CCED93 /* CrossThreadCopier.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 515F794B1CFC9F4A00CCED93 /* CrossThreadCopier.cpp */; };
+               517F82D71FD22F3000DA3DEA /* CrossThreadTaskHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 517F82D51FD22F2F00DA3DEA /* CrossThreadTaskHandler.cpp */; };
                51F1752B1F3D486000C74950 /* PersistentCoders.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51F175261F3D486000C74950 /* PersistentCoders.cpp */; };
                51F1752C1F3D486000C74950 /* PersistentDecoder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51F175271F3D486000C74950 /* PersistentDecoder.cpp */; };
                51F1752D1F3D486000C74950 /* PersistentEncoder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51F175291F3D486000C74950 /* PersistentEncoder.cpp */; };
                515F794D1CFC9F4A00CCED93 /* CrossThreadTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadTask.h; sourceTree = "<group>"; };
                515F79551CFD3A6900CCED93 /* CrossThreadQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadQueue.h; sourceTree = "<group>"; };
                517A53571F5734B700DCDC0A /* Identified.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Identified.h; sourceTree = "<group>"; };
+               517F82D51FD22F2F00DA3DEA /* CrossThreadTaskHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CrossThreadTaskHandler.cpp; sourceTree = "<group>"; };
+               517F82D61FD22F2F00DA3DEA /* CrossThreadTaskHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadTaskHandler.h; sourceTree = "<group>"; };
                5182C22C1F2BC7E60059BA7C /* InstanceCounted.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InstanceCounted.h; sourceTree = "<group>"; };
                51F175241F3D486000C74950 /* PersistentCoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PersistentCoder.h; sourceTree = "<group>"; };
                51F175251F3D486000C74950 /* PersistentCoders.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PersistentCoders.h; sourceTree = "<group>"; };
                                515F794C1CFC9F4A00CCED93 /* CrossThreadCopier.h */,
                                515F79551CFD3A6900CCED93 /* CrossThreadQueue.h */,
                                515F794D1CFC9F4A00CCED93 /* CrossThreadTask.h */,
+                               517F82D51FD22F2F00DA3DEA /* CrossThreadTaskHandler.cpp */,
+                               517F82D61FD22F2F00DA3DEA /* CrossThreadTaskHandler.h */,
                                A8A47273151A825A004123FF /* CryptographicallyRandomNumber.cpp */,
                                A8A47274151A825A004123FF /* CryptographicallyRandomNumber.h */,
                                E15556F318A0CC18006F48FB /* CryptographicUtilities.cpp */,
                                E38C41281EB4E0680042957D /* CPUTime.cpp in Sources */,
                                E38C41251EB4E04C0042957D /* CPUTimeCocoa.mm in Sources */,
                                515F794E1CFC9F4A00CCED93 /* CrossThreadCopier.cpp in Sources */,
+                               517F82D71FD22F3000DA3DEA /* CrossThreadTaskHandler.cpp in Sources */,
                                A8A4739A151A825B004123FF /* CryptographicallyRandomNumber.cpp in Sources */,
                                E15556F518A0CC18006F48FB /* CryptographicUtilities.cpp in Sources */,
                                A8A47439151A825B004123FF /* CString.cpp in Sources */,
                                1469419316EAAF6D0024E146 /* RunLoopTimerCF.cpp in Sources */,
                                1469419916EAB0410024E146 /* SchedulePairCF.cpp in Sources */,
                                1469419716EAAFF80024E146 /* SchedulePairMac.mm in Sources */,
+                               FE85416E1FBE285D008DA5DA /* ScrambledPtr.cpp in Sources */,
                                0F66B28E1DC97BAB004A1D3F /* Seconds.cpp in Sources */,
                                A8A47421151A825B004123FF /* SHA1.cpp in Sources */,
                                5311BD531EA71CAD00525281 /* Signals.cpp in Sources */,
                                1C181C7F1D3078DA00F5FA16 /* TextBreakIterator.cpp in Sources */,
                                1C181C961D30800A00F5FA16 /* TextBreakIteratorInternalICUMac.mm in Sources */,
                                A3E4DD931F3A803400DED0B4 /* TextStream.cpp in Sources */,
-                               FE85416E1FBE285D008DA5DA /* ScrambledPtr.cpp in Sources */,
                                E311FB171F0A568B003C08DE /* ThreadGroup.cpp in Sources */,
                                A8A4744A151A825B004123FF /* Threading.cpp in Sources */,
                                A8A4744E151A825B004123FF /* ThreadingPthreads.cpp in Sources */,
index ff1dc1c..6e0c0ea 100644 (file)
@@ -21,6 +21,7 @@ set(WTF_HEADERS
     Condition.h
     CrossThreadCopier.h
     CrossThreadTask.h
+    CrossThreadTaskHandler.h
     CryptographicUtilities.h
     CryptographicallyRandomNumber.h
     CurrentTime.h
@@ -219,6 +220,7 @@ set(WTF_SOURCES
     ClockType.cpp
     CompilationThread.cpp
     CrossThreadCopier.cpp
+    CrossThreadTaskHandler.cpp
     CryptographicUtilities.cpp
     CryptographicallyRandomNumber.cpp
     CurrentTime.cpp
diff --git a/Source/WTF/wtf/CrossThreadTaskHandler.cpp b/Source/WTF/wtf/CrossThreadTaskHandler.cpp
new file mode 100644 (file)
index 0000000..9b07c5c
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2017 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 "CrossThreadTaskHandler.h"
+
+namespace WTF {
+
+CrossThreadTaskHandler::CrossThreadTaskHandler(const char* threadName)
+{
+    ASSERT(isMainThread());
+    Locker<Lock> locker(m_taskThreadCreationLock);
+    m_thread = Thread::create(threadName, [this] {
+        taskRunLoop();
+    });
+}
+
+CrossThreadTaskHandler::~CrossThreadTaskHandler()
+{
+    ASSERT(isMainThread());
+}
+
+void CrossThreadTaskHandler::postTask(CrossThreadTask&& task)
+{
+    m_taskQueue.append(WTFMove(task));
+}
+
+void CrossThreadTaskHandler::postTaskReply(CrossThreadTask&& task)
+{
+    m_taskReplyQueue.append(WTFMove(task));
+
+    Locker<Lock> locker(m_mainThreadReplyLock);
+    if (m_mainThreadReplyScheduled)
+        return;
+
+    m_mainThreadReplyScheduled = true;
+    callOnMainThread([this] {
+        handleTaskRepliesOnMainThread();
+    });
+}
+
+void CrossThreadTaskHandler::taskRunLoop()
+{
+    ASSERT(!isMainThread());
+    {
+        Locker<Lock> locker(m_taskThreadCreationLock);
+    }
+
+    while (!m_taskQueue.isKilled())
+        m_taskQueue.waitForMessage().performTask();
+}
+
+void CrossThreadTaskHandler::handleTaskRepliesOnMainThread()
+{
+    {
+        Locker<Lock> locker(m_mainThreadReplyLock);
+        m_mainThreadReplyScheduled = false;
+    }
+
+    while (auto task = m_taskReplyQueue.tryGetMessage())
+        task->performTask();
+}
+
+
+} // namespace WTF
diff --git a/Source/WTF/wtf/CrossThreadTaskHandler.h b/Source/WTF/wtf/CrossThreadTaskHandler.h
new file mode 100644 (file)
index 0000000..a01d393
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2017 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 <wtf/CrossThreadQueue.h>
+#include <wtf/CrossThreadTask.h>
+#include <wtf/Lock.h>
+#include <wtf/Threading.h>
+
+namespace WTF {
+
+class RegistrationStore;
+class SQLiteDatabase;
+
+class CrossThreadTaskHandler {
+public:
+    WTF_EXPORT_PRIVATE virtual ~CrossThreadTaskHandler();
+
+protected:
+    WTF_EXPORT_PRIVATE CrossThreadTaskHandler(const char* threadName);
+
+    WTF_EXPORT_PRIVATE void postTask(CrossThreadTask&&);
+    WTF_EXPORT_PRIVATE void postTaskReply(CrossThreadTask&&);
+
+private:
+    void handleTaskRepliesOnMainThread();
+    void taskRunLoop();
+
+    RefPtr<Thread> m_thread { nullptr };
+    Lock m_taskThreadCreationLock;
+    Lock m_mainThreadReplyLock;
+    bool m_mainThreadReplyScheduled { false };
+
+    CrossThreadQueue<CrossThreadTask> m_taskQueue;
+    CrossThreadQueue<CrossThreadTask> m_taskReplyQueue;
+};
+
+} // namespace WTF
+
+using WTF::CrossThreadTaskHandler;
+
index cdc0c30..26cad89 100644 (file)
@@ -1,3 +1,21 @@
+2017-12-02  Brady Eidson  <beidson@apple.com>
+
+        Factor out the "databaseTaskQueue" parts of IDBServer into something reusable.
+        https://bugs.webkit.org/show_bug.cgi?id=180298
+
+        Reviewed by Chris Dumez.
+
+        No new tests (Refactor only).
+
+        * Modules/indexeddb/server/IDBServer.cpp:
+        (WebCore::IDBServer::IDBServer::IDBServer):
+        (WebCore::IDBServer::IDBServer::postDatabaseTask):
+        (WebCore::IDBServer::IDBServer::postDatabaseTaskReply):
+        (WebCore::IDBServer::IDBServer::databaseRunLoop): Deleted.
+        (WebCore::IDBServer::IDBServer::handleTaskRepliesOnMainThread): Deleted.
+        * Modules/indexeddb/server/IDBServer.h:
+        (): Deleted.
+
 2017-12-02  Simon Fraser  <simon.fraser@apple.com>
 
         Add a log channel for viewports
index abc6193..c62d821 100644 (file)
@@ -53,24 +53,17 @@ Ref<IDBServer> IDBServer::create(const String& databaseDirectoryPath, IDBBacking
 }
 
 IDBServer::IDBServer(IDBBackingStoreTemporaryFileHandler& fileHandler)
-    : m_backingStoreTemporaryFileHandler(fileHandler)
+    : CrossThreadTaskHandler("IndexedDatabase Server")
+    , m_backingStoreTemporaryFileHandler(fileHandler)
 {
-    Locker<Lock> locker(m_databaseThreadCreationLock);
-    m_thread = Thread::create("IndexedDatabase Server", [this] {
-        databaseRunLoop();
-    });
 }
 
 IDBServer::IDBServer(const String& databaseDirectoryPath, IDBBackingStoreTemporaryFileHandler& fileHandler)
-    : m_databaseDirectoryPath(databaseDirectoryPath)
+    : CrossThreadTaskHandler("IndexedDatabase Server")
+    , m_databaseDirectoryPath(databaseDirectoryPath)
     , m_backingStoreTemporaryFileHandler(fileHandler)
 {
     LOG(IndexedDB, "IDBServer created at path %s", databaseDirectoryPath.utf8().data());
-
-    Locker<Lock> locker(m_databaseThreadCreationLock);
-    m_thread = Thread::create("IndexedDatabase Server", [this] {
-        databaseRunLoop();
-    });
 }
 
 void IDBServer::registerConnection(IDBConnectionToClient& connection)
@@ -488,43 +481,12 @@ void IDBServer::didGetAllDatabaseNames(uint64_t serverConnectionIdentifier, uint
 
 void IDBServer::postDatabaseTask(CrossThreadTask&& task)
 {
-    m_databaseQueue.append(WTFMove(task));
+    postTask(WTFMove(task));
 }
 
 void IDBServer::postDatabaseTaskReply(CrossThreadTask&& task)
 {
-    m_databaseReplyQueue.append(WTFMove(task));
-
-    Locker<Lock> locker(m_mainThreadReplyLock);
-    if (m_mainThreadReplyScheduled)
-        return;
-
-    m_mainThreadReplyScheduled = true;
-    callOnMainThread([this] {
-        handleTaskRepliesOnMainThread();
-    });
-}
-
-void IDBServer::databaseRunLoop()
-{
-    ASSERT(!isMainThread());
-    {
-        Locker<Lock> locker(m_databaseThreadCreationLock);
-    }
-
-    while (!m_databaseQueue.isKilled())
-        m_databaseQueue.waitForMessage().performTask();
-}
-
-void IDBServer::handleTaskRepliesOnMainThread()
-{
-    {
-        Locker<Lock> locker(m_mainThreadReplyLock);
-        m_mainThreadReplyScheduled = false;
-    }
-
-    while (auto task = m_databaseReplyQueue.tryGetMessage())
-        task->performTask();
+    postTaskReply(WTFMove(task));
 }
 
 static uint64_t generateDeleteCallbackID()
index 924e202..3507421 100644 (file)
@@ -31,8 +31,7 @@
 #include "IDBDatabaseIdentifier.h"
 #include "UniqueIDBDatabase.h"
 #include "UniqueIDBDatabaseConnection.h"
-#include <wtf/CrossThreadQueue.h>
-#include <wtf/CrossThreadTask.h>
+#include <wtf/CrossThreadTaskHandler.h>
 #include <wtf/HashMap.h>
 #include <wtf/Lock.h>
 #include <wtf/Ref.h>
@@ -51,7 +50,7 @@ namespace IDBServer {
 
 class IDBBackingStoreTemporaryFileHandler;
 
-class IDBServer : public RefCounted<IDBServer> {
+class IDBServer : public RefCounted<IDBServer>, public CrossThreadTaskHandler {
 public:
     static Ref<IDBServer> create(IDBBackingStoreTemporaryFileHandler&);
     WEBCORE_EXPORT static Ref<IDBServer> create(const String& databaseDirectoryPath, IDBBackingStoreTemporaryFileHandler&);
@@ -118,20 +117,9 @@ private:
     void performCloseAndDeleteDatabasesForOrigins(const Vector<SecurityOriginData>&, uint64_t callbackID);
     void didPerformCloseAndDeleteDatabases(uint64_t callbackID);
 
-    void databaseRunLoop();
-    void handleTaskRepliesOnMainThread();
-
     HashMap<uint64_t, RefPtr<IDBConnectionToClient>> m_connectionMap;
     HashMap<IDBDatabaseIdentifier, std::unique_ptr<UniqueIDBDatabase>> m_uniqueIDBDatabaseMap;
 
-    RefPtr<Thread> m_thread { nullptr };
-    Lock m_databaseThreadCreationLock;
-    Lock m_mainThreadReplyLock;
-    bool m_mainThreadReplyScheduled { false };
-
-    CrossThreadQueue<CrossThreadTask> m_databaseQueue;
-    CrossThreadQueue<CrossThreadTask> m_databaseReplyQueue;
-
     HashMap<uint64_t, UniqueIDBDatabaseConnection*> m_databaseConnections;
     HashMap<IDBResourceIdentifier, UniqueIDBDatabaseTransaction*> m_transactions;