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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/Condition.h>
29#include <wtf/Deque.h>
30#include <wtf/Lock.h>
31#include <wtf/ThreadSafeRefCounted.h>
32
33namespace WTF {
34
35template<typename T, size_t BufferSize>
36class SynchronizedFixedQueue : public ThreadSafeRefCounted<SynchronizedFixedQueue<T, BufferSize>> {
37public:
38 static Ref<SynchronizedFixedQueue> create()
39 {
40 return adoptRef(*new SynchronizedFixedQueue());
41 }
42
43 void open()
44 {
45 LockHolder lockHolder(m_mutex);
46 if (m_open)
47 return;
48
49 // Restore the queue to its original state.
50 m_open = true;
51 m_queue.clear();
52 }
53
54 void close()
55 {
56 LockHolder lockHolder(m_mutex);
57 if (!m_open)
58 return;
59
60 // Wake all the sleeping threads up with a closing state.
61 m_open = false;
62 m_condition.notifyAll();
63 }
64
65 bool isOpen()
66 {
67 LockHolder lockHolder(m_mutex);
68 return m_open;
69 }
70
71 bool enqueue(const T& value)
72 {
73 LockHolder lockHolder(m_mutex);
74
75 // Wait for an empty place to be available in the queue.
76 m_condition.wait(m_mutex, [this]() { return !m_open || m_queue.size() < BufferSize; });
77
78 // The queue is closing, exit immediately.
79 if (!m_open)
80 return false;
81
82 // Add the item in the queue.
83 m_queue.append(value);
84
85 // Notify the other threads that an item was added to the queue.
86 m_condition.notifyAll();
87 return true;
88 }
89
90 bool dequeue(T& value)
91 {
92 LockHolder lockHolder(m_mutex);
93
94 // Wait for an item to be added.
95 m_condition.wait(m_mutex, [this]() { return !m_open || m_queue.size(); });
96
97 // The queue is closing, exit immediately.
98 if (!m_open)
99 return false;
100
101 // Get a copy from m_queue.first and then remove it.
102 value = m_queue.first();
103 m_queue.removeFirst();
104
105 // Notify the other threads that an item was removed from the queue.
106 m_condition.notifyAll();
107 return true;
108 }
109
110private:
111 SynchronizedFixedQueue()
112 {
113 static_assert(!((BufferSize - 1) & BufferSize), "BufferSize must be power of 2.");
114 }
115
116 Lock m_mutex;
117 Condition m_condition;
118
119 bool m_open { true };
120 Deque<T, BufferSize> m_queue;
121};
122
123}
124
125using WTF::SynchronizedFixedQueue;
126