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 | |
36 | namespace WTF { |
37 | |
38 | template<typename DataType> |
39 | class CrossThreadQueue final { |
40 | WTF_MAKE_FAST_ALLOCATED; |
41 | WTF_MAKE_NONCOPYABLE(CrossThreadQueue); |
42 | public: |
43 | CrossThreadQueue() = default; |
44 | |
45 | void append(DataType&&); |
46 | |
47 | DataType waitForMessage(); |
48 | Optional<DataType> tryGetMessage(); |
49 | |
50 | void kill(); |
51 | bool isKilled() const; |
52 | bool isEmpty() const; |
53 | |
54 | private: |
55 | Deque<DataType> m_queue; |
56 | mutable Lock m_lock; |
57 | Condition m_condition; |
58 | bool m_killed { false }; |
59 | }; |
60 | |
61 | template<typename DataType> |
62 | void CrossThreadQueue<DataType>::append(DataType&& message) |
63 | { |
64 | LockHolder lock(m_lock); |
65 | ASSERT(!m_killed); |
66 | m_queue.append(WTFMove(message)); |
67 | m_condition.notifyOne(); |
68 | } |
69 | |
70 | template<typename DataType> |
71 | DataType CrossThreadQueue<DataType>::waitForMessage() |
72 | { |
73 | LockHolder lock(m_lock); |
74 | |
75 | auto found = m_queue.end(); |
76 | while (found == m_queue.end()) { |
77 | found = m_queue.begin(); |
78 | if (found != m_queue.end()) |
79 | break; |
80 | |
81 | m_condition.wait(m_lock); |
82 | } |
83 | |
84 | return m_queue.takeFirst(); |
85 | } |
86 | |
87 | template<typename DataType> |
88 | Optional<DataType> CrossThreadQueue<DataType>::tryGetMessage() |
89 | { |
90 | LockHolder lock(m_lock); |
91 | |
92 | if (m_queue.isEmpty()) |
93 | return { }; |
94 | |
95 | return m_queue.takeFirst(); |
96 | } |
97 | |
98 | template<typename DataType> |
99 | void CrossThreadQueue<DataType>::kill() |
100 | { |
101 | LockHolder lock(m_lock); |
102 | m_killed = true; |
103 | m_condition.notifyAll(); |
104 | } |
105 | |
106 | template<typename DataType> |
107 | bool CrossThreadQueue<DataType>::isKilled() const |
108 | { |
109 | LockHolder lock(m_lock); |
110 | return m_killed; |
111 | } |
112 | |
113 | template<typename DataType> |
114 | bool CrossThreadQueue<DataType>::isEmpty() const |
115 | { |
116 | LockHolder lock(m_lock); |
117 | return m_queue.isEmpty(); |
118 | } |
119 | |
120 | } // namespace WTF |
121 | |
122 | using WTF::CrossThreadQueue; |
123 | |