Update WorkQueue::concurrentApply() to take a WTF::Function instead of an std::function
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 9 Jun 2017 17:54:16 +0000 (17:54 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 9 Jun 2017 17:54:16 +0000 (17:54 +0000)
https://bugs.webkit.org/show_bug.cgi?id=173165

Reviewed by Saam Barati.

Update WorkQueue::concurrentApply() to take a WTF::Function instead of an std::function
as std::function has issues with regards to thread safety.

* wtf/WorkQueue.h:
* wtf/cocoa/WorkQueueCocoa.cpp:
(WTF::WorkQueue::concurrentApply):

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

Source/WTF/ChangeLog
Source/WTF/wtf/WorkQueue.cpp
Source/WTF/wtf/WorkQueue.h
Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp

index 4d0bd5b..f50e049 100644 (file)
@@ -1,3 +1,17 @@
+2017-06-09  Chris Dumez  <cdumez@apple.com>
+
+        Update WorkQueue::concurrentApply() to take a WTF::Function instead of an std::function
+        https://bugs.webkit.org/show_bug.cgi?id=173165
+
+        Reviewed by Saam Barati.
+
+        Update WorkQueue::concurrentApply() to take a WTF::Function instead of an std::function
+        as std::function has issues with regards to thread safety.
+
+        * wtf/WorkQueue.h:
+        * wtf/cocoa/WorkQueueCocoa.cpp:
+        (WTF::WorkQueue::concurrentApply):
+
 2017-06-08  Xabier Rodriguez Calvar  <calvaris@igalia.com>
 
         MediaTime class has rounding issues in different platforms
index 9730fcf..f449f9b 100644 (file)
@@ -29,6 +29,7 @@
 #include <mutex>
 #include <wtf/Condition.h>
 #include <wtf/Deque.h>
+#include <wtf/Function.h>
 #include <wtf/Lock.h>
 #include <wtf/NeverDestroyed.h>
 #include <wtf/NumberOfCores.h>
@@ -55,7 +56,7 @@ WorkQueue::~WorkQueue()
 }
 
 #if !PLATFORM(COCOA)
-void WorkQueue::concurrentApply(size_t iterations, const std::function<void (size_t index)>& function)
+void WorkQueue::concurrentApply(size_t iterations, WTF::Function<void (size_t index)>&& function)
 {
     if (!iterations)
         return;
@@ -82,7 +83,7 @@ void WorkQueue::concurrentApply(size_t iterations, const std::function<void (siz
 
         size_t workerCount() const { return m_workers.size(); }
 
-        void dispatch(const std::function<void ()>* function)
+        void dispatch(const WTF::Function<void ()>* function)
         {
             LockHolder holder(m_lock);
 
@@ -94,7 +95,7 @@ void WorkQueue::concurrentApply(size_t iterations, const std::function<void (siz
         NO_RETURN void threadBody()
         {
             while (true) {
-                const std::function<void ()>* function;
+                const WTF::Function<void ()>* function;
 
                 {
                     LockHolder holder(m_lock);
@@ -112,7 +113,7 @@ void WorkQueue::concurrentApply(size_t iterations, const std::function<void (siz
 
         Lock m_lock;
         Condition m_condition;
-        Deque<const std::function<void ()>*> m_queue;
+        Deque<const WTF::Function<void ()>*> m_queue;
 
         Vector<RefPtr<Thread>> m_workers;
     };
@@ -132,7 +133,7 @@ void WorkQueue::concurrentApply(size_t iterations, const std::function<void (siz
     Condition condition;
     Lock lock;
 
-    std::function<void ()> applier = [&] {
+    WTF::Function<void ()> applier = [&, function = WTFMove(function)] {
         size_t index;
 
         // Call the function for as long as there are iterations left.
index 76345c0..23198d4 100644 (file)
@@ -69,7 +69,7 @@ public:
     WTF_EXPORT_PRIVATE void dispatch(Function<void()>&&) override;
     WTF_EXPORT_PRIVATE void dispatchAfter(Seconds, Function<void()>&&);
 
-    WTF_EXPORT_PRIVATE static void concurrentApply(size_t iterations, const std::function<void(size_t index)>&);
+    WTF_EXPORT_PRIVATE static void concurrentApply(size_t iterations, WTF::Function<void(size_t index)>&&);
 
 #if USE(COCOA_EVENT_LOOP)
     dispatch_queue_t dispatchQueue() const { return m_dispatchQueue; }
index 59ee1f8..f1589f6 100644 (file)
@@ -100,11 +100,11 @@ void WorkQueue::platformInvalidate()
     dispatch_release(m_dispatchQueue);
 }
 
-void WorkQueue::concurrentApply(size_t iterations, const std::function<void(size_t index)>& function)
+void WorkQueue::concurrentApply(size_t iterations, WTF::Function<void(size_t index)>&& function)
 {
-    dispatch_apply(iterations, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
+    dispatch_apply(iterations, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), BlockPtr<void(size_t index)>::fromCallable([function = WTFMove(function)](size_t index) {
         function(index);
-    });
+    }).get());
 }
 
 }