forked from voidccc/mini-muduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventLoop.cc
150 lines (129 loc) · 2.96 KB
/
EventLoop.cc
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
//author voidccc
#include <sys/eventfd.h>
#include "EventLoop.h"
#include "Channel.h"
#include "Epoll.h"
#include "TimerQueue.h"
#include "Timestamp.h"
#include "Task.h"
#include "CurrentThread.h"
#include <iostream>
using namespace std;
EventLoop::EventLoop()
:_quit(false)
,_callingPendingFunctors(false)
,_pPoller(new Epoll()) // Memory Leak !!!
,_threadId(CurrentThread::tid())
,_pTimerQueue(new TimerQueue(this)) // Memory Leak!!!
{
_eventfd = createEventfd();
_pEventfdChannel = new Channel(this, _eventfd); // Memory Leak !!!
_pEventfdChannel->setCallback(this);
_pEventfdChannel->enableReading();
}
EventLoop::~EventLoop()
{}
void EventLoop::loop()
{
while(!_quit)
{
vector<Channel*> channels;
_pPoller->poll(&channels);
vector<Channel*>::iterator it;
for(it = channels.begin(); it != channels.end(); ++it)
{
(*it)->handleEvent();
}
doPendingFunctors();
}
}
void EventLoop::update(Channel* pChannel)
{
_pPoller->update(pChannel);
}
void EventLoop::queueInLoop(Task& task)
{
{
MutexLockGuard guard(_mutex);
_pendingFunctors.push_back(task);
}
if(!isInLoopThread() || _callingPendingFunctors)
{
wakeup();
}
}
void EventLoop::runInLoop(Task& task)
{
if(isInLoopThread())
{
task.doTask();
}
else
{
queueInLoop(task);
}
}
void EventLoop::wakeup()
{
uint64_t one = 1;
ssize_t n = ::write(_eventfd, &one, sizeof one);
if (n != sizeof one)
{
cout << "EventLoop::wakeup() writes " << n << " bytes instead of 8" << endl;
}
}
int EventLoop::createEventfd()
{
int evtfd = ::eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (evtfd < 0)
{
cout << "Failed in eventfd" << endl;
}
return evtfd;
}
void EventLoop::handleRead()
{
uint64_t one = 1;
ssize_t n = ::read(_eventfd, &one, sizeof one);
if (n != sizeof one)
{
cout << "EventEventLoop::handleRead() reads " << n << " bytes instead of 8" << endl;
}
}
void EventLoop::handleWrite()
{}
void EventLoop::doPendingFunctors()
{
vector<Task> tempRuns;
_callingPendingFunctors = true;
{
MutexLockGuard guard(_mutex);
tempRuns.swap(_pendingFunctors);
}
vector<Task>::iterator it;
for(it = tempRuns.begin(); it != tempRuns.end(); ++it)
{
it->doTask();
}
_callingPendingFunctors = false;
}
int EventLoop::runAt(Timestamp when, IRun0* pRun)
{
return _pTimerQueue->addTimer(pRun, when, 0.0);
}
int EventLoop::runAfter(double delay, IRun0* pRun)
{
return _pTimerQueue->addTimer(pRun, Timestamp::nowAfter(delay), 0.0);
}
int EventLoop::runEvery(double interval, IRun0* pRun)
{
return _pTimerQueue->addTimer(pRun, Timestamp::nowAfter(interval), interval);
}
void EventLoop::cancelTimer(int timerId)
{
_pTimerQueue->cancelTimer(timerId);
}
bool EventLoop::isInLoopThread()
{
return _threadId == CurrentThread::tid();
}