Add and implement WorkQueue::concurrentApply
https://bugs.webkit.org/show_bug.cgi?id=148488
Reviewed by Geoffrey Garen.
WorkQueue::concurrentApply is modeled after dispatch_apply, and on Cocoa it uses dispatch_apply directly.
For other ports there's a generic concurrentApply implemented using our threading primitives.
* wtf/NeverDestroyed.h:
(WTF::LazyNeverDestroyed::operator->):
* wtf/WorkQueue.cpp:
(WTF::WorkQueue::concurrentApply):
* wtf/WorkQueue.h:
* wtf/cocoa/WorkQueueCocoa.cpp:
(WTF::WorkQueue::concurrentApply):
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@188981 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WTF/ChangeLog b/Source/WTF/ChangeLog
index 51dba4c..cb4df95 100644
--- a/Source/WTF/ChangeLog
+++ b/Source/WTF/ChangeLog
@@ -1,3 +1,21 @@
+2015-08-26 Anders Carlsson <andersca@apple.com>
+
+ Add and implement WorkQueue::concurrentApply
+ https://bugs.webkit.org/show_bug.cgi?id=148488
+
+ Reviewed by Geoffrey Garen.
+
+ WorkQueue::concurrentApply is modeled after dispatch_apply, and on Cocoa it uses dispatch_apply directly.
+ For other ports there's a generic concurrentApply implemented using our threading primitives.
+
+ * wtf/NeverDestroyed.h:
+ (WTF::LazyNeverDestroyed::operator->):
+ * wtf/WorkQueue.cpp:
+ (WTF::WorkQueue::concurrentApply):
+ * wtf/WorkQueue.h:
+ * wtf/cocoa/WorkQueueCocoa.cpp:
+ (WTF::WorkQueue::concurrentApply):
+
2015-08-25 Filip Pizlo <fpizlo@apple.com>
Node::origin should be able to tell you if it's OK to exit
diff --git a/Source/WTF/wtf/NeverDestroyed.h b/Source/WTF/wtf/NeverDestroyed.h
index c7f76d9..1e36a86 100644
--- a/Source/WTF/wtf/NeverDestroyed.h
+++ b/Source/WTF/wtf/NeverDestroyed.h
@@ -94,6 +94,8 @@
operator T&() { return *asPtr(); }
T& get() { return *asPtr(); }
+ T* operator->() { return asPtr(); }
+
private:
typedef typename std::remove_const<T>::type* PointerType;
diff --git a/Source/WTF/wtf/WorkQueue.cpp b/Source/WTF/wtf/WorkQueue.cpp
index 639984c..ac38122 100644
--- a/Source/WTF/wtf/WorkQueue.cpp
+++ b/Source/WTF/wtf/WorkQueue.cpp
@@ -26,7 +26,14 @@
#include "config.h"
#include "WorkQueue.h"
-#include "Ref.h"
+#include <mutex>
+#include <wtf/BinarySemaphore.h>
+#include <wtf/MessageQueue.h>
+#include <wtf/NeverDestroyed.h>
+#include <wtf/NumberOfCores.h>
+#include <wtf/Ref.h>
+#include <wtf/Threading.h>
+#include <wtf/text/WTFString.h>
namespace WTF {
@@ -45,4 +52,105 @@
platformInvalidate();
}
+#if !PLATFORM(COCOA)
+void WorkQueue::concurrentApply(size_t iterations, const std::function<void (size_t index)>& function)
+{
+ if (!iterations)
+ return;
+
+ if (iterations == 1) {
+ function(0);
+ return;
+ }
+
+ class ThreadPool {
+ public:
+ ThreadPool()
+ {
+ // We don't need a thread for the current core.
+ unsigned threadCount = numberOfProcessorCores() - 1;
+
+ m_workers.reserveInitialCapacity(threadCount);
+ for (unsigned i = 0; i < threadCount; ++i) {
+ m_workers.append(createThread(String::format("ThreadPool Worker %u", i).utf8().data(), [this] {
+ threadBody();
+ }));
+ }
+ }
+
+ size_t workerCount() const { return m_workers.size(); }
+
+ void dispatch(const std::function<void ()>* function)
+ {
+ LockHolder holder(m_lock);
+
+ m_queue.append(function);
+ m_condition.notifyOne();
+ }
+
+ private:
+ NO_RETURN void threadBody()
+ {
+ while (true) {
+ const std::function<void ()>* function;
+
+ {
+ LockHolder holder(m_lock);
+
+ m_condition.wait(m_lock, [this] {
+ return !m_queue.isEmpty();
+ });
+
+ function = m_queue.takeFirst();
+ }
+
+ (*function)();
+ }
+ }
+
+ Lock m_lock;
+ Condition m_condition;
+ Deque<const std::function<void ()>*> m_queue;
+
+ Vector<ThreadIdentifier> m_workers;
+ };
+
+ static LazyNeverDestroyed<ThreadPool> threadPool;
+ static std::once_flag onceFlag;
+ std::call_once(onceFlag, [] {
+ threadPool.construct();
+ });
+
+ // Cap the worker count to the number of iterations (excluding this thread)
+ const size_t workerCount = std::min(iterations - 1, threadPool->workerCount());
+
+ std::atomic<size_t> currentIndex(0);
+ std::atomic<size_t> activeThreads(workerCount + 1);
+
+ Condition condition;
+ Lock lock;
+
+ std::function<void ()> applier = [&] {
+ size_t index;
+
+ // Call the function for as long as there are iterations left.
+ while ((index = currentIndex++) < iterations)
+ function(index);
+
+ // If there are no active threads left, signal the caller.
+ if (!--activeThreads) {
+ LockHolder holder(lock);
+ condition.notifyOne();
+ }
+ };
+
+ for (size_t i = 0; i < workerCount; ++i)
+ threadPool->dispatch(&applier);
+ applier();
+
+ LockHolder holder(lock);
+ condition.wait(lock, [&] { return !activeThreads; });
+}
+#endif
+
}
diff --git a/Source/WTF/wtf/WorkQueue.h b/Source/WTF/wtf/WorkQueue.h
index 6851c09..2b9bd3e 100644
--- a/Source/WTF/wtf/WorkQueue.h
+++ b/Source/WTF/wtf/WorkQueue.h
@@ -71,6 +71,8 @@
WTF_EXPORT_PRIVATE virtual void dispatch(std::function<void ()>) override;
WTF_EXPORT_PRIVATE void dispatchAfter(std::chrono::nanoseconds, std::function<void ()>);
+ WTF_EXPORT_PRIVATE static void concurrentApply(size_t iterations, const std::function<void (size_t index)>&);
+
#if OS(DARWIN)
dispatch_queue_t dispatchQueue() const { return m_dispatchQueue; }
#elif PLATFORM(GTK)
diff --git a/Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp b/Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp
index 1901b60..134961e 100644
--- a/Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp
+++ b/Source/WTF/wtf/cocoa/WorkQueueCocoa.cpp
@@ -102,4 +102,11 @@
dispatch_release(m_dispatchQueue);
}
+void WorkQueue::concurrentApply(size_t iterations, const std::function<void (size_t index)>& function)
+{
+ dispatch_apply(iterations, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
+ function(index);
+ });
+}
+
}