-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathKEventLoop.cpp
More file actions
166 lines (150 loc) · 4.31 KB
/
Copy pathKEventLoop.cpp
File metadata and controls
166 lines (150 loc) · 4.31 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
#include "KEventLoop.h"
#include "../poller/KChannel.h"
#include "../poller/KEventManager.h"
#include "KAsyncWaker.h"
#include <signal.h>
#include <sys/eventfd.h>
using namespace kback;
// 通过 __thread 来保证一个线程只能创建一个 EventLoop
// __thread 变量是每个线程有的一份独立的实体
// 类似于static 只在编译期初始化
__thread EventLoop *t_loopInThisThread = 0;
const int kPollTimeMs = 1000;
EventLoop::EventLoop()
: looping_(false), threadId_(std::this_thread::get_id()),
eventmanager_(new EventManager(this)), quit_(false),
callingPendingFunctors_(false), asyncwaker(new AsyncWaker(this)) {
#ifdef USE_STD_COUT
std::cout << "LOG_TRACE: "
<< "EventLoop created " << this << " in thread " << threadId_
<< std::endl;
#endif
if (t_loopInThisThread) {
#ifdef USE_STD_COUT
std::cout << "LOG_FATAL: "
<< "Another EventLoop " << t_loopInThisThread
<< " exists in this thread " << threadId_ << std::endl;
#endif
} else {
t_loopInThisThread = this;
}
}
EventLoop::~EventLoop() {
assert(!looping_);
t_loopInThisThread = nullptr;
}
void EventLoop::loop() {
assert(!looping_);
assertInLoopThread();
looping_ = true;
quit_ = false;
while (!quit_) {
activeChannels_.clear();
// 这里poll在轮询中,为阻塞的,想要回调活动事件,必须想办法唤醒
// muduo这里采用了一个很巧妙的办法,专门用一个文件描述符来进行唤醒
pollReturnTime_ = eventmanager_->poll(kPollTimeMs, &activeChannels_);
for (auto it = activeChannels_.begin(); it != activeChannels_.end(); ++it) {
(*it)->handleEvent(pollReturnTime_);
}
doPendingFunctors();
}
#ifdef USE_STD_COUT
std::cout << "LOG_TRACE: "
<< "EventLoop " << this << " stop looping" << std::endl;
#endif
looping_ = false;
}
void EventLoop::quit() {
quit_ = true;
if (!isInLoopThread()) {
asyncwaker->wakeup();
}
}
// channel 中注册了事件,需要更新,但是
// 为了保证线程安全,channel自己是不会更新的,
// 将更新的工作转交给loop, 然后在loop线程中 同步poll 中的channel
void EventLoop::updateChannel(Channel *channel) {
assert(channel->ownerLoop() == this);
assertInLoopThread();
// EventLoop 实际上不负责更新channel
eventmanager_->updateChannel(channel);
}
void EventLoop::removeChannel(Channel *channel) {
assert(channel->ownerLoop() == this);
assertInLoopThread();
eventmanager_->removeChannel(channel);
}
void EventLoop::abortNotInLoopThread() {
#ifdef USE_STD_COUT
std::cerr << "LOG_FATAL: "
<< "EventLoop::abortNotInLoopThread - EventLoop " << this
<< " was created in threadId_ = " << threadId_
<< ", current thread id = " << std::this_thread::get_id()
<< std::endl;
#endif
std::abort();
}
// 执行装载的的回调函数,下面的处理方法很巧妙
void EventLoop::doPendingFunctors() {
#ifdef USE_LOCKFREEQUEUE
callingPendingFunctors_ = true;
// 遍历执行回调函数
for (;;) {
Functor functor;
bool flag = pendingFunctors_.Try_Dequeue(functor);
if (flag) {
if (functor != nullptr) {
functor();
}
} else {
break;
}
}
#else
{
std::vector<Functor> functors;
callingPendingFunctors_ = true;
#ifdef USE_SPINLOCK
spinlock.lock();
functors.swap(pendingFunctors_);
spinlock.unlock();
#else
std::lock_guard<std::mutex> lock(mutex_);
functors.swap(pendingFunctors_);
#endif
// 遍历执行回调函数
for (auto &functor : functors) {
functor();
}
}
#endif
callingPendingFunctors_ = false;
}
// !这个函数是在loop启动之前调用的,用于设置channel
void EventLoop::runInLoop(const Functor &cb) {
if (isInLoopThread()) {
cb();
} else {
queueInLoop(cb);
}
}
// queueInLoop 是public的,不一定要被runInLoop调用,也可以被直接调用
void EventLoop::queueInLoop(const Functor &cb) {
{
#ifdef USE_LOCKFREEQUEUE
pendingFunctors_.Enqueue(cb);
#else
#ifdef USE_SPINLOCK
spinlock.lock();
pendingFunctors_.push_back(cb);
spinlock.unlock();
#else
std::lock_guard<std::mutex> lock(mutex_);
pendingFunctors_.push_back(cb);
#endif
#endif
}
if (!isInLoopThread() || callingPendingFunctors_) {
asyncwaker->wakeup();
}
}