-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoop.cpp
More file actions
199 lines (173 loc) · 5.76 KB
/
Copy pathEventLoop.cpp
File metadata and controls
199 lines (173 loc) · 5.76 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
#include <muduo/EventLoop.h>
#include <muduo/poller/PollPoller.h>
#include <muduo/Callbacks.h>
#include <muduo/TimerQueue.h>
#include <muduo/Channel.h>
#include <muduo/Bridge.h>
#include <chrono>
#include <cassert>
#include <sys/poll.h>
namespace {
thread_local muduo::EventLoop* tl_loop_inThisThread = {nullptr};
}
namespace muduo {
using namespace std::chrono;
const EventLoop::TimeoutDuration_t EventLoop::kPollTimeout = duration_cast<EventLoop::TimeoutDuration_t>(seconds(5));
/**
* @code
* +--------------------------------------+
* | MemoryPool of EventLoop |
* | |
* | +----------------------------------+ |
* | | components... | |
* | +----------------------------------+ |
* | | TcpConnection instance | |
* | +----------------------------------+ |
* | | TcpConnection instance |
* | +----------------------------------+ |
* | | ... | |
* | +----------------------------------+ |
* | |
* +--------------------------------------+
* @endcode
*/
EventLoop::EventLoop()
#ifdef MUDUO_USE_MEMPOOL
: memPool_(std::make_unique<base::MemoryPool>(this))
, threadId_(::pthread_self())
#else
: threadId_(::pthread_self())
#endif
, looping_(false)
, quit_(false)
, eventHandling_(false)
#ifdef MUDUO_USE_MEMPOOL
, poller_(new (memPool_.get()) PollPoller(this))
, timerQueue_(new (memPool_.get()) TimerQueue(this))
, activeChannels_(base::allocator<Channel*>(GetMemoryPool()))
, bridge_(new (memPool_.get()) Bridge(this))
#else
, poller_(std::make_unique<PollPoller>(this))
, timerQueue_(std::make_unique<TimerQueue>(this))
, activeChannels_()
, bridge_(std::make_unique<Bridge>(this))
#endif
, pendingCbsQueue_()
, callingPendingCbs_(false)
{
LOG_DEBUG << "EventLoop is created in thread " << threadId_;
if (tl_loop_inThisThread != nullptr) {
LOG_FATAL << "Another EventLoop instance " << tl_loop_inThisThread
<< " exists in this thread";
} else {
tl_loop_inThisThread = this;
}
}
EventLoop::~EventLoop() {
#ifdef MUDUO_USE_MEMPOOL
// All components of EventLoop were allocated in the loop-level-mempool held by the EventLoop instance,
// so must deallocate in the Loop-thread for thread-safe.
AssertInLoopThread();
#endif
LOG_DEBUG << "EventLoop " << this << " of thread " << threadId_
<< " destructs in thread " << ::pthread_self();
tl_loop_inThisThread = nullptr;
}
EventLoop* EventLoop::GetCurrentThreadLoop() {
return tl_loop_inThisThread;
}
void EventLoop::PrintActiveChannels() const {
for (const Channel* channel : activeChannels_) {
LOG_TRACE << "<" << channel->REventsToString() << "> ";
}
}
void EventLoop::Loop() {
AssertInLoopThread(); // must loop in itself thread
assert(!looping_);
assert(!eventHandling_);
assert(!callingPendingCbs_);
looping_ = true;
quit_ = false;
LOG_TRACE << "EventLoop " << this << " start to loop";
while (!quit_) {
activeChannels_.clear(); // Clear the list for polling new channels
receiveTimePoint_ = poller_->Poll(kPollTimeout, &activeChannels_);
if (muduo::GetLoglevel() <= Logger::LogLevel::TRACE) {
PrintActiveChannels();
}
HandleActiveChannels();
HandlePendingCallbacks();
}
looping_ = false;
}
void EventLoop::HandleActiveChannels() {
assert(!eventHandling_);
eventHandling_ = true;
for (auto c : activeChannels_) {
c->HandleEvents(receiveTimePoint_);
}
eventHandling_ = false;
}
void EventLoop::UpdateChannel(Channel* c) {
poller_->UpdateChannel(c);
}
void EventLoop::RemoveChannel(Channel* c) {
poller_->RemoveChannel(c);
}
TimerId_t EventLoop::RunAt(const TimePoint_t& when, const TimeoutCb_t& cb) {
return timerQueue_->AddTimer(when, TimeoutDuration_t::zero(), cb);
}
TimerId_t EventLoop::RunAfter(const Interval_t& delay, const TimeoutCb_t& cb) {
using namespace std;
auto timepoint = chrono::steady_clock::now() + delay;
return RunAt(timepoint, cb);
}
TimerId_t EventLoop::RunEvery(const Interval_t& interval, const TimeoutCb_t& cb) {
using namespace std;
auto timepoint = chrono::steady_clock::now() + interval;
return timerQueue_->AddTimer(timepoint, interval, cb);
}
void EventLoop::cancelTimer(const TimerId_t timerId) {
timerQueue_->CancelTimer(timerId);
}
void EventLoop::EnqueueEventLoop(const PendingEventCb_t& cb) {
{
std::lock_guard<std::mutex> guard(mtx_);
pendingCbsQueue_.push_back(cb);
}
/**
* 1. 如果不在IO线程中,因为IO线程此时可能阻塞在poll中,为确保任务即使被处理,故要调用WakeUp
* 2. 如果在IO线程中并且此时线程正在处理pending Callbacks,由Loop内部实现决定此时还需调用WakeUp,防止IO线程阻塞
* 3. 如果在IO线程中且此时线程正在处理activeChannels的callback,则无需调用WakeUp
*/
if (!IsInLoopThread() || callingPendingCbs_) {
bridge_->WakeUp();
}
}
void EventLoop::RunInEventLoop(const PendingEventCb_t& cb) {
if (IsInLoopThread()) {
cb();
} else {
EnqueueEventLoop(cb);
}
}
void EventLoop::HandlePendingCallbacks() {
PendingCallbacksQueue tmp_queue;
callingPendingCbs_.store(true);
{ // 缩短临界区大小
std::lock_guard<std::mutex> guard(mtx_);
tmp_queue.swap(pendingCbsQueue_); // Can effectively prevent deadlock
}
for (const auto& cb : tmp_queue) {
cb.operator()();
}
callingPendingCbs_.store(false);
}
void EventLoop::Quit() {
assert(quit_ == false);
quit_.store(true);
if (!IsInLoopThread()) {
bridge_->WakeUp();
}
}
}