1// excerpts from http://code.google.com/p/muduo/
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the License file.
5//
6// Author: Shuo Chen (chenshuo at chenshuo dot com)
7
8#ifndef MUDUO_NET_EPOLLER_H
9#define MUDUO_NET_EPOLLER_H
10
11#include <map>
12#include <vector>
13
14#include "datetime/Timestamp.h"
15#include "EventLoop.h"
16
17struct epoll_event;
18
19namespace muduo
20{
21
22class Channel;
23
24///
25/// IO Multiplexing with epoll(4).
26///
27/// This class doesn't own the Channel objects.
28class EPoller : boost::noncopyable
29{
30 public:
31  typedef std::vector<Channel*> ChannelList;
32
33  EPoller(EventLoop* loop);
34  ~EPoller();
35
36  /// Polls the I/O events.
37  /// Must be called in the loop thread.
38  Timestamp poll(int timeoutMs, ChannelList* activeChannels);
39
40  /// Changes the interested I/O events.
41  /// Must be called in the loop thread.
42  void updateChannel(Channel* channel);
43  /// Remove the channel, when it destructs.
44  /// Must be called in the loop thread.
45  void removeChannel(Channel* channel);
46
47  void assertInLoopThread() { ownerLoop_->assertInLoopThread(); }
48
49 private:
50  static const int kInitEventListSize = 16;
51
52  void fillActiveChannels(int numEvents,
53                          ChannelList* activeChannels) const;
54  void update(int operation, Channel* channel);
55
56  typedef std::vector<struct epoll_event> EventList;
57  typedef std::map<int, Channel*> ChannelMap;
58
59  EventLoop* ownerLoop_;
60  int epollfd_;
61  EventList events_;
62  ChannelMap channels_;
63};
64
65}
66#endif  // MUDUO_NET_EPOLLER_H
67