1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <limits>
29#include <wtf/Assertions.h>
30#include <wtf/Condition.h>
31#include <wtf/Deque.h>
32#include <wtf/Lock.h>
33#include <wtf/Noncopyable.h>
34#include <wtf/Optional.h>
35
36namespace WTF {
37
38template<typename DataType>
39class CrossThreadQueue {
40 WTF_MAKE_NONCOPYABLE(CrossThreadQueue);
41public:
42 CrossThreadQueue() = default;
43
44 void append(DataType&&);
45
46 DataType waitForMessage();
47 Optional<DataType> tryGetMessage();
48
49 void kill();
50 bool isKilled() const;
51 bool isEmpty() const;
52
53private:
54 Deque<DataType> m_queue;
55 mutable Lock m_lock;
56 Condition m_condition;
57 bool m_killed { false };
58};
59
60template<typename DataType>
61void CrossThreadQueue<DataType>::append(DataType&& message)
62{
63 LockHolder lock(m_lock);
64 ASSERT(!m_killed);
65 m_queue.append(WTFMove(message));
66 m_condition.notifyOne();
67}
68
69template<typename DataType>
70DataType CrossThreadQueue<DataType>::waitForMessage()
71{
72 LockHolder lock(m_lock);
73
74 auto found = m_queue.end();
75 while (found == m_queue.end()) {
76 found = m_queue.begin();
77 if (found != m_queue.end())
78 break;
79
80 m_condition.wait(m_lock);
81 }
82
83 return m_queue.takeFirst();
84}
85
86template<typename DataType>
87Optional<DataType> CrossThreadQueue<DataType>::tryGetMessage()
88{
89 LockHolder lock(m_lock);
90
91 if (m_queue.isEmpty())
92 return { };
93
94 return m_queue.takeFirst();
95}
96
97template<typename DataType>
98void CrossThreadQueue<DataType>::kill()
99{
100 LockHolder lock(m_lock);
101 m_killed = true;
102 m_condition.notifyAll();
103}
104
105template<typename DataType>
106bool CrossThreadQueue<DataType>::isKilled() const
107{
108 LockHolder lock(m_lock);
109 return m_killed;
110}
111
112template<typename DataType>
113bool CrossThreadQueue<DataType>::isEmpty() const
114{
115 LockHolder lock(m_lock);
116 return m_queue.isEmpty();
117}
118
119} // namespace WTF
120
121using WTF::CrossThreadQueue;
122