-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoop.h
More file actions
65 lines (49 loc) · 1.74 KB
/
EventLoop.h
File metadata and controls
65 lines (49 loc) · 1.74 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
#pragma once
#include <functional>
#include <memory>
#include <atomic>
#include <sched.h>
#include <mutex>
#include <map>
#include <queue>
#include "Epoll.h"
#include "Channel.h"
#include "Connection.h"
class Connection;
class Channel;
class Epoll;
using spConnection = std::shared_ptr<Connection>;
class EventLoop {
private:
int m_timetvl; // 闹钟时间间隔
int m_timeout; // Connection对象超时时间
bool m_mainloop; // true 为主事件循环,false 为从事件循环
std::unique_ptr<Epoll> m_ep;
std::map<int, spConnection> m_conns; // Connection的fd 和 对应的Connection对象
std::queue<std::function<void()>> m_taskqueue; // 不处理业务逻辑
pid_t m_threadid;
std::atomic_bool m_stop;
std::mutex m_mutex; // 任务队列同步的互斥锁
std::mutex m_mmutex; // 保护 m_conns 的互斥锁
std::function<void(EventLoop*)> m_epolltimeoutcallback; // epoll_wait() 超时的回调函数
std::function<void(int)> m_timercallback; // 超时回调,删除超时的 Connection 对象
int m_wakeupfd;
std::unique_ptr<Channel> m_wakechannel;
int m_timerfd;
std::unique_ptr<Channel> m_timerchannel;
public:
EventLoop(bool m_mainloop, int m_timetvl=30, int m_timeout=80);
~EventLoop();
void run();
void stop();
bool isinloopthread(); // 判断当前线程是否为事件循环线程
void updatechannel(Channel* ch);
void removechannel(Channel* ch);
void setepolltimeoutcallback(std::function<void(EventLoop*)> fn);
void settimercallback(std::function<void(int)> fn);
void newconnection(spConnection conn);
void queueinloop(std::function<void()> fn);
void wakeup();
void handlewakeup();
void handletimer();
};