-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThreadPool.cpp
More file actions
59 lines (50 loc) · 1.58 KB
/
Copy pathEventLoopThreadPool.cpp
File metadata and controls
59 lines (50 loc) · 1.58 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
#include <muduo/EventLoopThreadPool.h>
#include <muduo/EventLoopThread.h>
#include <muduo/EventLoop.h>
#include <cassert>
using namespace muduo;
EventLoopThreadPool::EventLoopThreadPool(EventLoop* base_loop, const std::string& name)
: baseLoop_(base_loop)
, name_(name)
#ifdef MUDUO_USE_MEMPOOL
, threadPool_(baseLoop_->GetMemoryPool())
, loops_(baseLoop_->GetMemoryPool())
#else
, threadPool_()
, loops_()
#endif
{ assert(baseLoop_ != nullptr); }
EventLoopThreadPool::~EventLoopThreadPool() noexcept = default;
void EventLoopThreadPool::BuildAndRun() {
assert(!started_);
baseLoop_->AssertInLoopThread();
for (size_t i = 0; i < poolSize_; i++) {
std::string cur_trd_name = name_+":"+std::to_string(i);
#ifdef MUDUO_USE_MEMPOOL
threadPool_.emplace_back(new (baseLoop_->GetMemoryPool()) EventLoopThread(initCb_, cur_trd_name));
#else
threadPool_.emplace_back(std::make_unique<EventLoopThread>(initCb_, cur_trd_name));
#endif
loops_.push_back(threadPool_[i]->Run());
}
assert(loops_.size() == poolSize_);
started_ = true;
if (poolSize_ == 0 && initCb_) {
initCb_(baseLoop_);
}
}
EventLoop* EventLoopThreadPool::GetNextLoop() const {
baseLoop_->AssertInLoopThread();
assert(started_);
EventLoop* result = baseLoop_;
if (!loops_.empty()) {
// round-robin
assert(nextLoopIdx_ < poolSize_);
result = loops_[nextLoopIdx_];
nextLoopIdx_ += 1;
if (nextLoopIdx_ >= loops_.size()) {
nextLoopIdx_ = 0;
}
}
return result;
}