forked from WebKit/WebKit-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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): git-svn-id: http://svn.webkit.org/repository/webkit/trunk@232619 268f45cc-cd09-0410-ab3c-d52691b4dbfc
- Loading branch information
1 parent
0caddbd
commit c476d16
Showing
14 changed files
with
329 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,31 @@ | ||
2018-06-07 Yusuke Suzuki <[email protected]> | ||
|
||
[WTF] Add WorkerPool | ||
https://bugs.webkit.org/show_bug.cgi?id=174569 | ||
|
||
Reviewed by Carlos Garcia Campos. | ||
|
||
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): | ||
|
||
2018-06-07 Chris Dumez <[email protected]> | ||
|
||
Add base class to get WeakPtrFactory member and avoid some boilerplate code | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright (C) 2017 Yusuke Suzuki <[email protected]>. | ||
* | ||
* 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 final : public AutomaticThread { | ||
public: | ||
friend class WorkerPool; | ||
|
||
Worker(const AbstractLocker& locker, WorkerPool& pool, Box<Lock> lock, RefPtr<AutomaticThreadCondition> condition, Seconds timeout) | ||
: AutomaticThread(locker, lock, condition, timeout) | ||
, m_pool(pool) | ||
{ | ||
} | ||
|
||
PollResult poll(const AbstractLocker&) final | ||
{ | ||
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() final | ||
{ | ||
m_task(); | ||
m_task = nullptr; | ||
return WorkResult::Continue; | ||
} | ||
|
||
void threadDidStart() final | ||
{ | ||
LockHolder locker(*m_pool.m_lock); | ||
m_pool.m_numberOfActiveWorkers++; | ||
} | ||
|
||
void threadIsStopping(const AbstractLocker&) final | ||
{ | ||
m_pool.m_numberOfActiveWorkers--; | ||
} | ||
|
||
bool shouldSleep(const AbstractLocker& locker) final | ||
{ | ||
return m_pool.shouldSleep(locker); | ||
} | ||
|
||
private: | ||
WorkerPool& m_pool; | ||
Function<void()> m_task; | ||
}; | ||
|
||
WorkerPool::WorkerPool(unsigned numberOfWorkers, Seconds timeout) | ||
: m_lock(Box<Lock>::create()) | ||
, m_condition(AutomaticThreadCondition::create()) | ||
, m_timeout(timeout) | ||
{ | ||
LockHolder locker(*m_lock); | ||
for (unsigned i = 0; i < numberOfWorkers; ++i) | ||
m_workers.append(adoptRef(*new Worker(locker, *this, m_lock, m_condition, timeout))); | ||
} | ||
|
||
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&) | ||
{ | ||
if (m_timeout > 0_s && std::isinf(m_timeout)) | ||
return false; | ||
|
||
MonotonicTime currentTime = MonotonicTime::now(); | ||
if (std::isnan(m_lastTimeoutTime) || (currentTime >= (m_lastTimeoutTime + m_timeout))) { | ||
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2017 Yusuke Suzuki <[email protected]>. | ||
* | ||
* 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: | ||
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(unsigned numberOfWorkers = WTF::numberOfProcessorCores(), Seconds timeout = Seconds::infinity()) | ||
{ | ||
ASSERT(numberOfWorkers >= 1); | ||
return adoptRef(*new WorkerPool(numberOfWorkers, timeout)); | ||
} | ||
|
||
private: | ||
class Worker; | ||
friend class Worker; | ||
|
||
WTF_EXPORT_PRIVATE WorkerPool(unsigned numberOfWorkers, Seconds timeout); | ||
|
||
bool shouldSleep(const AbstractLocker&); | ||
|
||
Box<Lock> m_lock; | ||
RefPtr<AutomaticThreadCondition> m_condition; | ||
Seconds m_timeout; | ||
MonotonicTime m_lastTimeoutTime { MonotonicTime::nan() }; | ||
unsigned m_numberOfActiveWorkers { 0 }; | ||
Vector<Ref<Worker>> m_workers; | ||
Deque<Function<void()>> m_tasks; | ||
}; | ||
|
||
} | ||
|
||
using WTF::WorkerPool; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
2018-06-07 Yusuke Suzuki <[email protected]> | ||
|
||
[WTF] Add WorkerPool | ||
https://bugs.webkit.org/show_bug.cgi?id=174569 | ||
|
||
Reviewed by Carlos Garcia Campos. | ||
|
||
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: | ||
|
||
2018-06-08 Miguel Gomez <[email protected]> | ||
|
||
[GTK][WPE] Wrong result when calling ImageBufferCairo's getImageData() | ||
|
Oops, something went wrong.