Repository navigation
Expand file tree
/
Copy pathEventLoop.cpp
More file actions
197 lines (180 loc) · 6.13 KB
/
Copy pathEventLoop.cpp
File metadata and controls
197 lines (180 loc) · 6.13 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
#include "EventLoop.h"
#include "Channel.h"
#include "Epoll.h"
#include <bits/types/struct_itimerspec.h>
#include <bits/types/struct_timeval.h>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <mutex>
#include <sys/eventfd.h>
#include <sys/syscall.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <vector>
int createtimerfd(int sec = 30)
{
int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); // 创建timerfd
struct itimerspec timeout; // 定义时间的数据结构
memset(&timeout, 0, sizeof(struct itimerspec));
timeout.it_value.tv_sec = sec; // 定时时间 固定为5
timeout.it_value.tv_nsec = 0;
timerfd_settime(tfd, 0, &timeout, 0);
return tfd;
}
// 在构造函数中创建Epoll对象ep_
EventLoop::EventLoop(bool mainloop, int timetvl, int timeout) : timetvl_(timetvl), timeout_(timeout), ep_(new Epoll),
threadid_(0), wakeupfd_(eventfd(0, EFD_NONBLOCK)),
wakechannel_(new Channel(this, wakeupfd_)), timerfd_(createtimerfd(timeout_)),
timerchannel_(new Channel(this, timerfd_)), mainloop_(mainloop),
stop_(false)
{
if (wakeupfd_ < 0)
{
perror("eventfd() failed");
exit(-1);
}
if (timerfd_ < 0)
{
perror("timerfd_create() failed");
exit(-1);
}
wakechannel_->setreadcallback(std::bind(&EventLoop::handlewakeup, this));
wakechannel_->enablereading();
timerchannel_->setreadcallback(std::bind(&EventLoop::handletimer, this));
timerchannel_->enablereading();
}
EventLoop::~EventLoop()
{
close(wakeupfd_); // Channel不负责关闭fd 由EventLoop关闭
close(timerfd_);
}
void EventLoop::run() // 运行事件循环
{
threadid_ = syscall(SYS_gettid); // 获取事件循环所在线程的id
while (stop_ == false) // 事件循环
{
std::vector<Channel *> channels = ep_->loop(10 * 1000); // 等待监视的fd有事件发生
// 如果channels为空 表示超时 回调TcpServer::epolltimeout()
if (channels.size() == 0)
{
if (epolltimeoutcallback_)
{
epolltimeoutcallback_(this);
}
}
else
{
for (auto &ch : channels)
{
ch->handleevent(); // 处理epoll_wait()返回的事件
}
}
}
}
void EventLoop::stop() // 停止事件循环
{
stop_ = true;
wakeup(); // 唤醒事件循环 如果没有这行代码 事件循环将在下次闹钟响时或者epoll_wait()超时时才会停下来
}
void EventLoop::updatechannel(Channel *ch) // 把channel添加到红黑树上 channel中有fd 也有需要监视的事件
{
ep_->updatechannel(ch);
}
void EventLoop::removechannel(Channel *ch) // 从红黑树上删除channel
{
ep_->removechannel(ch);
}
void EventLoop::setepolltimeoutcallback(std::function<void(EventLoop *)> fn) // 设置epoll_wait()超时的回调函数
{
epolltimeoutcallback_ = fn;
}
bool EventLoop::isinloopthread() // 判断当前线程是否为事件循环线程
{
return threadid_ == syscall(SYS_gettid);
}
void EventLoop::queueinloop(std::function<void()> fn) // 把任务添加到队列中
{
{
std::lock_guard<std::mutex> gd(mutex_); // 给任务队列加锁
taskqueue_.push(fn); // 任务入队
}
wakeup(); // 唤醒事件循环
}
void EventLoop::wakeup() // 用eventfd唤醒事件循环线程
{
uint64_t val = 1;
ssize_t ret = write(wakeupfd_, &val, sizeof(val));
(void)ret;
}
void EventLoop::handlewakeup() // 事件循环线程被eventfd唤醒后执行的函数
{
uint64_t val;
ssize_t ret = read(wakeupfd_, &val, sizeof(val)); // 从eventfd中读取数据 如果不读取 eventfd的读事件会一直触发
(void)ret;
std::queue<std::function<void()>> tasks;
{
std::lock_guard<std::mutex> gd(mutex_); // 只把任务换出来 不在持锁状态下执行
tasks.swap(taskqueue_);
}
// 执行队列中全部的发送任务
while (tasks.empty() == false)
{
std::function<void()> fn = std::move(tasks.front()); // 出队一个元素
tasks.pop();
fn(); // 执行任务 任务中再调用queueinloop()也不会死锁
}
}
void EventLoop::handletimer() // 闹钟响时事件的函数
{
// 重新计时
struct itimerspec timeout; // 定时时间的数据结构
memset(&timeout, 0, sizeof(struct itimerspec));
timeout.it_value.tv_sec = timetvl_; // 定时时间
timeout.it_value.tv_nsec = 0;
timerfd_settime(timerfd_, 0, &timeout, 0);
if (mainloop_)
{
printf("主事件循环的闹钟到了");
}
else
{
printf("从事件循环的闹钟到了");
time_t now = time(0); // 获取当前时间
// 先在锁内收集超时连接的fd 再在锁外回调 避免跨线程锁顺序不一致导致死锁
std::vector<int> timeoutfds;
{
std::lock_guard<std::mutex> gd(mmutex_);
for (auto it = conns_.begin(); it != conns_.end();)
{
if (it->second->timeout(now, timeout_))
{
timeoutfds.push_back(it->first);
it = conns_.erase(it); // erase返回下一个迭代器 不能在range-for中erase
}
else
{
++it;
}
}
}
for (int fd : timeoutfds)
{
if (timercallback_)
{
timercallback_(fd); // 从TcpServer的map中删除超时的conn
}
}
}
}
void EventLoop::newconnection(spConnection conn) // 把Connection对象保存在conns_中
{
std::lock_guard<std::mutex> gd(mmutex_);
conns_[conn->fd()] = conn;
}
void EventLoop::settimercallback(std::function<void(int)> fn) // 将被设置为TcpServer::removeconn()
{
timercallback_ = fn;
}