1048f6023SShuo Chen // excerpts from http://code.google.com/p/muduo/
2048f6023SShuo Chen //
3048f6023SShuo Chen // Use of this source code is governed by a BSD-style license
4048f6023SShuo Chen // that can be found in the License file.
5048f6023SShuo Chen //
6048f6023SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
7048f6023SShuo Chen 
8048f6023SShuo Chen #ifndef MUDUO_NET_POLLER_H
9048f6023SShuo Chen #define MUDUO_NET_POLLER_H
10048f6023SShuo Chen 
11048f6023SShuo Chen #include <map>
12048f6023SShuo Chen #include <vector>
13048f6023SShuo Chen 
14048f6023SShuo Chen #include "datetime/Timestamp.h"
15048f6023SShuo Chen #include "EventLoop.h"
16048f6023SShuo Chen 
17048f6023SShuo Chen struct pollfd;
18048f6023SShuo Chen 
19048f6023SShuo Chen namespace muduo
20048f6023SShuo Chen {
21048f6023SShuo Chen 
22048f6023SShuo Chen class Channel;
23048f6023SShuo Chen 
24048f6023SShuo Chen ///
25048f6023SShuo Chen /// IO Multiplexing with poll(2).
26048f6023SShuo Chen ///
27048f6023SShuo Chen /// This class doesn't own the Channel objects.
28048f6023SShuo Chen class Poller : boost::noncopyable
29048f6023SShuo Chen {
30048f6023SShuo Chen  public:
31048f6023SShuo Chen   typedef std::vector<Channel*> ChannelList;
32048f6023SShuo Chen 
33048f6023SShuo Chen   Poller(EventLoop* loop);
34048f6023SShuo Chen   ~Poller();
35048f6023SShuo Chen 
36048f6023SShuo Chen   /// Polls the I/O events.
37048f6023SShuo Chen   /// Must be called in the loop thread.
38048f6023SShuo Chen   Timestamp poll(int timeoutMs, ChannelList* activeChannels);
39048f6023SShuo Chen 
40048f6023SShuo Chen   /// Changes the interested I/O events.
41048f6023SShuo Chen   /// Must be called in the loop thread.
42048f6023SShuo Chen   void updateChannel(Channel* channel);
43048f6023SShuo Chen+  /// Remove the channel, when it destructs.
44048f6023SShuo Chen+  /// Must be called in the loop thread.
45048f6023SShuo Chen+  void removeChannel(Channel* channel);
46048f6023SShuo Chen 
47048f6023SShuo Chen   void assertInLoopThread() { ownerLoop_->assertInLoopThread(); }
48048f6023SShuo Chen 
49048f6023SShuo Chen  private:
50048f6023SShuo Chen   void fillActiveChannels(int numEvents,
51048f6023SShuo Chen                           ChannelList* activeChannels) const;
52048f6023SShuo Chen 
53048f6023SShuo Chen   typedef std::vector<struct pollfd> PollFdList;
54048f6023SShuo Chen   typedef std::map<int, Channel*> ChannelMap;
55048f6023SShuo Chen 
56048f6023SShuo Chen   EventLoop* ownerLoop_;
57048f6023SShuo Chen   PollFdList pollfds_;
58048f6023SShuo Chen   ChannelMap channels_;
59048f6023SShuo Chen };
60048f6023SShuo Chen 
61048f6023SShuo Chen }
62048f6023SShuo Chen #endif  // MUDUO_NET_POLLER_H
63