| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2008 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Google Inc. All rights reserved. | 3 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 */ | 28 */ |
| 29 | 29 |
| 30 #ifndef MessageQueue_h | 30 #ifndef MessageQueue_h |
| 31 #define MessageQueue_h | 31 #define MessageQueue_h |
| 32 | 32 |
| 33 #include <limits> | 33 #include <limits> |
| 34 #include "wtf/Assertions.h" | 34 #include "wtf/Assertions.h" |
| 35 #include "wtf/Deque.h" | 35 #include "wtf/Deque.h" |
| 36 #include "wtf/Noncopyable.h" | 36 #include "wtf/Noncopyable.h" |
| 37 #include "wtf/OwnPtr.h" |
| 37 #include "wtf/PassOwnPtr.h" | 38 #include "wtf/PassOwnPtr.h" |
| 38 #include "wtf/ThreadingPrimitives.h" | 39 #include "wtf/ThreadingPrimitives.h" |
| 39 | 40 |
| 40 namespace WTF { | 41 namespace WTF { |
| 41 | 42 |
| 42 enum MessageQueueWaitResult { | 43 enum MessageQueueWaitResult { |
| 43 MessageQueueTerminated, // Queue was destroyed while waiting for m
essage. | 44 MessageQueueTerminated, // Queue was destroyed while waiting for m
essage. |
| 44 MessageQueueTimeout, // Timeout was specified and it expired. | 45 MessageQueueTimeout, // Timeout was specified and it expired. |
| 45 MessageQueueMessageReceived // A message was successfully received and
returned. | 46 MessageQueueMessageReceived // A message was successfully received and
returned. |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 // The queue takes ownership of messages and transfer it to the new owner | 49 // The queue takes ownership of messages and transfer it to the new owner |
| 49 // when messages are fetched from the queue. | 50 // when messages are fetched from the queue. |
| 50 // Essentially, MessageQueue acts as a queue of OwnPtr<DataType>. | 51 // Essentially, MessageQueue acts as a queue of OwnPtr<DataType>. |
| 51 template<typename DataType> | 52 template<typename DataType> |
| 52 class MessageQueue { | 53 class MessageQueue { |
| 53 WTF_MAKE_NONCOPYABLE(MessageQueue); | 54 WTF_MAKE_NONCOPYABLE(MessageQueue); |
| 54 public: | 55 public: |
| 55 MessageQueue() : m_killed(false) { } | 56 MessageQueue() : m_killed(false) { } |
| 56 ~MessageQueue(); | |
| 57 | 57 |
| 58 // Returns true if the queue is still alive, false if the queue has been
killed. | 58 // Returns true if the queue is still alive, false if the queue has been
killed. |
| 59 bool append(PassOwnPtr<DataType>); | 59 bool append(PassOwnPtr<DataType>); |
| 60 void appendAndKill(PassOwnPtr<DataType>); | 60 void appendAndKill(PassOwnPtr<DataType>); |
| 61 bool appendAndCheckEmpty(PassOwnPtr<DataType>); | 61 bool appendAndCheckEmpty(PassOwnPtr<DataType>); |
| 62 void prepend(PassOwnPtr<DataType>); | 62 void prepend(PassOwnPtr<DataType>); |
| 63 | 63 |
| 64 PassOwnPtr<DataType> waitForMessage(); | 64 PassOwnPtr<DataType> waitForMessage(); |
| 65 PassOwnPtr<DataType> tryGetMessage(); | 65 PassOwnPtr<DataType> tryGetMessage(); |
| 66 PassOwnPtr<DataType> tryGetMessageIgnoringKilled(); | 66 PassOwnPtr<DataType> tryGetMessageIgnoringKilled(); |
| 67 PassOwnPtr<DataType> waitForMessageWithTimeout(MessageQueueWaitResult&,
double absoluteTime); | 67 PassOwnPtr<DataType> waitForMessageWithTimeout(MessageQueueWaitResult&,
double absoluteTime); |
| 68 | 68 |
| 69 void kill(); | 69 void kill(); |
| 70 bool killed() const; | 70 bool killed() const; |
| 71 | 71 |
| 72 // The result of isEmpty() is only valid if no other thread is manipulat
ing the queue at the same time. | 72 // The result of isEmpty() is only valid if no other thread is manipulat
ing the queue at the same time. |
| 73 bool isEmpty(); | 73 bool isEmpty(); |
| 74 | 74 |
| 75 static double infiniteTime() { return std::numeric_limits<double>::max()
; } | 75 static double infiniteTime() { return std::numeric_limits<double>::max()
; } |
| 76 | 76 |
| 77 private: | 77 private: |
| 78 mutable Mutex m_mutex; | 78 mutable Mutex m_mutex; |
| 79 ThreadCondition m_condition; | 79 ThreadCondition m_condition; |
| 80 Deque<DataType*> m_queue; | 80 Deque<OwnPtr<DataType> > m_queue; |
| 81 bool m_killed; | 81 bool m_killed; |
| 82 }; | 82 }; |
| 83 | 83 |
| 84 template<typename DataType> | 84 template<typename DataType> |
| 85 MessageQueue<DataType>::~MessageQueue() | |
| 86 { | |
| 87 deleteAllValues(m_queue); | |
| 88 } | |
| 89 | |
| 90 template<typename DataType> | |
| 91 inline bool MessageQueue<DataType>::append(PassOwnPtr<DataType> message) | 85 inline bool MessageQueue<DataType>::append(PassOwnPtr<DataType> message) |
| 92 { | 86 { |
| 93 MutexLocker lock(m_mutex); | 87 MutexLocker lock(m_mutex); |
| 94 m_queue.append(message.leakPtr()); | 88 m_queue.append(message); |
| 95 m_condition.signal(); | 89 m_condition.signal(); |
| 96 return !m_killed; | 90 return !m_killed; |
| 97 } | 91 } |
| 98 | 92 |
| 99 template<typename DataType> | 93 template<typename DataType> |
| 100 inline void MessageQueue<DataType>::appendAndKill(PassOwnPtr<DataType> messa
ge) | 94 inline void MessageQueue<DataType>::appendAndKill(PassOwnPtr<DataType> messa
ge) |
| 101 { | 95 { |
| 102 MutexLocker lock(m_mutex); | 96 MutexLocker lock(m_mutex); |
| 103 m_queue.append(message.leakPtr()); | 97 m_queue.append(message); |
| 104 m_killed = true; | 98 m_killed = true; |
| 105 m_condition.broadcast(); | 99 m_condition.broadcast(); |
| 106 } | 100 } |
| 107 | 101 |
| 108 // Returns true if the queue was empty before the item was added. | 102 // Returns true if the queue was empty before the item was added. |
| 109 template<typename DataType> | 103 template<typename DataType> |
| 110 inline bool MessageQueue<DataType>::appendAndCheckEmpty(PassOwnPtr<DataType>
message) | 104 inline bool MessageQueue<DataType>::appendAndCheckEmpty(PassOwnPtr<DataType>
message) |
| 111 { | 105 { |
| 112 MutexLocker lock(m_mutex); | 106 MutexLocker lock(m_mutex); |
| 113 bool wasEmpty = m_queue.isEmpty(); | 107 bool wasEmpty = m_queue.isEmpty(); |
| 114 m_queue.append(message.leakPtr()); | 108 m_queue.append(message); |
| 115 m_condition.signal(); | 109 m_condition.signal(); |
| 116 return wasEmpty; | 110 return wasEmpty; |
| 117 } | 111 } |
| 118 | 112 |
| 119 template<typename DataType> | 113 template<typename DataType> |
| 120 inline void MessageQueue<DataType>::prepend(PassOwnPtr<DataType> message) | 114 inline void MessageQueue<DataType>::prepend(PassOwnPtr<DataType> message) |
| 121 { | 115 { |
| 122 MutexLocker lock(m_mutex); | 116 MutexLocker lock(m_mutex); |
| 123 m_queue.prepend(message.leakPtr()); | 117 m_queue.prepend(message); |
| 124 m_condition.signal(); | 118 m_condition.signal(); |
| 125 } | 119 } |
| 126 | 120 |
| 127 template<typename DataType> | 121 template<typename DataType> |
| 128 inline PassOwnPtr<DataType> MessageQueue<DataType>::waitForMessage() | 122 inline PassOwnPtr<DataType> MessageQueue<DataType>::waitForMessage() |
| 129 { | 123 { |
| 130 MessageQueueWaitResult exitReason; | 124 MessageQueueWaitResult exitReason; |
| 131 OwnPtr<DataType> result = waitForMessageWithTimeout(exitReason, infinite
Time()); | 125 OwnPtr<DataType> result = waitForMessageWithTimeout(exitReason, infinite
Time()); |
| 132 ASSERT(exitReason == MessageQueueTerminated || exitReason == MessageQueu
eMessageReceived); | 126 ASSERT(exitReason == MessageQueueTerminated || exitReason == MessageQueu
eMessageReceived); |
| 133 return result.release(); | 127 return result.release(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 147 if (m_killed) { | 141 if (m_killed) { |
| 148 result = MessageQueueTerminated; | 142 result = MessageQueueTerminated; |
| 149 return nullptr; | 143 return nullptr; |
| 150 } | 144 } |
| 151 | 145 |
| 152 if (timedOut) { | 146 if (timedOut) { |
| 153 result = MessageQueueTimeout; | 147 result = MessageQueueTimeout; |
| 154 return nullptr; | 148 return nullptr; |
| 155 } | 149 } |
| 156 | 150 |
| 157 DequeConstIterator<DataType*> found = m_queue.begin(); | 151 ASSERT_WITH_SECURITY_IMPLICATION(!m_queue.isEmpty()); |
| 152 result = MessageQueueMessageReceived; |
| 158 | 153 |
| 159 ASSERT_WITH_SECURITY_IMPLICATION(found != m_queue.end()); | 154 return m_queue.takeFirst(); |
| 160 OwnPtr<DataType> message = adoptPtr(*found); | |
| 161 m_queue.remove(found); | |
| 162 result = MessageQueueMessageReceived; | |
| 163 return message.release(); | |
| 164 } | 155 } |
| 165 | 156 |
| 166 template<typename DataType> | 157 template<typename DataType> |
| 167 inline PassOwnPtr<DataType> MessageQueue<DataType>::tryGetMessage() | 158 inline PassOwnPtr<DataType> MessageQueue<DataType>::tryGetMessage() |
| 168 { | 159 { |
| 169 MutexLocker lock(m_mutex); | 160 MutexLocker lock(m_mutex); |
| 170 if (m_killed) | 161 if (m_killed) |
| 171 return nullptr; | 162 return nullptr; |
| 172 if (m_queue.isEmpty()) | 163 if (m_queue.isEmpty()) |
| 173 return nullptr; | 164 return nullptr; |
| 174 | 165 |
| 175 return adoptPtr(m_queue.takeFirst()); | 166 return m_queue.takeFirst(); |
| 176 } | 167 } |
| 177 | 168 |
| 178 template<typename DataType> | 169 template<typename DataType> |
| 179 inline PassOwnPtr<DataType> MessageQueue<DataType>::tryGetMessageIgnoringKil
led() | 170 inline PassOwnPtr<DataType> MessageQueue<DataType>::tryGetMessageIgnoringKil
led() |
| 180 { | 171 { |
| 181 MutexLocker lock(m_mutex); | 172 MutexLocker lock(m_mutex); |
| 182 if (m_queue.isEmpty()) | 173 if (m_queue.isEmpty()) |
| 183 return nullptr; | 174 return nullptr; |
| 184 | 175 |
| 185 return adoptPtr(m_queue.takeFirst()); | 176 return m_queue.takeFirst(); |
| 186 } | 177 } |
| 187 | 178 |
| 188 template<typename DataType> | 179 template<typename DataType> |
| 189 inline bool MessageQueue<DataType>::isEmpty() | 180 inline bool MessageQueue<DataType>::isEmpty() |
| 190 { | 181 { |
| 191 MutexLocker lock(m_mutex); | 182 MutexLocker lock(m_mutex); |
| 192 if (m_killed) | 183 if (m_killed) |
| 193 return true; | 184 return true; |
| 194 return m_queue.isEmpty(); | 185 return m_queue.isEmpty(); |
| 195 } | 186 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 211 } // namespace WTF | 202 } // namespace WTF |
| 212 | 203 |
| 213 using WTF::MessageQueue; | 204 using WTF::MessageQueue; |
| 214 // MessageQueueWaitResult enum and all its values. | 205 // MessageQueueWaitResult enum and all its values. |
| 215 using WTF::MessageQueueWaitResult; | 206 using WTF::MessageQueueWaitResult; |
| 216 using WTF::MessageQueueTerminated; | 207 using WTF::MessageQueueTerminated; |
| 217 using WTF::MessageQueueTimeout; | 208 using WTF::MessageQueueTimeout; |
| 218 using WTF::MessageQueueMessageReceived; | 209 using WTF::MessageQueueMessageReceived; |
| 219 | 210 |
| 220 #endif // MessageQueue_h | 211 #endif // MessageQueue_h |
| OLD | NEW |