| 1 | /* |
| 2 | * Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include <wtf/WorkerPool.h> |
| 28 | |
| 29 | #include <wtf/NeverDestroyed.h> |
| 30 | |
| 31 | namespace WTF { |
| 32 | |
| 33 | class WorkerPool::Worker final : public AutomaticThread { |
| 34 | public: |
| 35 | friend class WorkerPool; |
| 36 | |
| 37 | Worker(const AbstractLocker& locker, WorkerPool& pool, Box<Lock> lock, Ref<AutomaticThreadCondition>&& condition, Seconds timeout) |
| 38 | : AutomaticThread(locker, lock, WTFMove(condition), timeout) |
| 39 | , m_pool(pool) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | PollResult poll(const AbstractLocker&) final |
| 44 | { |
| 45 | if (m_pool.m_tasks.isEmpty()) |
| 46 | return PollResult::Wait; |
| 47 | m_task = m_pool.m_tasks.takeFirst(); |
| 48 | if (!m_task) |
| 49 | return PollResult::Stop; |
| 50 | return PollResult::Work; |
| 51 | } |
| 52 | |
| 53 | WorkResult work() final |
| 54 | { |
| 55 | m_task(); |
| 56 | m_task = nullptr; |
| 57 | return WorkResult::Continue; |
| 58 | } |
| 59 | |
| 60 | void threadDidStart() final |
| 61 | { |
| 62 | LockHolder locker(*m_pool.m_lock); |
| 63 | m_pool.m_numberOfActiveWorkers++; |
| 64 | } |
| 65 | |
| 66 | void threadIsStopping(const AbstractLocker&) final |
| 67 | { |
| 68 | m_pool.m_numberOfActiveWorkers--; |
| 69 | } |
| 70 | |
| 71 | bool shouldSleep(const AbstractLocker& locker) final |
| 72 | { |
| 73 | return m_pool.shouldSleep(locker); |
| 74 | } |
| 75 | |
| 76 | const char* name() const override |
| 77 | { |
| 78 | return m_pool.name(); |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | WorkerPool& m_pool; |
| 83 | Function<void()> m_task; |
| 84 | }; |
| 85 | |
| 86 | WorkerPool::WorkerPool(ASCIILiteral name, unsigned numberOfWorkers, Seconds timeout) |
| 87 | : m_lock(Box<Lock>::create()) |
| 88 | , m_condition(AutomaticThreadCondition::create()) |
| 89 | , m_timeout(timeout) |
| 90 | , m_name(name) |
| 91 | { |
| 92 | LockHolder locker(*m_lock); |
| 93 | for (unsigned i = 0; i < numberOfWorkers; ++i) |
| 94 | m_workers.append(adoptRef(*new Worker(locker, *this, m_lock, m_condition.copyRef(), timeout))); |
| 95 | } |
| 96 | |
| 97 | WorkerPool::~WorkerPool() |
| 98 | { |
| 99 | { |
| 100 | LockHolder locker(*m_lock); |
| 101 | for (unsigned i = m_workers.size(); i--;) |
| 102 | m_tasks.append(nullptr); // Use null task to indicate that we want the thread to terminate. |
| 103 | m_condition->notifyAll(locker); |
| 104 | } |
| 105 | for (auto& worker : m_workers) |
| 106 | worker->join(); |
| 107 | ASSERT(!m_numberOfActiveWorkers); |
| 108 | } |
| 109 | |
| 110 | bool WorkerPool::shouldSleep(const AbstractLocker&) |
| 111 | { |
| 112 | if (m_timeout > 0_s && std::isinf(m_timeout)) |
| 113 | return false; |
| 114 | |
| 115 | MonotonicTime currentTime = MonotonicTime::now(); |
| 116 | if (std::isnan(m_lastTimeoutTime) || (currentTime >= (m_lastTimeoutTime + m_timeout))) { |
| 117 | m_lastTimeoutTime = currentTime; |
| 118 | return true; |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | void WorkerPool::postTask(Function<void()>&& task) |
| 124 | { |
| 125 | LockHolder locker(*m_lock); |
| 126 | m_tasks.append(WTFMove(task)); |
| 127 | m_condition->notifyOne(locker); |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |