Repository navigation
Expand file tree
/
Copy pathEventLoop.h
More file actions
61 lines (47 loc) · 1.39 KB
/
Copy pathEventLoop.h
File metadata and controls
61 lines (47 loc) · 1.39 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
#pragma once
#include <cstdint>
#include <sys/epoll.h>
#include <vector>
#include <map>
#include <memory>
#include <functional>
#include <mutex>
#include <thread>
#include <set>
#include "Timer.h"
class Channel;
class EventLoop {
public:
using Functor = std::function<void()>;
EventLoop();
~EventLoop();
EventLoop(const EventLoop&) = delete;
EventLoop& operator=(const EventLoop&) = delete;
void loop();
bool isInLoopThread() const;
void runInLoop(Functor cb);
void queueInLoop(Functor cb);
void updateChannel(Channel *channel);
void removeChannel(Channel *channel);
void holdChannel(std::shared_ptr<Channel> channel);
void addTimer(int id, int64_t expireTime, Timer::TimerCallback cb);
void removeTimer(int id);
void handleExpiredTimers();
static int64_t nowMs();
private:
void doPendingFunctors();
void wakeup();
void removeTimerInLock(int id);
int epollFd_;
std::vector<struct epoll_event> events_;
std::map<int, bool> fdInEpoll_;
std::map<int, std::shared_ptr<Channel>> activeChannels_;
std::thread::id threadId_;
std::vector<Functor> pendingFunctors_;
std::mutex mutex_;
int wakeupFd_;
std::multiset<std::pair<int64_t, int>> timers_;
std::map<int, std::shared_ptr<Timer>> timerMap_;
std::mutex timerMutex_; // 保护 timers_ 和 timerMap_
static const int kInitEventsSize = 128;
};