Don't use dispatch_semaphore in NetworkCacheStorage
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 6 Mar 2015 17:30:32 +0000 (17:30 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 6 Mar 2015 17:30:32 +0000 (17:30 +0000)
https://bugs.webkit.org/show_bug.cgi?id=142395

Reviewed by Anders Carlsson.

* NetworkProcess/cache/NetworkCacheIOChannel.h:
* NetworkProcess/cache/NetworkCacheIOChannelCocoa.mm:
(WebKit::NetworkCache::IOChannel::read):
(WebKit::NetworkCache::IOChannel::readSync):

    Add readSync to encapsulate the semaphore.

* NetworkProcess/cache/NetworkCacheStorageCocoa.mm:
(WebKit::NetworkCache::Storage::traverse):

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

Source/WebKit2/ChangeLog
Source/WebKit2/NetworkProcess/cache/NetworkCacheIOChannel.h
Source/WebKit2/NetworkProcess/cache/NetworkCacheIOChannelCocoa.mm
Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm

index da889b9ce745e8650502bb20294b2372c61c359c..c92f121e3bf91631b8982a735061d20f82fc1c90 100644 (file)
@@ -1,3 +1,20 @@
+2015-03-06  Antti Koivisto  <antti@apple.com>
+
+        Don't use dispatch_semaphore in NetworkCacheStorage
+        https://bugs.webkit.org/show_bug.cgi?id=142395
+
+        Reviewed by Anders Carlsson.
+
+        * NetworkProcess/cache/NetworkCacheIOChannel.h:
+        * NetworkProcess/cache/NetworkCacheIOChannelCocoa.mm:
+        (WebKit::NetworkCache::IOChannel::read):
+        (WebKit::NetworkCache::IOChannel::readSync):
+
+            Add readSync to encapsulate the semaphore.
+
+        * NetworkProcess/cache/NetworkCacheStorageCocoa.mm:
+        (WebKit::NetworkCache::Storage::traverse):
+
 2015-03-06  Antti Koivisto  <antti@apple.com>
 
         Move dispatch_data use behind NetworkCache::Data abstraction
index 1ceda3eec46a589ef8a0f285790b10053433057a..fec4fae6427c396d7b2778bdc688c910c453b35e 100644 (file)
@@ -42,6 +42,7 @@ public:
     static Ref<IOChannel> open(const String& file, Type);
 
     void read(size_t offset, size_t, std::function<void (Data&, int error)>);
+    void readSync(size_t offset, size_t, std::function<void (Data&, int error)>);
     void write(size_t offset, const Data&, std::function<void (int error)>);
 
     int fileDescriptor() const { return m_fileDescriptor; }
index 456119400cf1ecdc208611c10824a6e2254635cf..e8017e9de85a852f379d5ab8d4f7c176e07d2c88 100644 (file)
@@ -75,7 +75,7 @@ Ref<IOChannel> IOChannel::open(const String& filePath, IOChannel::Type type)
     return adoptRef(*new IOChannel(fd));
 }
 
-void IOChannel::read(size_t offset, size_t size, std::function<void ( Data&, int error)> completionHandler)
+void IOChannel::read(size_t offset, size_t size, std::function<void (Data&, int error)> completionHandler)
 {
     RefPtr<IOChannel> channel(this);
     bool didCallCompletionHandler = false;
@@ -95,6 +95,17 @@ void IOChannel::read(size_t offset, size_t size, std::function<void ( Data&, int
     });
 }
 
+// FIXME: It would be better to do without this.
+void IOChannel::readSync(size_t offset, size_t size, std::function<void (Data&, int error)> completionHandler)
+{
+    auto semaphore = adoptDispatch(dispatch_semaphore_create(0));
+    read(offset, size, [semaphore, &completionHandler](Data& data, int error) {
+        completionHandler(data, error);
+        dispatch_semaphore_signal(semaphore.get());
+    });
+    dispatch_semaphore_wait(semaphore.get(), DISPATCH_TIME_FOREVER);
+}
+
 void IOChannel::write(size_t offset, const Data& data, std::function<void (int error)> completionHandler)
 {
     RefPtr<IOChannel> channel(this);
index f9da869b84e151483ddec3b91fe39e646143d83f..0a1d198ec10c9121d413f4052478fdddf6733608 100644 (file)
@@ -32,7 +32,6 @@
 #include "NetworkCacheCoders.h"
 #include "NetworkCacheFileSystemPosix.h"
 #include "NetworkCacheIOChannel.h"
-#include <dispatch/dispatch.h>
 #include <wtf/PageBlock.h>
 #include <wtf/RandomNumber.h>
 #include <wtf/RunLoop.h>
@@ -409,21 +408,19 @@ void Storage::traverse(std::function<void (const Entry*)>&& traverseHandler)
     StringCapture cachePathCapture(m_directoryPath);
     ioQueue().dispatch([this, cachePathCapture, traverseHandler] {
         String cachePath = cachePathCapture.string();
-        auto semaphore = adoptDispatch(dispatch_semaphore_create(0));
-        traverseCacheFiles(cachePath, [this, &semaphore, &traverseHandler](const String& fileName, const String& partitionPath) {
+        traverseCacheFiles(cachePath, [this, &traverseHandler](const String& fileName, const String& partitionPath) {
             auto filePath = WebCore::pathByAppendingComponent(partitionPath, fileName);
             auto channel = IOChannel::open(filePath, IOChannel::Type::Read);
             const size_t headerReadSize = 16 << 10;
-            channel->read(0, headerReadSize, [this, &semaphore, &traverseHandler](Data& fileData, int) {
+            // FIXME: Traversal is slower than it should be due to lack of parallelism.
+            channel->readSync(0, headerReadSize, [this, &traverseHandler](Data& fileData, int) {
                 EntryMetaData metaData;
                 Data headerData;
                 if (decodeEntryHeader(fileData, metaData, headerData)) {
                     Entry entry { metaData.key, metaData.timeStamp, headerData, { } };
                     traverseHandler(&entry);
                 }
-                dispatch_semaphore_signal(semaphore.get());
             });
-            dispatch_semaphore_wait(semaphore.get(), DISPATCH_TIME_FOREVER);
         });
         RunLoop::main().dispatch([this, traverseHandler] {
             traverseHandler(nullptr);