-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventLoop.h
54 lines (48 loc) · 1.42 KB
/
EventLoop.h
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
#pragma once
#include "Channel.h"
#include "Epoll.h"
#include "Util.h"
#include "base/CurrentThread.h"
#include "base/MutexLock.h"
#include <assert.h>
#include <memory>
#include <iostream>
class Channel;
class EventLoop {
public:
typedef std::function<void()> Functor;
EventLoop();
~EventLoop();
void loop();
void quit();
void runInLoop(Functor&& cb);
void queueInLoop(Functor&& cb);
bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }
void assertInLoopThread() { assert(isInLoopThread()); }
void shutdown(std::shared_ptr<Channel> channel) { shutDownWR(channel->getFd()); }
void removeFromPoller(std::shared_ptr<Channel> channel) {
poller_->epoll_del(channel);
}
void updatePoller(std::shared_ptr<Channel> channel, int timeout = 0) {
poller_->epoll_mod(channel, timeout);
}
void addToPoller(std::shared_ptr<Channel> channel, int timeout = 0) {
poller_->epoll_add(channel, timeout);
}
private:
// 声明顺序 wakeupFd_ -> pwakeupChannel_
bool looping_;
std::shared_ptr<Epoll> poller_;
int wakeupFd_;
bool quit_;
bool eventHandling_;
mutable MutexLock mutex_;
std::vector<Functor> pendingFunctors_;
bool callingPendingFunctors_;
const pid_t threadId_;
std::shared_ptr<Channel> pwakeupChannel_;
void wakeup();
void handleRead();
void doPendingFunctors();
void handleConn();
};