-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecvq.cpp
80 lines (62 loc) · 1.44 KB
/
recvq.cpp
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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
#include "bot.h"
using namespace irc::bot;
decltype(recvq::mutex) recvq::mutex;
decltype(recvq::interrupted) recvq::interrupted;
decltype(recvq::ios) recvq::ios;
decltype(recvq::thread) recvq::thread;
static const scope join([]
{
recvq::interrupt();
for(auto &thread : recvq::thread)
{
thread->join();
delete thread;
}
});
void recvq::interrupt()
{
interrupted.store(true,std::memory_order_release);
ios.stop();
}
void recvq::reset()
{
const std::lock_guard<decltype(mutex)> lock(mutex);
if(thread.size() <= 1)
ios.reset();
}
size_t recvq::num_threads()
{
const std::lock_guard<decltype(mutex)> lock(mutex);
return thread.size();
}
void recvq::add_thread(const size_t &num)
{
const std::lock_guard<decltype(mutex)> lock(mutex);
for(size_t i(0); i < num; ++i)
thread.emplace_back(new std::thread(&recvq::worker));
}
void recvq::min_threads(const size_t &num)
{
const std::lock_guard<decltype(mutex)> lock(mutex);
for(size_t i(thread.size()); i < num; ++i)
thread.emplace_back(new std::thread(&recvq::worker));
}
void recvq::worker()
{
const boost::asio::io_service::work work(ios);
while(!interrupted.load(std::memory_order_consume)) try
{
const scope r(std::bind(&recvq::reset));
ios.run();
}
catch(const Interrupted &e)
{
continue;
}
}