WebKit Bugzilla
New
Browse
Search+
Log In
×
Sign in with GitHub
or
Remember my login
Create Account
·
Forgot Password
Forgotten password account recovery
[patch]
Patch
bug-174569-20170717043424.patch (text/plain), 14.98 KB, created by
Yusuke Suzuki
on 2017-07-16 12:34:25 PDT
(
hide
)
Description:
Patch
Filename:
MIME Type:
Creator:
Yusuke Suzuki
Created:
2017-07-16 12:34:25 PDT
Size:
14.98 KB
patch
obsolete
>Subversion Revision: 219547 >diff --git a/Source/WTF/ChangeLog b/Source/WTF/ChangeLog >index 0180818c0fcbc8825168d2b97a9bd44dc1fa4efe..37ee1955549f4b6d72be0adaeac2b933a584f6d8 100644 >--- a/Source/WTF/ChangeLog >+++ b/Source/WTF/ChangeLog >@@ -1,3 +1,26 @@ >+2017-07-16 Yusuke Suzuki <utatane.tea@gmail.com> >+ >+ [WTF] Add WorkerPool >+ https://bugs.webkit.org/show_bug.cgi?id=174569 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ * wtf/AutomaticThread.cpp: >+ (WTF::AutomaticThread::AutomaticThread): >+ (WTF::AutomaticThread::start): >+ * wtf/AutomaticThread.h: >+ * wtf/CMakeLists.txt: >+ * wtf/WorkerPool.cpp: Added. >+ (WTF::WorkerPool::Worker::Worker): >+ (WTF::WorkerPool::Worker::threadIsStopping): >+ (WTF::WorkerPool::sharedInstance): >+ (WTF::WorkerPool::WorkerPool): >+ (WTF::WorkerPool::~WorkerPool): >+ (WTF::WorkerPool::shouldSleep): >+ (WTF::WorkerPool::postTask): >+ * wtf/WorkerPool.h: Added. >+ (WTF::WorkerPool::create): >+ > 2017-07-14 Jonathan Bedard <jbedard@apple.com> > > Add iOS 11 SPI >diff --git a/Source/WTF/wtf/AutomaticThread.cpp b/Source/WTF/wtf/AutomaticThread.cpp >index 81118760b3968dcd024b491d12974a8502b188ef..16a9e0cbba6a07a3ef1597530da18f4075000275 100644 >--- a/Source/WTF/wtf/AutomaticThread.cpp >+++ b/Source/WTF/wtf/AutomaticThread.cpp >@@ -103,9 +103,10 @@ bool AutomaticThreadCondition::contains(const AbstractLocker&, AutomaticThread* > return m_threads.contains(thread); > } > >-AutomaticThread::AutomaticThread(const AbstractLocker& locker, Box<Lock> lock, RefPtr<AutomaticThreadCondition> condition) >+AutomaticThread::AutomaticThread(const AbstractLocker& locker, Box<Lock> lock, RefPtr<AutomaticThreadCondition> condition, Seconds timeout) > : m_lock(lock) > , m_condition(condition) >+ , m_timeout(timeout) > { > if (verbose) > dataLog(RawPointer(this), ": Allocated AutomaticThread.\n"); >@@ -203,10 +204,10 @@ void AutomaticThread::start(const AbstractLocker&) > // Shut the thread down after a timeout. > m_isWaiting = true; > bool awokenByNotify = >- m_waitCondition.waitFor(*m_lock, 10_s); >+ m_waitCondition.waitFor(*m_lock, m_timeout); > if (verbose && !awokenByNotify && !m_isWaiting) > dataLog(RawPointer(this), ": waitFor timed out, but notified via m_isWaiting flag!\n"); >- if (m_isWaiting) { >+ if (m_isWaiting && shouldSleep(locker)) { > m_isWaiting = false; > if (verbose) > dataLog(RawPointer(this), ": Going to sleep!\n"); >diff --git a/Source/WTF/wtf/AutomaticThread.h b/Source/WTF/wtf/AutomaticThread.h >index 1b904540c7f6046b0752afe975d22a3b4a2c8b8a..ff54f31741be78fd3a52ce4247671874b2cd6dc9 100644 >--- a/Source/WTF/wtf/AutomaticThread.h >+++ b/Source/WTF/wtf/AutomaticThread.h >@@ -129,7 +129,7 @@ class WTF_EXPORT_PRIVATE AutomaticThread : public ThreadSafeRefCounted<Automatic > protected: > // This logically creates the thread, but in reality the thread won't be created until someone > // calls AutomaticThreadCondition::notifyOne() or notifyAll(). >- AutomaticThread(const AbstractLocker&, Box<Lock>, RefPtr<AutomaticThreadCondition>); >+ AutomaticThread(const AbstractLocker&, Box<Lock>, RefPtr<AutomaticThreadCondition>, Seconds timeout = 10_s); > > // To understand PollResult and WorkResult, imagine that poll() and work() are being called like > // so: >@@ -168,6 +168,11 @@ class WTF_EXPORT_PRIVATE AutomaticThread : public ThreadSafeRefCounted<Automatic > // can be sure that the default ones don't do anything (so you don't need a super call). > virtual void threadDidStart(); > virtual void threadIsStopping(const AbstractLocker&); >+ >+ // Control whether this automatic thread should sleep when timeout happens. >+ // By overriding this function, we can customize how automatic threads will sleep. >+ // For example, when you have thread pool, you can decrease active threads moderately. >+ virtual bool shouldSleep(const AbstractLocker&) { return true; } > > private: > friend class AutomaticThreadCondition; >@@ -181,6 +186,7 @@ class WTF_EXPORT_PRIVATE AutomaticThread : public ThreadSafeRefCounted<Automatic > bool m_hasUnderlyingThread { false }; > Condition m_waitCondition; > Condition m_isRunningCondition; >+ Seconds m_timeout; > }; > > } // namespace WTF >diff --git a/Source/WTF/wtf/CMakeLists.txt b/Source/WTF/wtf/CMakeLists.txt >index 936afb59801d8527b8d009bb7bb520b8fc433c8d..c8b1eaba43e122194b48159cdbd68426b641abb8 100644 >--- a/Source/WTF/wtf/CMakeLists.txt >+++ b/Source/WTF/wtf/CMakeLists.txt >@@ -152,6 +152,7 @@ set(WTF_HEADERS > WeakPtr.h > WordLock.h > WorkQueue.h >+ WorkerPool.h > dtoa.h > > dtoa/bignum-dtoa.h >@@ -254,6 +255,7 @@ set(WTF_SOURCES > WallTime.cpp > WordLock.cpp > WorkQueue.cpp >+ WorkerPool.cpp > dtoa.cpp > > dtoa/bignum-dtoa.cc >diff --git a/Source/WTF/wtf/WorkerPool.cpp b/Source/WTF/wtf/WorkerPool.cpp >new file mode 100644 >index 0000000000000000000000000000000000000000..b990c522e754039fb6bfa6e4c31cc57b0398f44f >--- /dev/null >+++ b/Source/WTF/wtf/WorkerPool.cpp >@@ -0,0 +1,126 @@ >+/* >+ * Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>. >+ * >+ * 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. ``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 >+ * 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 "WorkerPool.h" >+ >+#include <wtf/NeverDestroyed.h> >+ >+namespace WTF { >+ >+class WorkerPool::Worker : public AutomaticThread { >+public: >+ friend class WorkerPool; >+ >+ Worker(const AbstractLocker& locker, WorkerPool& pool, Box<Lock> lock, RefPtr<AutomaticThreadCondition> condition) >+ : AutomaticThread(locker, lock, condition, 2_s) >+ , m_pool(pool) >+ { >+ } >+ >+ PollResult poll(const AbstractLocker&) override >+ { >+ if (m_pool.m_tasks.isEmpty()) >+ return PollResult::Wait; >+ m_task = m_pool.m_tasks.takeFirst(); >+ if (!m_task) >+ return PollResult::Stop; >+ return PollResult::Work; >+ } >+ >+ WorkResult work() override >+ { >+ m_task(); >+ m_task = nullptr; >+ return WorkResult::Continue; >+ } >+ >+ void threadDidStart() override >+ { >+ LockHolder locker(*m_pool.m_lock); >+ m_pool.m_numberOfActiveWorkers++; >+ } >+ >+ virtual void threadIsStopping(const AbstractLocker&) >+ { >+ m_pool.m_numberOfActiveWorkers--; >+ } >+ >+ bool shouldSleep(const AbstractLocker& locker) override >+ { >+ return m_pool.shouldSleep(locker); >+ } >+ >+private: >+ WorkerPool& m_pool; >+ Function<void()> m_task; >+}; >+ >+WorkerPool& WorkerPool::sharedInstance() >+{ >+ static NeverDestroyed<Ref<WorkerPool>> pool(WorkerPool::create()); >+ return pool.get().get(); >+} >+ >+WorkerPool::WorkerPool(int numberOfWorkers) >+ : m_lock(Box<Lock>::create()) >+ , m_condition(AutomaticThreadCondition::create()) >+{ >+ LockHolder locker(*m_lock); >+ for (int i = 0; i < numberOfWorkers; ++i) >+ m_workers.append(adoptRef(*new Worker(locker, *this, m_lock, m_condition))); >+} >+ >+WorkerPool::~WorkerPool() >+{ >+ { >+ LockHolder locker(*m_lock); >+ for (unsigned i = m_workers.size(); i--;) >+ m_tasks.append(nullptr); // Use null task to indicate that we want the thread to terminate. >+ m_condition->notifyAll(locker); >+ } >+ for (auto& worker : m_workers) >+ worker->join(); >+ ASSERT(!m_numberOfActiveWorkers); >+} >+ >+bool WorkerPool::shouldSleep(const AbstractLocker&) >+{ >+ MonotonicTime currentTime = MonotonicTime::now(); >+ if (std::isnan(m_lastTimeoutTime) || (currentTime >= (m_lastTimeoutTime + 5_s))) { >+ m_lastTimeoutTime = currentTime; >+ return true; >+ } >+ return false; >+} >+ >+void WorkerPool::postTask(Function<void()> task) >+{ >+ LockHolder locker(*m_lock); >+ m_tasks.append(WTFMove(task)); >+ m_condition->notifyOne(locker); >+} >+ >+} >diff --git a/Source/WTF/wtf/WorkerPool.h b/Source/WTF/wtf/WorkerPool.h >new file mode 100644 >index 0000000000000000000000000000000000000000..e05051110de9813de20d6ab4faab6e533e25d431 >--- /dev/null >+++ b/Source/WTF/wtf/WorkerPool.h >@@ -0,0 +1,68 @@ >+/* >+ * Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>. >+ * >+ * 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. ``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 >+ * 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/AutomaticThread.h> >+#include <wtf/Deque.h> >+#include <wtf/Function.h> >+#include <wtf/NumberOfCores.h> >+#include <wtf/Vector.h> >+ >+namespace WTF { >+ >+class WorkerPool : public ThreadSafeRefCounted<WorkerPool> { >+public: >+ ~WorkerPool(); >+ >+ void postTask(Function<void()>); >+ >+ static WorkerPool& sharedInstance(); >+ >+ static Ref<WorkerPool> create(int numberOfWorkers = WTF::numberOfProcessorCores()) >+ { >+ ASSERT(numberOfWorkers >= 1); >+ return adoptRef(*new WorkerPool(numberOfWorkers)); >+ } >+ >+private: >+ class Worker; >+ friend class Worker; >+ >+ WorkerPool(int numberOfWorkers); >+ >+ bool shouldSleep(const AbstractLocker&); >+ >+ Box<Lock> m_lock; >+ RefPtr<AutomaticThreadCondition> m_condition; >+ MonotonicTime m_lastTimeoutTime { MonotonicTime::nan() }; >+ unsigned m_numberOfActiveWorkers { 0 }; >+ Vector<Ref<Worker>> m_workers; >+ Deque<Function<void()>> m_tasks; >+}; >+ >+} >+ >+using WTF::WorkerPool; >diff --git a/Tools/ChangeLog b/Tools/ChangeLog >index 913f09ea1592696d2c2ecd7b3e4c72739d22f878..a3ff61befb2209dfc7545cef07a443ba4770d960 100644 >--- a/Tools/ChangeLog >+++ b/Tools/ChangeLog >@@ -1,3 +1,14 @@ >+2017-07-16 Yusuke Suzuki <utatane.tea@gmail.com> >+ >+ [WTF] Add WorkerPool >+ https://bugs.webkit.org/show_bug.cgi?id=174569 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ * TestWebKitAPI/CMakeLists.txt: >+ * TestWebKitAPI/Tests/WTF/WorkerPool.cpp: Added. >+ (TestWebKitAPI::TEST): >+ > 2017-07-16 Bernhard M. Wiedemann <bwiedemann@suse.de> > > [GTK] Sort inspector GResource manifest to ensure reproducible builds >diff --git a/Tools/TestWebKitAPI/CMakeLists.txt b/Tools/TestWebKitAPI/CMakeLists.txt >index 0e3e0f6b33c514ae1054b89c4477091462f849a5..d047f1ae0646af6e3dda6c42494ae4318d6ee133 100644 >--- a/Tools/TestWebKitAPI/CMakeLists.txt >+++ b/Tools/TestWebKitAPI/CMakeLists.txt >@@ -88,6 +88,7 @@ set(TestWTF_SOURCES > ${TESTWEBKITAPI_DIR}/Tests/WTF/WTFString.cpp > ${TESTWEBKITAPI_DIR}/Tests/WTF/WeakPtr.cpp > ${TESTWEBKITAPI_DIR}/Tests/WTF/WorkQueue.cpp >+ ${TESTWEBKITAPI_DIR}/Tests/WTF/WorkerPool.cpp > ) > > # FIXME: Tests/WTF/RunLoop.cpp is missing because it doesn't work for Windows. >diff --git a/Tools/TestWebKitAPI/Tests/WTF/WorkerPool.cpp b/Tools/TestWebKitAPI/Tests/WTF/WorkerPool.cpp >new file mode 100644 >index 0000000000000000000000000000000000000000..7e1bdf471353ba634ef575f7b13d2059f7f2ac1d >--- /dev/null >+++ b/Tools/TestWebKitAPI/Tests/WTF/WorkerPool.cpp >@@ -0,0 +1,47 @@ >+/* >+ * Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>. >+ * >+ * 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 <wtf/CurrentTime.h> >+#include <wtf/DataLog.h> >+#include <wtf/WorkerPool.h> >+ >+namespace TestWebKitAPI { >+ >+TEST(WTF, WorkerPoolDecrease) >+{ >+ Ref<WorkerPool> pool = WorkerPool::create(); >+ for (int i = 0; i < 1000; ++i) { >+ sleep(0.001); >+ pool->postTask([=] { >+ dataLog("[", i, "] Hello World\n"); >+ sleep(0.02); >+ }); >+ } >+ sleep(200.0); >+} >+ >+} // namespace TestWebKitAPI
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Formatted Diff
|
Diff
Attachments on
bug 174569
:
315618
|
342125
|
342126
|
342133
|
342147
|
342164
|
342165
|
342167