19a1e991dSShuo Chen// excerpts from http://code.google.com/p/muduo/
29a1e991dSShuo Chen//
39a1e991dSShuo Chen// Use of this source code is governed by a BSD-style license
49a1e991dSShuo Chen// that can be found in the License file.
59a1e991dSShuo Chen//
69a1e991dSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
79a1e991dSShuo Chen
89a1e991dSShuo Chen#ifndef MUDUO_NET_POLLER_H
99a1e991dSShuo Chen#define MUDUO_NET_POLLER_H
109a1e991dSShuo Chen
119a1e991dSShuo Chen#include <map>
129a1e991dSShuo Chen#include <vector>
139a1e991dSShuo Chen
149a1e991dSShuo Chen#include "datetime/Timestamp.h"
159a1e991dSShuo Chen#include "EventLoop.h"
169a1e991dSShuo Chen
179a1e991dSShuo Chenstruct pollfd;
189a1e991dSShuo Chen
199a1e991dSShuo Chennamespace muduo
209a1e991dSShuo Chen{
219a1e991dSShuo Chen
229a1e991dSShuo Chenclass Channel;
239a1e991dSShuo Chen
249a1e991dSShuo Chen///
259a1e991dSShuo Chen/// IO Multiplexing with poll(2).
269a1e991dSShuo Chen///
279a1e991dSShuo Chen/// This class doesn't own the Channel objects.
289a1e991dSShuo Chenclass Poller : boost::noncopyable
299a1e991dSShuo Chen{
309a1e991dSShuo Chen public:
319a1e991dSShuo Chen  typedef std::vector<Channel*> ChannelList;
329a1e991dSShuo Chen
339a1e991dSShuo Chen  Poller(EventLoop* loop);
349a1e991dSShuo Chen  ~Poller();
359a1e991dSShuo Chen
369a1e991dSShuo Chen  /// Polls the I/O events.
379a1e991dSShuo Chen  /// Must be called in the loop thread.
389a1e991dSShuo Chen  Timestamp poll(int timeoutMs, ChannelList* activeChannels);
399a1e991dSShuo Chen
409a1e991dSShuo Chen  /// Changes the interested I/O events.
419a1e991dSShuo Chen  /// Must be called in the loop thread.
429a1e991dSShuo Chen  void updateChannel(Channel* channel);
439a1e991dSShuo Chen
440615e80eSShuo Chen  void assertInLoopThread() { ownerLoop_->assertInLoopThread(); }
459a1e991dSShuo Chen
469a1e991dSShuo Chen private:
479a1e991dSShuo Chen  void fillActiveChannels(int numEvents,
489a1e991dSShuo Chen                          ChannelList* activeChannels) const;
499a1e991dSShuo Chen
509a1e991dSShuo Chen  typedef std::vector<struct pollfd> PollFdList;
519a1e991dSShuo Chen  typedef std::map<int, Channel*> ChannelMap;
529a1e991dSShuo Chen
539a1e991dSShuo Chen  EventLoop* ownerLoop_;
549a1e991dSShuo Chen  PollFdList pollfds_;
559a1e991dSShuo Chen  ChannelMap channels_;
569a1e991dSShuo Chen};
579a1e991dSShuo Chen
589a1e991dSShuo Chen}
599a1e991dSShuo Chen#endif  // MUDUO_NET_POLLER_H
60