Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / wtf / MessageQueue.h
1 /*
2  * Copyright (C) 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2009 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef MessageQueue_h
31 #define MessageQueue_h
32
33 #include <limits>
34 #include "wtf/Assertions.h"
35 #include "wtf/Deque.h"
36 #include "wtf/Noncopyable.h"
37 #include "wtf/PassOwnPtr.h"
38 #include "wtf/ThreadingPrimitives.h"
39
40 namespace WTF {
41
42     enum MessageQueueWaitResult {
43         MessageQueueTerminated,       // Queue was destroyed while waiting for message.
44         MessageQueueTimeout,          // Timeout was specified and it expired.
45         MessageQueueMessageReceived   // A message was successfully received and returned.
46     };
47
48     // The queue takes ownership of messages and transfer it to the new owner
49     // when messages are fetched from the queue.
50     // Essentially, MessageQueue acts as a queue of OwnPtr<DataType>.
51     template<typename DataType>
52     class MessageQueue {
53         WTF_MAKE_NONCOPYABLE(MessageQueue);
54     public:
55         MessageQueue() : m_killed(false) { }
56         ~MessageQueue();
57
58         // Returns true if the queue is still alive, false if the queue has been killed.
59         bool append(PassOwnPtr<DataType>);
60         void appendAndKill(PassOwnPtr<DataType>);
61         bool appendAndCheckEmpty(PassOwnPtr<DataType>);
62         void prepend(PassOwnPtr<DataType>);
63
64         PassOwnPtr<DataType> waitForMessage();
65         PassOwnPtr<DataType> tryGetMessage();
66         PassOwnPtr<DataType> tryGetMessageIgnoringKilled();
67         PassOwnPtr<DataType> waitForMessageWithTimeout(MessageQueueWaitResult&, double absoluteTime);
68
69         void kill();
70         bool killed() const;
71
72         // The result of isEmpty() is only valid if no other thread is manipulating the queue at the same time.
73         bool isEmpty();
74
75         static double infiniteTime() { return std::numeric_limits<double>::max(); }
76
77     private:
78         mutable Mutex m_mutex;
79         ThreadCondition m_condition;
80         Deque<DataType*> m_queue;
81         bool m_killed;
82     };
83
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)
92     {
93         MutexLocker lock(m_mutex);
94         m_queue.append(message.leakPtr());
95         m_condition.signal();
96         return !m_killed;
97     }
98
99     template<typename DataType>
100     inline void MessageQueue<DataType>::appendAndKill(PassOwnPtr<DataType> message)
101     {
102         MutexLocker lock(m_mutex);
103         m_queue.append(message.leakPtr());
104         m_killed = true;
105         m_condition.broadcast();
106     }
107
108     // Returns true if the queue was empty before the item was added.
109     template<typename DataType>
110     inline bool MessageQueue<DataType>::appendAndCheckEmpty(PassOwnPtr<DataType> message)
111     {
112         MutexLocker lock(m_mutex);
113         bool wasEmpty = m_queue.isEmpty();
114         m_queue.append(message.leakPtr());
115         m_condition.signal();
116         return wasEmpty;
117     }
118
119     template<typename DataType>
120     inline void MessageQueue<DataType>::prepend(PassOwnPtr<DataType> message)
121     {
122         MutexLocker lock(m_mutex);
123         m_queue.prepend(message.leakPtr());
124         m_condition.signal();
125     }
126
127     template<typename DataType>
128     inline PassOwnPtr<DataType> MessageQueue<DataType>::waitForMessage()
129     {
130         MessageQueueWaitResult exitReason;
131         OwnPtr<DataType> result = waitForMessageWithTimeout(exitReason, infiniteTime());
132         ASSERT(exitReason == MessageQueueTerminated || exitReason == MessageQueueMessageReceived);
133         return result.release();
134     }
135
136     template<typename DataType>
137     inline PassOwnPtr<DataType> MessageQueue<DataType>::waitForMessageWithTimeout(MessageQueueWaitResult& result, double absoluteTime)
138     {
139         MutexLocker lock(m_mutex);
140         bool timedOut = false;
141
142         while (!m_killed && !timedOut && m_queue.isEmpty())
143             timedOut = !m_condition.timedWait(m_mutex, absoluteTime);
144
145         ASSERT(!timedOut || absoluteTime != infiniteTime());
146
147         if (m_killed) {
148             result = MessageQueueTerminated;
149             return nullptr;
150         }
151
152         if (timedOut) {
153             result = MessageQueueTimeout;
154             return nullptr;
155         }
156
157         DequeConstIterator<DataType*> found = m_queue.begin();
158
159         ASSERT_WITH_SECURITY_IMPLICATION(found != m_queue.end());
160         OwnPtr<DataType> message = adoptPtr(*found);
161         m_queue.remove(found);
162         result = MessageQueueMessageReceived;
163         return message.release();
164     }
165
166     template<typename DataType>
167     inline PassOwnPtr<DataType> MessageQueue<DataType>::tryGetMessage()
168     {
169         MutexLocker lock(m_mutex);
170         if (m_killed)
171             return nullptr;
172         if (m_queue.isEmpty())
173             return nullptr;
174
175         return adoptPtr(m_queue.takeFirst());
176     }
177
178     template<typename DataType>
179     inline PassOwnPtr<DataType> MessageQueue<DataType>::tryGetMessageIgnoringKilled()
180     {
181         MutexLocker lock(m_mutex);
182         if (m_queue.isEmpty())
183             return nullptr;
184
185         return adoptPtr(m_queue.takeFirst());
186     }
187
188     template<typename DataType>
189     inline bool MessageQueue<DataType>::isEmpty()
190     {
191         MutexLocker lock(m_mutex);
192         if (m_killed)
193             return true;
194         return m_queue.isEmpty();
195     }
196
197     template<typename DataType>
198     inline void MessageQueue<DataType>::kill()
199     {
200         MutexLocker lock(m_mutex);
201         m_killed = true;
202         m_condition.broadcast();
203     }
204
205     template<typename DataType>
206     inline bool MessageQueue<DataType>::killed() const
207     {
208         MutexLocker lock(m_mutex);
209         return m_killed;
210     }
211 } // namespace WTF
212
213 using WTF::MessageQueue;
214 // MessageQueueWaitResult enum and all its values.
215 using WTF::MessageQueueWaitResult;
216 using WTF::MessageQueueTerminated;
217 using WTF::MessageQueueTimeout;
218 using WTF::MessageQueueMessageReceived;
219
220 #endif // MessageQueue_h