-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollPoller.cpp
More file actions
143 lines (106 loc) · 3.12 KB
/
PollPoller.cpp
File metadata and controls
143 lines (106 loc) · 3.12 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
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
#include "muduo/net/poller/PollPoller.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Types.h"
#include "muduo/net/Channel.h"
#include <assert>
#include <errno.h>
#include <poll.h>
using namespace muduo;
using namespace muduo::net;
PollPoller::PollPoller(EventLoop* loop):Poller(loop){}
PollPoller::~PollPoller()=default;
Timestamp PollPoller::poll(int timeoutMs, ChannelList* activeChannels)
{
int numEvents = ::poll(&*pollfds_.begin(),pollfds_.size(),timeoutMs);
int savedErrno=errno;
Timestamp now(Timestamp::now());
if(numEvents>0){
LOG_TRACE << numEvents << " events happened";
fillActiveChannels(numEvents, activeChannels);
}
else if(numEvents==0)
{
LOG_TRACE<<" nothing happened";
}
else{
if(savedErrno!=EINTR)
{
errno=savedErrno;
LOG_SYSERR<<" PollPoller::poll()";
}
}
return now;
}
void fillActiveChannels(int numEvents, ChannelList* activeChannels) const
{
for(PollFdList::const_iterator pfd=pollfds_.begin();
pfd!=pollfds_.end()&&numEvents>0;++pfd)
{
if(pfd->revents>0)
{
--numEvents;
ChannelMap::const_iterator ch=channels_.find(pfd->fd);
assert(ch!=channels_.end());
Channel* channel=ch->second();
channel->set_revents(pfd->revents);
activeChannels->pushback(channel);
}
}
}
void PollPoller::updateChannel(Channel* channel)
{
Poller::assertInLoopThread();
LOG_TRACE<<"fd="<<channel->fd()<<" events=" <<channel->events();
if(channel->index()<0)
{
assert(channels_.find(channel->fd())==channels_.end());
struct pollfd pfd;
pfd.fd=channel->fd();
pfd.events=static_cast<short>(channel->event());
pfd.revents=0;
pollfds_.push_back(pfd);
int idx=static_cast<int>(pollfds_.size())-1;
channel->set_index(idx);
channels_[pfd.fd]=channel;
}
else
{
assert(channels_.find(channel->fd())!=channels_.end());
assert(channels_[channel->fd()]==channel);
int idx = channel->index();
assert(0 <= idx && idx < static_cast<int>(pollfds_.size()));
struct pollfd& pfd = pollfds_[idx];
assert(pfd.fd==channel->fd() || pfd.fd == -channel->fd()-1);
pfd.fd=channel->fd();
pfd.events=static_cast<short>(channel->events());
pfd.revents=0;
if(channel->isNoneEvent())
pfd.fd=-channel->fd()-1;
}
}
void PollPoller::removeChannel(Channel* channel)
{
PollPoller::assertInLoopThread();
LOG_TRACE<<"fd="<<channel->fd();
assert(channels_.find(channel->fd())!=channels_.end());
assert(channels_[channel->fd()]==channel);
assert(channel->isNoneEvent());
int idx=channel->index();
assert(idx>=0&&idx<static_cast<int>(pollfds_.size()));
const struct pollfd& pfd = pollfds_[idx];
assert(pfd.fd==-channel->fd()-1 && pfd.events == channel->events());
size_t n = channels_.erase(channel->fd());
assert(n==1);
if(implicit_cast<size_t>(idx) == pollfds_.size()-1)
pollfds_.pop_back();
else{
int channelAtEnd=pollfds_.back().fd;
iter_swap(pollfds_.begin()+idx,pollfds_.end()-1);
if(channelAtEnd<0)
{
channelAtEnd=-channelAtEnd-1;
}
channels_[channelAtEnd].set_index(idx);
pollfds_.pop_back();
}
}