-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventLoop.cpp
207 lines (186 loc) · 4.32 KB
/
EventLoop.cpp
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
198
199
200
201
202
203
204
205
206
207
#include"EventLoop.h"
#include"poll.h"
#include"TimeQueue.h"
#include"PollPoller.h"
#include"base/Logging.h"
#include<algorithm>
#include<sys/eventfd.h>
#include<unistd.h>
#include<signal.h>
namespace
{
__thread EventLoop* t_loopInthisThread=nullptr;
const int kPollTimeMs=10000;
//create eventfd
int createEventfd()
{
int fd=::eventfd(0,EFD_NONBLOCK|EFD_CLOEXEC);
if(fd<0)
{
Log<<"error in creating eventfd\n";
abort();
}
return fd;
}
//ignore signal
} // namespace
EventLoop::EventLoop()
: looping_(false),
quit_(false),
eventHandling_(false),
callingPendingFunctors_(false),
iteration_(0),
threadId_(CurrentThread::tid()),
pollReturnTime(0),
poller_(Poller::newEpoller(this)),
timequeue_(new TimeQueue(this)),
WakeupFd_(::createEventfd()),
wakeupChannel(new Channel(this,WakeupFd_)),
activechannels_(),
currentactivechannel_(nullptr),
pendingFunctors_(),
mutex_()
{
assert(t_loopInthisThread==nullptr);
t_loopInthisThread=this;
wakeupChannel->setReadCallback(std::bind(&EventLoop::handleRead,this));
wakeupChannel->enableReading();
}
EventLoop::~EventLoop()
{
t_loopInthisThread=nullptr;
wakeupChannel->disableAll();
wakeupChannel->remove();
}
void EventLoop::loop()
{
assertInLoopThread();
assert(!looping_);
looping_=true;
quit_=false;
while (!quit_)
{
++iteration_;
activechannels_.clear();
pollReturnTime=poller_->poll(kPollTimeMs,&activechannels_);
eventHandling_=true;
for (ChannelList::const_iterator it = activechannels_.begin(); it != activechannels_.end(); it++)
{
currentactivechannel_=(*it);
(*it)->handleEvent();
currentactivechannel_=nullptr;
}
eventHandling_=false;
doPendingFunctors();
}
looping_=false;
}
void EventLoop::quit()
{
quit_=true;
if(!isInLoopThread())
{
wakeup();
}
}
void EventLoop::runInLoop(Functor cb)
{
if(isInLoopThread())
{
cb();
}
else
{
queueInLoop(std::move(cb));
}
}
void EventLoop::queueInLoop(Functor cb)
{
{
MutexLockGuard lock(mutex_);
pendingFunctors_.push_back(std::move(cb));
}
//wake up if not in IO thread or callingpending
if(!isInLoopThread()||callingPendingFunctors_)
{
wakeup();
}
}
size_t EventLoop::queueSize() const
{
MutexLockGuard lock(mutex_);
return pendingFunctors_.size();
}
void EventLoop::wakeup()
{
uint64_t one=1;
ssize_t n=::write(WakeupFd_,&one,sizeof one);
if(n!=sizeof one)
{
Log<<"error in wake up \n";
}
}
void EventLoop::handleRead()
{
uint64_t one;
ssize_t n=::read(WakeupFd_,&one,sizeof one);
if(n!=sizeof one)
{
Log<<"error in read wakeupfd_\n";
}
}
void EventLoop::updateChannel(Channel *channel)
{
assert(channel->ownerLoop()==this);
assertInLoopThread();
poller_->updateChannel(channel);
}
void EventLoop::removeChannel(Channel *channel)
{
assert(channel->ownerLoop()==this);
assertInLoopThread();
poller_->removeChannel(channel);
}
bool EventLoop::hasChannel(Channel *channel)
{
assert(channel->ownerLoop()==this);
assertInLoopThread();
return poller_->hasChannel(channel);
}
EventLoop* EventLoop::getEventLoopOfCurrentThread()
{
return t_loopInthisThread;
}
void EventLoop::abortNotInLoopThread()
{
Log<<"EventLoop has created in thread "<<CurrentThread::tid()<<"\n";
abort();
}
void EventLoop::doPendingFunctors()
{
callingPendingFunctors_=true;
std::vector<Functor> Functors;
{
MutexLockGuard lock(mutex_);
pendingFunctors_.swap(Functors);
}
for (auto &func : Functors)
{
func();
}
callingPendingFunctors_=false;
}
TimerId EventLoop::runAt(Timestamp time, TimerCallback cb)
{
return timequeue_->addTimer(std::move(cb),time,0);
}
TimerId EventLoop::runAfter(double delay,TimerCallback cb)
{
Timestamp when=Timer::addTime(now(),delay);
return timequeue_->addTimer(std::move(cb),when,0);
}
TimerId EventLoop::runEvery(double interval,TimerCallback cb)
{
Timestamp when=Timer::addTime(now(),interval);
return timequeue_->addTimer(std::move(cb),when,interval);
}