forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.h
260 lines (236 loc) · 6.44 KB
/
Queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#pragma once
#include <queue>
#include <deque>
#include <list>
#include <vector>
#include <algorithm>
#include <mutex>
#include <condition_variable>
#include <atomic>
#define SHOW_QUE_LOG 0
#if SHOW_QUE_LOG
///
/// \brief currTime
/// \return
///
inline std::string CurrTime_()
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "%d.%m.%Y %H:%M:%S:");
return oss.str();
}
#define QUE_LOG std::cout << CurrTime_()
#define QUE_ERR_LOG (std::cerr << CurrTime_())
#endif
struct FrameInfo;
typedef std::shared_ptr<FrameInfo> frame_ptr;
///
/// A threadsafe-queue with Frames
///
class FramesQueue
{
private:
typedef std::list<frame_ptr> queue_t;
public:
///
/// \brief FramesQueue
///
FramesQueue()
: m_que(),
m_mutex(),
m_cond(),
m_break(false)
{}
FramesQueue(const FramesQueue&) = delete;
FramesQueue(FramesQueue&&) = delete;
FramesQueue& operator=(const FramesQueue&) = delete;
FramesQueue& operator=(FramesQueue&&) = delete;
///
~FramesQueue(void) = default;
///
/// \brief AddNewFrame
/// \param frameInfo
///
void AddNewFrame(frame_ptr frameInfo, size_t maxQueueSize)
{
#if SHOW_QUE_LOG
QUE_LOG << "AddNewFrame start: " << frameInfo->m_dt << std::endl;
#endif
std::lock_guard<std::mutex> lock(m_mutex);
if (!maxQueueSize || (maxQueueSize > 0 && m_que.size() < maxQueueSize))
m_que.push_back(frameInfo);
#if SHOW_QUE_LOG
QUE_LOG << "AddNewFrame end: " << frameInfo->m_dt << ", frameInd " << frameInfo->m_frameInd << ", queue size " << m_que.size() << std::endl;
#endif
m_cond.notify_all();
}
#if SHOW_QUE_LOG
///
/// \brief PrintQue
///
void PrintQue()
{
QUE_LOG << "m_que (" << m_que.size() << "): ";
size_t i = 0;
for (auto it : m_que)
{
if (it->m_inDetector.load() && it->m_inTracker.load())
std::cout << i << " d" << it->m_inDetector.load() << " t" << it->m_inTracker.load() << "; ";
else if (it->m_inDetector.load())
std::cout << i << " d" << it->m_inDetector.load() << "; ";
else if (it->m_inTracker.load())
std::cout << i << " t" << it->m_inTracker.load() << "; ";
else
std::cout << i << "; ";
++i;
}
std::cout << std::endl;
}
#endif
///
/// \brief GetLastUndetectedFrame
/// \return
///
frame_ptr GetLastUndetectedFrame()
{
#if SHOW_QUE_LOG
QUE_LOG << "GetLastUndetectedFrame start" << std::endl;
#endif
std::unique_lock<std::mutex> lock(m_mutex);
while (m_que.empty() || m_que.back()->m_inDetector.load())
{
if (m_break.load())
break;
m_cond.wait(lock);
//PrintQue();
}
if (!m_break.load())
{
frame_ptr frameInfo = m_que.back();
assert(frameInfo->m_inDetector.load() == 0);
assert(frameInfo->m_inTracker.load());
frameInfo->m_inDetector.store(1);
#if SHOW_QUE_LOG
QUE_LOG << "GetLastUndetectedFrame end: " << frameInfo->m_dt << ", frameInd " << frameInfo->m_frameInd << std::endl;
#endif
return frameInfo;
}
return nullptr;
}
///
/// \brief SearchUntracked
/// \return
///
queue_t::iterator SearchUntracked()
{
queue_t::iterator res_it = m_que.end();
for (queue_t::iterator it = m_que.begin(); it != m_que.end(); ++it)
{
if ((*it)->m_inDetector.load() == 1)
break;
else if ((*it)->m_inTracker.load() == 0)
{
res_it = it;
break;
}
}
return res_it;
}
///
/// \brief GetFirstDetectedFrame
/// \return
///
frame_ptr GetFirstDetectedFrame()
{
#if SHOW_QUE_LOG
QUE_LOG << "GetFirstDetectedFrame start" << std::endl;
#endif
std::unique_lock<std::mutex> lock(m_mutex);
queue_t::iterator it = SearchUntracked();
while (it == m_que.end())
{
if (m_break.load())
break;
m_cond.wait(lock);
it = SearchUntracked();
//PrintQue();
}
if (!m_break.load())
{
frame_ptr frameInfo = *it;
assert(frameInfo->m_inTracker.load() == 0);
assert(frameInfo->m_inDetector.load() != 1);
frameInfo->m_inTracker.store(1);
#if SHOW_QUE_LOG
QUE_LOG << "GetFirstDetectedFrame end: " << frameInfo->m_dt << ", frameInd " << frameInfo->m_frameInd << std::endl;
#endif
return frameInfo;
}
return nullptr;
}
///
/// \brief GetFirstProcessedFrame
/// \return
///
frame_ptr GetFirstProcessedFrame()
{
#if SHOW_QUE_LOG
QUE_LOG << "GetFirstProcessedFrame start" << std::endl;
#endif
std::unique_lock<std::mutex> lock(m_mutex);
while (m_que.empty() || m_que.front()->m_inTracker.load() != 2)
{
if (m_break.load())
break;
m_cond.wait(lock);
//PrintQue();
}
if (!m_break.load())
{
frame_ptr frameInfo = m_que.front();
m_que.pop_front();
#if SHOW_QUE_LOG
QUE_LOG << "GetFirstProcessedFrame end: " << frameInfo->m_dt << ", frameInd " << frameInfo->m_frameInd << std::endl;
#endif
return frameInfo;
}
return nullptr;
}
///
/// \brief Signal
///
void Signal(
#if SHOW_QUE_LOG
int64 ts
#else
int64 /*ts*/
#endif
)
{
#if SHOW_QUE_LOG
QUE_LOG << "Signal start:" << ts << std::endl;
#endif
m_cond.notify_all();
#if SHOW_QUE_LOG
QUE_LOG << "Signal end: " << ts << std::endl;
#endif
}
void SetBreak(bool val)
{
#if SHOW_QUE_LOG
QUE_LOG << "SetBreak start:" << val << std::endl;
#endif
m_break = val;
Signal(0);
#if SHOW_QUE_LOG
QUE_LOG << "SetBreak end:" << val << std::endl;
#endif
}
private:
queue_t m_que;
mutable std::mutex m_mutex;
std::condition_variable m_cond;
std::atomic<bool> m_break;
};