diff options
Diffstat (limited to 'Source/WTF/wtf/MessageQueue.h')
-rw-r--r-- | Source/WTF/wtf/MessageQueue.h | 75 |
1 files changed, 43 insertions, 32 deletions
diff --git a/Source/WTF/wtf/MessageQueue.h b/Source/WTF/wtf/MessageQueue.h index 4d3d419c1..c52a6ac9c 100644 --- a/Source/WTF/wtf/MessageQueue.h +++ b/Source/WTF/wtf/MessageQueue.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Apple Inc. All rights reserved. + * Copyright (C) 2008, 2015-2016 Apple Inc. All rights reserved. * Copyright (C) 2009 Google Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -11,7 +11,7 @@ * 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. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -32,9 +32,12 @@ #include <limits> #include <wtf/Assertions.h> +#include <wtf/Condition.h> #include <wtf/Deque.h> +#include <wtf/Lock.h> #include <wtf/Noncopyable.h> #include <wtf/Threading.h> +#include <wtf/WallTime.h> namespace WTF { @@ -61,9 +64,10 @@ namespace WTF { std::unique_ptr<DataType> waitForMessage(); std::unique_ptr<DataType> tryGetMessage(); + Deque<std::unique_ptr<DataType>> takeAllMessages(); std::unique_ptr<DataType> tryGetMessageIgnoringKilled(); template<typename Predicate> - std::unique_ptr<DataType> waitForMessageFilteredWithTimeout(MessageQueueWaitResult&, Predicate&&, double absoluteTime); + std::unique_ptr<DataType> waitForMessageFilteredWithTimeout(MessageQueueWaitResult&, Predicate&&, WallTime absoluteTime); template<typename Predicate> void removeIf(Predicate&&); @@ -74,11 +78,9 @@ namespace WTF { // The result of isEmpty() is only valid if no other thread is manipulating the queue at the same time. bool isEmpty(); - static double infiniteTime() { return std::numeric_limits<double>::max(); } - private: - mutable Mutex m_mutex; - ThreadCondition m_condition; + mutable Lock m_mutex; + Condition m_condition; Deque<std::unique_ptr<DataType>> m_queue; bool m_killed; }; @@ -91,53 +93,53 @@ namespace WTF { template<typename DataType> inline void MessageQueue<DataType>::append(std::unique_ptr<DataType> message) { - MutexLocker lock(m_mutex); - m_queue.append(std::move(message)); - m_condition.signal(); + LockHolder lock(m_mutex); + m_queue.append(WTFMove(message)); + m_condition.notifyOne(); } template<typename DataType> inline void MessageQueue<DataType>::appendAndKill(std::unique_ptr<DataType> message) { - MutexLocker lock(m_mutex); - m_queue.append(std::move(message)); + LockHolder lock(m_mutex); + m_queue.append(WTFMove(message)); m_killed = true; - m_condition.broadcast(); + m_condition.notifyAll(); } // Returns true if the queue was empty before the item was added. template<typename DataType> inline bool MessageQueue<DataType>::appendAndCheckEmpty(std::unique_ptr<DataType> message) { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); bool wasEmpty = m_queue.isEmpty(); - m_queue.append(std::move(message)); - m_condition.signal(); + m_queue.append(WTFMove(message)); + m_condition.notifyOne(); return wasEmpty; } template<typename DataType> inline void MessageQueue<DataType>::prepend(std::unique_ptr<DataType> message) { - MutexLocker lock(m_mutex); - m_queue.prepend(std::move(message)); - m_condition.signal(); + LockHolder lock(m_mutex); + m_queue.prepend(WTFMove(message)); + m_condition.notifyOne(); } template<typename DataType> inline auto MessageQueue<DataType>::waitForMessage() -> std::unique_ptr<DataType> { MessageQueueWaitResult exitReason; - std::unique_ptr<DataType> result = waitForMessageFilteredWithTimeout(exitReason, [](const DataType&) { return true; }, infiniteTime()); + std::unique_ptr<DataType> result = waitForMessageFilteredWithTimeout(exitReason, [](const DataType&) { return true; }, WallTime::infinity()); ASSERT(exitReason == MessageQueueTerminated || exitReason == MessageQueueMessageReceived); return result; } template<typename DataType> template<typename Predicate> - inline auto MessageQueue<DataType>::waitForMessageFilteredWithTimeout(MessageQueueWaitResult& result, Predicate&& predicate, double absoluteTime) -> std::unique_ptr<DataType> + inline auto MessageQueue<DataType>::waitForMessageFilteredWithTimeout(MessageQueueWaitResult& result, Predicate&& predicate, WallTime absoluteTime) -> std::unique_ptr<DataType> { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); bool timedOut = false; auto found = m_queue.end(); @@ -149,10 +151,10 @@ namespace WTF { if (found != m_queue.end()) break; - timedOut = !m_condition.timedWait(m_mutex, absoluteTime); + timedOut = !m_condition.waitUntil(m_mutex, absoluteTime); } - ASSERT(!timedOut || absoluteTime != infiniteTime()); + ASSERT(!timedOut || absoluteTime != WallTime::infinity()); if (m_killed) { result = MessageQueueTerminated; @@ -165,7 +167,7 @@ namespace WTF { } ASSERT(found != m_queue.end()); - std::unique_ptr<DataType> message = std::move(*found); + std::unique_ptr<DataType> message = WTFMove(*found); m_queue.remove(found); result = MessageQueueMessageReceived; return message; @@ -174,7 +176,7 @@ namespace WTF { template<typename DataType> inline auto MessageQueue<DataType>::tryGetMessage() -> std::unique_ptr<DataType> { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); if (m_killed) return nullptr; if (m_queue.isEmpty()) @@ -184,9 +186,18 @@ namespace WTF { } template<typename DataType> + inline auto MessageQueue<DataType>::takeAllMessages() -> Deque<std::unique_ptr<DataType>> + { + LockHolder lock(m_mutex); + if (m_killed) + return { }; + return WTFMove(m_queue); + } + + template<typename DataType> inline auto MessageQueue<DataType>::tryGetMessageIgnoringKilled() -> std::unique_ptr<DataType> { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); if (m_queue.isEmpty()) return nullptr; @@ -197,7 +208,7 @@ namespace WTF { template<typename Predicate> inline void MessageQueue<DataType>::removeIf(Predicate&& predicate) { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); while (true) { auto found = m_queue.findIf([&predicate](const std::unique_ptr<DataType>& ptr) -> bool { ASSERT(ptr); @@ -214,7 +225,7 @@ namespace WTF { template<typename DataType> inline bool MessageQueue<DataType>::isEmpty() { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); if (m_killed) return true; return m_queue.isEmpty(); @@ -223,15 +234,15 @@ namespace WTF { template<typename DataType> inline void MessageQueue<DataType>::kill() { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); m_killed = true; - m_condition.broadcast(); + m_condition.notifyAll(); } template<typename DataType> inline bool MessageQueue<DataType>::killed() const { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); return m_killed; } } // namespace WTF |