From 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 27 Jun 2017 06:07:23 +0000 Subject: webkitgtk-2.16.5 --- Source/WTF/wtf/MessageQueue.h | 75 +++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 32 deletions(-) (limited to 'Source/WTF/wtf/MessageQueue.h') 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 #include +#include #include +#include #include #include +#include namespace WTF { @@ -61,9 +64,10 @@ namespace WTF { std::unique_ptr waitForMessage(); std::unique_ptr tryGetMessage(); + Deque> takeAllMessages(); std::unique_ptr tryGetMessageIgnoringKilled(); template - std::unique_ptr waitForMessageFilteredWithTimeout(MessageQueueWaitResult&, Predicate&&, double absoluteTime); + std::unique_ptr waitForMessageFilteredWithTimeout(MessageQueueWaitResult&, Predicate&&, WallTime absoluteTime); template 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::max(); } - private: - mutable Mutex m_mutex; - ThreadCondition m_condition; + mutable Lock m_mutex; + Condition m_condition; Deque> m_queue; bool m_killed; }; @@ -91,53 +93,53 @@ namespace WTF { template inline void MessageQueue::append(std::unique_ptr 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 inline void MessageQueue::appendAndKill(std::unique_ptr 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 inline bool MessageQueue::appendAndCheckEmpty(std::unique_ptr 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 inline void MessageQueue::prepend(std::unique_ptr 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 inline auto MessageQueue::waitForMessage() -> std::unique_ptr { MessageQueueWaitResult exitReason; - std::unique_ptr result = waitForMessageFilteredWithTimeout(exitReason, [](const DataType&) { return true; }, infiniteTime()); + std::unique_ptr result = waitForMessageFilteredWithTimeout(exitReason, [](const DataType&) { return true; }, WallTime::infinity()); ASSERT(exitReason == MessageQueueTerminated || exitReason == MessageQueueMessageReceived); return result; } template template - inline auto MessageQueue::waitForMessageFilteredWithTimeout(MessageQueueWaitResult& result, Predicate&& predicate, double absoluteTime) -> std::unique_ptr + inline auto MessageQueue::waitForMessageFilteredWithTimeout(MessageQueueWaitResult& result, Predicate&& predicate, WallTime absoluteTime) -> std::unique_ptr { - 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 message = std::move(*found); + std::unique_ptr message = WTFMove(*found); m_queue.remove(found); result = MessageQueueMessageReceived; return message; @@ -174,7 +176,7 @@ namespace WTF { template inline auto MessageQueue::tryGetMessage() -> std::unique_ptr { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); if (m_killed) return nullptr; if (m_queue.isEmpty()) @@ -183,10 +185,19 @@ namespace WTF { return m_queue.takeFirst(); } + template + inline auto MessageQueue::takeAllMessages() -> Deque> + { + LockHolder lock(m_mutex); + if (m_killed) + return { }; + return WTFMove(m_queue); + } + template inline auto MessageQueue::tryGetMessageIgnoringKilled() -> std::unique_ptr { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); if (m_queue.isEmpty()) return nullptr; @@ -197,7 +208,7 @@ namespace WTF { template inline void MessageQueue::removeIf(Predicate&& predicate) { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); while (true) { auto found = m_queue.findIf([&predicate](const std::unique_ptr& ptr) -> bool { ASSERT(ptr); @@ -214,7 +225,7 @@ namespace WTF { template inline bool MessageQueue::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 inline void MessageQueue::kill() { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); m_killed = true; - m_condition.broadcast(); + m_condition.notifyAll(); } template inline bool MessageQueue::killed() const { - MutexLocker lock(m_mutex); + LockHolder lock(m_mutex); return m_killed; } } // namespace WTF -- cgit v1.2.1