sptk2 logo
SPTK Home Page
SynchronizedList.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <functional>
30#include <list>
31#include <mutex>
32#include <sptk5/sptk.h>
33#include <sptk5/threads/Semaphore.h>
34
35namespace sptk {
36
47template<class T>
49{
50public:
58 using CallbackFunction = std::function<bool(T& item)>;
59
63 SynchronizedList() = default;
64
65 SynchronizedList(const SynchronizedList&) = delete;
66
67 SynchronizedList(SynchronizedList&&) noexcept = default;
68
69 SynchronizedList& operator=(const SynchronizedList&) = delete;
70
71 SynchronizedList& operator=(SynchronizedList&&) noexcept = default;
72
76 virtual ~SynchronizedList() = default;
77
85 virtual void push_front(const T& data)
86 {
87 std::scoped_lock lock(m_mutex);
88 m_list.push_front(data);
89 m_semaphore.post();
90 }
91
100 virtual bool pop_front(T& item, std::chrono::milliseconds timeout)
101 {
102 if (m_semaphore.sleep_for(timeout))
103 {
104 std::scoped_lock lock(m_mutex);
105 if (!m_list.empty())
106 {
107 item = m_list.front();
108 m_list.pop_front();
109 return true;
110 }
111 }
112 return false;
113 }
114
122 virtual void push_back(const T& data)
123 {
124 std::scoped_lock lock(m_mutex);
125 m_list.push_back(data);
126 m_semaphore.post();
127 }
128
137 virtual bool pop_back(T& item, std::chrono::milliseconds timeout)
138 {
139 if (m_semaphore.sleep_for(timeout))
140 {
141 std::scoped_lock lock(m_mutex);
142 if (!m_list.empty())
143 {
144 item = m_list.back();
145 m_list.pop_back();
146 return true;
147 }
148 }
149 return false;
150 }
151
155 virtual void remove(T& item)
156 {
157 std::scoped_lock lock(m_mutex);
158 m_list.remove(item);
159 }
160
166 virtual void wakeup()
167 {
168 m_semaphore.post();
169 }
170
174 bool empty() const
175 {
176 std::scoped_lock lock(m_mutex);
177 return m_list.empty();
178 }
179
183 size_t size() const
184 {
185 std::scoped_lock lock(m_mutex);
186 return m_list.size();
187 }
188
192 void clear()
193 {
194 std::scoped_lock lock(m_mutex);
195 m_list.clear();
196 }
197
204 bool each(const CallbackFunction& callbackFunction)
205 {
206 std::scoped_lock lock(m_mutex);
207 for (auto itor = m_list.begin(); itor != m_list.end(); ++itor)
208 {
209 if (!callbackFunction(*itor))
210 {
211 return false;
212 }
213 }
214 return true;
215 }
216
217private:
218 mutable std::mutex m_mutex;
219 Semaphore m_semaphore;
220 std::list<T> m_list;
221};
222
227}
Generic unnamed semaphore class.
Definition: Semaphore.h:52
bool sleep_for(std::chrono::milliseconds timeout)
Wait until semaphore value is greater than zero, or until timeout interval is passed.
void post()
Post the semaphore.
Definition: SynchronizedList.h:49
virtual bool pop_back(T &item, std::chrono::milliseconds timeout)
Definition: SynchronizedList.h:137
void clear()
Definition: SynchronizedList.h:192
virtual void push_front(const T &data)
Definition: SynchronizedList.h:85
size_t size() const
Definition: SynchronizedList.h:183
bool empty() const
Definition: SynchronizedList.h:174
std::function< bool(T &item)> CallbackFunction
Definition: SynchronizedList.h:58
virtual void push_back(const T &data)
Definition: SynchronizedList.h:122
virtual bool pop_front(T &item, std::chrono::milliseconds timeout)
Definition: SynchronizedList.h:100
virtual void wakeup()
Definition: SynchronizedList.h:166
bool each(const CallbackFunction &callbackFunction)
Definition: SynchronizedList.h:204
virtual void remove(T &item)
Definition: SynchronizedList.h:155

Fri Oct 14 2022 09:58:32: SPTK 5.4.1