-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
265 lines (230 loc) · 6.83 KB
/
Queue.cpp
File metadata and controls
265 lines (230 loc) · 6.83 KB
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
261
262
263
264
265
#include <iostream>
#include <algorithm>
#include <memory>
#include <assert.h>
#include "Queue.h"
// caffe
#include <caffe/caffe.hpp>
#include <caffe/layers/memory_data_layer.hpp>
using namespace MQ;
namespace MQ {
template <class T>
class Worker
{
public:
Worker(T & queue_, int gpu_id_, int gpu_num_,
int handler_id_, const std::string& model_path_,
dev::ConfigParams* cp_ = nullptr)
: queue(queue_), gpu_id(gpu_id_), gpu_num(gpu_num_),
handler_id(handler_id_), model_path(model_path_),
status(true), cp(cp_)
{
//std::cout << "hbk in work() and start to create ... \n";
thread = new std::thread(&Worker::run, this);
//std::cout << "hbk in work() and end create ... \n";
}
~Worker()
{
if (thread) {
delete thread;
thread = nullptr;
}
}
void run()
{
#ifdef USE_CAFFE_CPU
std::cout << "Worker::Run CPU" << std::endl;
caffe::Caffe::set_mode(caffe::Caffe::CPU);
#else
std::cout << "Worker::Run GPU" << std::endl;
caffe::Caffe::set_mode(caffe::Caffe::GPU);
caffe::Caffe::SetDevice(gpu_id);
caffe::Caffe::set_solver_count(gpu_num);
#endif
//std::cout << "hbk in work::run() ... \n";
Message message;
std::shared_ptr<HandlerBase> handler =
HandlerFactory::instance()->create(handler_id, model_path, cp);
while (status) {
if (queue.get(message)) {
//std::cout << "\nget one message in Queue and message type is : " << message.msgType << "\n";
std::string response;
response = handler->handle(message);
message.call(response);
//std::cout << "have done the detect and the result is : \n" << response << "\n\n";
}
}
}
void shutdown()
{
status = false;
join();
}
private:
void join()
{
if (thread->joinable())
thread->join();
}
T& queue;
std::thread* thread;
int gpu_id;
int gpu_num;
int handler_id;
std::string model_path;
bool status;
dev::ConfigParams* cp;
};
}
DQueue::DQueue(dev::ConfigParams* cp_, int worker_count_, const std::string& model_path_,
int handler_id_, int gpu_num_) : status(true)
{
HandlerFactory::instance();
// assert(workerCount_ > 0);
try {
//std::cout << "hbk in DQueue()... \n";
for (int i = 0; i < worker_count_; i++)
threads.push_back(
new Worker<DQueue>(*this, gpu_num_ > 1 ? i%gpu_num_ : 0,
gpu_num_, handler_id_, model_path_, cp_));
//std::cout << "hbk in DQueue() and end ... \n";
} catch (std::exception &e) {
LOG(ERROR) << "Thread init failed!";
LOG(ERROR) << e.what();
} catch (...) {
LOG(ERROR) << "Thread init failed!";
}
}
DQueue::~DQueue()
{
shutdown();
for (auto it : threads) {
if (!it) {
delete it;
it = 0;
}
}
threads.clear();
}
int DQueue::push(const Message& message)
{
{
std::lock_guard<std::mutex> lock(mtx);
msg_list.emplace_back(message);
}
condition.notify_one();
return 0;
}
bool DQueue::get(Message& message, const uint16_t &time_ms)
{
std::unique_lock<std::mutex> lock(mtx);
if (!status)
return false;
if (!condition.wait_for(lock, std::chrono::milliseconds(time_ms), [&] { return !msg_list.empty(); }) )
return false;
// while (msg_list.empty())
// condition.wait(lock);
//std::cout << "hbk in DQueue::get() : \n";
//std::cout << "before pop msg_list.size() : " << msg_list.size() << "\n";
message = msg_list.front();
msg_list.pop_front();
//std::cout << "after pop msg_list.size() : " << msg_list.size() << "\n";
return true;
}
void DQueue::shutdown()
{
std::lock_guard<std::mutex> lock(mtx);
status = false;
condition.notify_all();
for (auto it : threads) {
if (!it) {
it->shutdown();
}
}
}
BQueue::BQueue(int worker_count_,
const std::string& model_path_,
int handler_id_,
int gpu_num_,
int capacity_)
: capacity(capacity_), queue(std::vector<Message>(capacity))
{
HandlerFactory::instance();
// assert(workerCount_ > 0);
try {
for (int i = 0; i < worker_count_; i++)
threads.push_back(
new Worker<BQueue>(*this, gpu_num_ > 1 ? i%gpu_num_ : 0,
gpu_num_, handler_id_, model_path_));
} catch (std::exception &e) {
LOG(ERROR) << "Thread init failed!";
LOG(ERROR) << e.what();
} catch (...) {
LOG(ERROR) << "Thread init failed!";
}
}
BQueue::~BQueue()
{
shutdown();
// join();
for (auto it : threads) {
if (!it) {
delete it;
it = 0;
}
}
threads.clear();
}
bool BQueue::push(const Message &item)
{
{
std::unique_lock<std::mutex> lck(mtx);
notFull.wait(lck, [&] { return (tail + 1) % capacity != head; });
// if (!notFull.wait_for(lck, std::chrono::milliseconds(1000), [&] { return (tail + 1) % capacity != head; }))
// return false;
// while ((tail + 1) % capacity == head) //is full
// notFull.wait(lck);
queue[tail] = item;
tail = (tail + 1) % capacity;
}
//wake up get thread
notEmpty.notify_one();
return true;
}
bool BQueue::get(Message &msg, const uint16_t &time_ms)
{
{
std::unique_lock<std::mutex> lck(mtx);
if (!notEmpty.wait_for(lck, std::chrono::milliseconds(time_ms), [&] { return head != tail; }))
return false;
msg = queue[head];
head = (head + 1) % capacity;
}
notFull.notify_one();
return true;
}
void BQueue::getNow(Message& msg)
{
{
std::unique_lock<std::mutex> lck(mtx);
notEmpty.wait(lck, [&] { return head != tail; });
// while (head == tail) // is empty
// notEmpty.wait(lck);
msg = queue[head];
head = (head + 1) % capacity;
DEBUGLOG(std::cout << "get..." << std::endl);
}
//wake up push thread
notFull.notify_one();
}
void BQueue::shutdown()
{
std::lock_guard<std::mutex> lock(mtx);
for (auto it : threads) {
if (!it) {
it->shutdown();
}
}
notFull.notify_all();
notEmpty.notify_all();
}