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_POLLER_H
9#define MUDUO_NET_POLLER_H
10
11#include <map>
12#include <vector>
13
14#include "datetime/Timestamp.h"
15#include "EventLoop.h"
16
17struct pollfd;
18
19namespace muduo
20{
21
22class Channel;
23
24///
25/// IO Multiplexing with poll(2).
26///
27/// This class doesn't own the Channel objects.
28class Poller : boost::noncopyable
29{
30 public:
31  typedef std::vector<Channel*> ChannelList;
32
33  Poller(EventLoop* loop);
34  ~Poller();
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
44  void assertInLoopThread() { ownerLoop_->assertInLoopThread(); }
45
46 private:
47  void fillActiveChannels(int numEvents,
48                          ChannelList* activeChannels) const;
49
50  typedef std::vector<struct pollfd> PollFdList;
51  typedef std::map<int, Channel*> ChannelMap;
52
53  EventLoop* ownerLoop_;
54  PollFdList pollfds_;
55  ChannelMap channels_;
56};
57
58}
59#endif  // MUDUO_NET_POLLER_H
60