-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventLoop.h
77 lines (60 loc) · 1.82 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef MUDUO_NET_EVENTLOOP_H
#define MUDUO_NET_EVENTLOOP_H
#include "Callbacks.h"
#include "TimerId.h"
#include "TimerQueue.h"
#include "datetime/Timestamp.h"
#include "thread/Condition.h"
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <vector>
namespace muduo {
class Poller;
class Channel;
class EventLoop : boost::noncopyable {
public:
typedef boost::function<void()> Functor;
EventLoop();
~EventLoop();
void loop();
void assertInLoopThread();
bool isInLoopThread();
void quit();
void updateChannel(Channel *channel);
void removeChannel(Channel *channel);
void runInLoop(const Functor &cb);
void queueInLoop(const Functor &cb);
void wakeup();
TimerId runAt(const Timestamp &time, const TimerCallback &cb) {
return timerQueue_->addTimer(cb, time, 0.0);
}
TimerId runAfter(double delay, const TimerCallback &cb) {
Timestamp time(addTime(Timestamp::now(), delay));
return runAt(time, cb);
}
TimerId runEvery(double interval, const TimerCallback &cb) {
Timestamp time(addTime(Timestamp::now(), interval));
return timerQueue_->addTimer(cb, time, interval);
}
Timestamp pollReturnTime() const { return pollReturnTime_; }
void cancel(TimerId timerId);
private:
void abortNotInLoopThread();
void handleRead();
void doPendingFunctors();
typedef std::vector<Channel *> ChannelList;
bool looping_;
bool quit_;
bool callingPendingFunctors_;
const pid_t threadId_;
Timestamp pollReturnTime_;
boost::scoped_ptr<Poller> poller_;
boost::scoped_ptr<TimerQueue> timerQueue_;
int wakeupFd_;
boost::scoped_ptr<Channel> wakeupChannel_;
std::vector<Functor> pendingFunctors_;
ChannelList activeChannels;
MutexLock mutex_;
};
} // namespace muduo
#endif