haikuwebkit/Source/WTF/wtf/WorkerPool.h

73 lines
2.6 KiB
C
Raw Permalink Normal View History

[WTF] Add WorkerPool https://bugs.webkit.org/show_bug.cgi?id=174569 Reviewed by Carlos Garcia Campos. Source/WebCore: We start using WorkerPool for NicosiaPaintingEngineThreaded instead of glib thread pool. This makes NicosiaPaintingEngineThreaded platform-independent and usable for WinCairo. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.cpp: (Nicosia::PaintingEngineThreaded::PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::~PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::paint): (Nicosia::s_threadFunc): Deleted. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.h: Source/WTF: This patch adds WorkerPool, which is a thread pool that consists of AutomaticThread. Since it is based on AutomaticThread, this WorkerPool can take `timeout`: once `timeout` passes without any tasks, threads in WorkerPool will be destroyed. We add shouldSleep handler to AutomaticThread to make destruction of threads in WorkerPool moderate. Without this, all threads are destroyed at once after `timeout` passes. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: (WTF::AutomaticThread::AutomaticThread): (WTF::AutomaticThread::start): * wtf/AutomaticThread.h: * wtf/CMakeLists.txt: * wtf/WorkerPool.cpp: Added. (WTF::WorkerPool::WorkerPool): (WTF::WorkerPool::~WorkerPool): (WTF::WorkerPool::shouldSleep): (WTF::WorkerPool::postTask): * wtf/WorkerPool.h: Added. (WTF::WorkerPool::create): Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/WorkerPool.cpp: Added. (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/201790@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-06-08 08:47:06 +00:00
/*
* 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>
#include <wtf/text/WTFString.h>
[WTF] Add WorkerPool https://bugs.webkit.org/show_bug.cgi?id=174569 Reviewed by Carlos Garcia Campos. Source/WebCore: We start using WorkerPool for NicosiaPaintingEngineThreaded instead of glib thread pool. This makes NicosiaPaintingEngineThreaded platform-independent and usable for WinCairo. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.cpp: (Nicosia::PaintingEngineThreaded::PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::~PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::paint): (Nicosia::s_threadFunc): Deleted. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.h: Source/WTF: This patch adds WorkerPool, which is a thread pool that consists of AutomaticThread. Since it is based on AutomaticThread, this WorkerPool can take `timeout`: once `timeout` passes without any tasks, threads in WorkerPool will be destroyed. We add shouldSleep handler to AutomaticThread to make destruction of threads in WorkerPool moderate. Without this, all threads are destroyed at once after `timeout` passes. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: (WTF::AutomaticThread::AutomaticThread): (WTF::AutomaticThread::start): * wtf/AutomaticThread.h: * wtf/CMakeLists.txt: * wtf/WorkerPool.cpp: Added. (WTF::WorkerPool::WorkerPool): (WTF::WorkerPool::~WorkerPool): (WTF::WorkerPool::shouldSleep): (WTF::WorkerPool::postTask): * wtf/WorkerPool.h: Added. (WTF::WorkerPool::create): Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/WorkerPool.cpp: Added. (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/201790@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-06-08 08:47:06 +00:00
namespace WTF {
class WorkerPool : public ThreadSafeRefCounted<WorkerPool> {
public:
WTF_EXPORT_PRIVATE void postTask(Function<void()>&&);
WTF_EXPORT_PRIVATE ~WorkerPool();
// If timeout is infinity, it means AutomaticThread will be never automatically destroyed.
static Ref<WorkerPool> create(ASCIILiteral name, unsigned numberOfWorkers = WTF::numberOfProcessorCores(), Seconds timeout = Seconds::infinity())
[WTF] Add WorkerPool https://bugs.webkit.org/show_bug.cgi?id=174569 Reviewed by Carlos Garcia Campos. Source/WebCore: We start using WorkerPool for NicosiaPaintingEngineThreaded instead of glib thread pool. This makes NicosiaPaintingEngineThreaded platform-independent and usable for WinCairo. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.cpp: (Nicosia::PaintingEngineThreaded::PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::~PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::paint): (Nicosia::s_threadFunc): Deleted. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.h: Source/WTF: This patch adds WorkerPool, which is a thread pool that consists of AutomaticThread. Since it is based on AutomaticThread, this WorkerPool can take `timeout`: once `timeout` passes without any tasks, threads in WorkerPool will be destroyed. We add shouldSleep handler to AutomaticThread to make destruction of threads in WorkerPool moderate. Without this, all threads are destroyed at once after `timeout` passes. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: (WTF::AutomaticThread::AutomaticThread): (WTF::AutomaticThread::start): * wtf/AutomaticThread.h: * wtf/CMakeLists.txt: * wtf/WorkerPool.cpp: Added. (WTF::WorkerPool::WorkerPool): (WTF::WorkerPool::~WorkerPool): (WTF::WorkerPool::shouldSleep): (WTF::WorkerPool::postTask): * wtf/WorkerPool.h: Added. (WTF::WorkerPool::create): Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/WorkerPool.cpp: Added. (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/201790@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-06-08 08:47:06 +00:00
{
ASSERT(numberOfWorkers >= 1);
return adoptRef(*new WorkerPool(name, numberOfWorkers, timeout));
[WTF] Add WorkerPool https://bugs.webkit.org/show_bug.cgi?id=174569 Reviewed by Carlos Garcia Campos. Source/WebCore: We start using WorkerPool for NicosiaPaintingEngineThreaded instead of glib thread pool. This makes NicosiaPaintingEngineThreaded platform-independent and usable for WinCairo. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.cpp: (Nicosia::PaintingEngineThreaded::PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::~PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::paint): (Nicosia::s_threadFunc): Deleted. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.h: Source/WTF: This patch adds WorkerPool, which is a thread pool that consists of AutomaticThread. Since it is based on AutomaticThread, this WorkerPool can take `timeout`: once `timeout` passes without any tasks, threads in WorkerPool will be destroyed. We add shouldSleep handler to AutomaticThread to make destruction of threads in WorkerPool moderate. Without this, all threads are destroyed at once after `timeout` passes. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: (WTF::AutomaticThread::AutomaticThread): (WTF::AutomaticThread::start): * wtf/AutomaticThread.h: * wtf/CMakeLists.txt: * wtf/WorkerPool.cpp: Added. (WTF::WorkerPool::WorkerPool): (WTF::WorkerPool::~WorkerPool): (WTF::WorkerPool::shouldSleep): (WTF::WorkerPool::postTask): * wtf/WorkerPool.h: Added. (WTF::WorkerPool::create): Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/WorkerPool.cpp: Added. (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/201790@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-06-08 08:47:06 +00:00
}
ASCIILiteral name() const { return m_name; }
[WTF] Add WorkerPool https://bugs.webkit.org/show_bug.cgi?id=174569 Reviewed by Carlos Garcia Campos. Source/WebCore: We start using WorkerPool for NicosiaPaintingEngineThreaded instead of glib thread pool. This makes NicosiaPaintingEngineThreaded platform-independent and usable for WinCairo. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.cpp: (Nicosia::PaintingEngineThreaded::PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::~PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::paint): (Nicosia::s_threadFunc): Deleted. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.h: Source/WTF: This patch adds WorkerPool, which is a thread pool that consists of AutomaticThread. Since it is based on AutomaticThread, this WorkerPool can take `timeout`: once `timeout` passes without any tasks, threads in WorkerPool will be destroyed. We add shouldSleep handler to AutomaticThread to make destruction of threads in WorkerPool moderate. Without this, all threads are destroyed at once after `timeout` passes. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: (WTF::AutomaticThread::AutomaticThread): (WTF::AutomaticThread::start): * wtf/AutomaticThread.h: * wtf/CMakeLists.txt: * wtf/WorkerPool.cpp: Added. (WTF::WorkerPool::WorkerPool): (WTF::WorkerPool::~WorkerPool): (WTF::WorkerPool::shouldSleep): (WTF::WorkerPool::postTask): * wtf/WorkerPool.h: Added. (WTF::WorkerPool::create): Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/WorkerPool.cpp: Added. (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/201790@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-06-08 08:47:06 +00:00
private:
class Worker;
friend class Worker;
WTF_EXPORT_PRIVATE WorkerPool(ASCIILiteral name, unsigned numberOfWorkers, Seconds timeout);
[WTF] Add WorkerPool https://bugs.webkit.org/show_bug.cgi?id=174569 Reviewed by Carlos Garcia Campos. Source/WebCore: We start using WorkerPool for NicosiaPaintingEngineThreaded instead of glib thread pool. This makes NicosiaPaintingEngineThreaded platform-independent and usable for WinCairo. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.cpp: (Nicosia::PaintingEngineThreaded::PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::~PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::paint): (Nicosia::s_threadFunc): Deleted. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.h: Source/WTF: This patch adds WorkerPool, which is a thread pool that consists of AutomaticThread. Since it is based on AutomaticThread, this WorkerPool can take `timeout`: once `timeout` passes without any tasks, threads in WorkerPool will be destroyed. We add shouldSleep handler to AutomaticThread to make destruction of threads in WorkerPool moderate. Without this, all threads are destroyed at once after `timeout` passes. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: (WTF::AutomaticThread::AutomaticThread): (WTF::AutomaticThread::start): * wtf/AutomaticThread.h: * wtf/CMakeLists.txt: * wtf/WorkerPool.cpp: Added. (WTF::WorkerPool::WorkerPool): (WTF::WorkerPool::~WorkerPool): (WTF::WorkerPool::shouldSleep): (WTF::WorkerPool::postTask): * wtf/WorkerPool.h: Added. (WTF::WorkerPool::create): Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/WorkerPool.cpp: Added. (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/201790@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-06-08 08:47:06 +00:00
bool shouldSleep(const AbstractLocker&);
Box<Lock> m_lock;
Ref<AutomaticThreadCondition> m_condition;
[WTF] Add WorkerPool https://bugs.webkit.org/show_bug.cgi?id=174569 Reviewed by Carlos Garcia Campos. Source/WebCore: We start using WorkerPool for NicosiaPaintingEngineThreaded instead of glib thread pool. This makes NicosiaPaintingEngineThreaded platform-independent and usable for WinCairo. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.cpp: (Nicosia::PaintingEngineThreaded::PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::~PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::paint): (Nicosia::s_threadFunc): Deleted. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.h: Source/WTF: This patch adds WorkerPool, which is a thread pool that consists of AutomaticThread. Since it is based on AutomaticThread, this WorkerPool can take `timeout`: once `timeout` passes without any tasks, threads in WorkerPool will be destroyed. We add shouldSleep handler to AutomaticThread to make destruction of threads in WorkerPool moderate. Without this, all threads are destroyed at once after `timeout` passes. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: (WTF::AutomaticThread::AutomaticThread): (WTF::AutomaticThread::start): * wtf/AutomaticThread.h: * wtf/CMakeLists.txt: * wtf/WorkerPool.cpp: Added. (WTF::WorkerPool::WorkerPool): (WTF::WorkerPool::~WorkerPool): (WTF::WorkerPool::shouldSleep): (WTF::WorkerPool::postTask): * wtf/WorkerPool.h: Added. (WTF::WorkerPool::create): Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/WorkerPool.cpp: Added. (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/201790@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-06-08 08:47:06 +00:00
Seconds m_timeout;
MonotonicTime m_lastTimeoutTime { MonotonicTime::nan() };
unsigned m_numberOfActiveWorkers { 0 };
Vector<Ref<Worker>> m_workers;
Deque<Function<void()>> m_tasks;
ASCIILiteral m_name;
[WTF] Add WorkerPool https://bugs.webkit.org/show_bug.cgi?id=174569 Reviewed by Carlos Garcia Campos. Source/WebCore: We start using WorkerPool for NicosiaPaintingEngineThreaded instead of glib thread pool. This makes NicosiaPaintingEngineThreaded platform-independent and usable for WinCairo. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.cpp: (Nicosia::PaintingEngineThreaded::PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::~PaintingEngineThreaded): (Nicosia::PaintingEngineThreaded::paint): (Nicosia::s_threadFunc): Deleted. * platform/graphics/nicosia/NicosiaPaintingEngineThreaded.h: Source/WTF: This patch adds WorkerPool, which is a thread pool that consists of AutomaticThread. Since it is based on AutomaticThread, this WorkerPool can take `timeout`: once `timeout` passes without any tasks, threads in WorkerPool will be destroyed. We add shouldSleep handler to AutomaticThread to make destruction of threads in WorkerPool moderate. Without this, all threads are destroyed at once after `timeout` passes. * WTF.xcodeproj/project.pbxproj: * wtf/AutomaticThread.cpp: (WTF::AutomaticThread::AutomaticThread): (WTF::AutomaticThread::start): * wtf/AutomaticThread.h: * wtf/CMakeLists.txt: * wtf/WorkerPool.cpp: Added. (WTF::WorkerPool::WorkerPool): (WTF::WorkerPool::~WorkerPool): (WTF::WorkerPool::shouldSleep): (WTF::WorkerPool::postTask): * wtf/WorkerPool.h: Added. (WTF::WorkerPool::create): Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/WorkerPool.cpp: Added. (TestWebKitAPI::TEST): Canonical link: https://commits.webkit.org/201790@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-06-08 08:47:06 +00:00
};
}
using WTF::WorkerPool;