Poller.h revision 4b7a333e
14b7a333eSShuo Chen// excerpts from http://code.google.com/p/muduo/
24b7a333eSShuo Chen//
34b7a333eSShuo Chen// Use of this source code is governed by a BSD-style license
44b7a333eSShuo Chen// that can be found in the License file.
54b7a333eSShuo Chen//
64b7a333eSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
74b7a333eSShuo Chen
84b7a333eSShuo Chen#ifndef MUDUO_NET_POLLER_H
94b7a333eSShuo Chen#define MUDUO_NET_POLLER_H
104b7a333eSShuo Chen
114b7a333eSShuo Chen#include <map>
124b7a333eSShuo Chen#include <vector>
134b7a333eSShuo Chen
144b7a333eSShuo Chen#include "datetime/Timestamp.h"
154b7a333eSShuo Chen#include "EventLoop.h"
164b7a333eSShuo Chen
174b7a333eSShuo Chenstruct pollfd;
184b7a333eSShuo Chen
194b7a333eSShuo Chennamespace muduo
204b7a333eSShuo Chen{
214b7a333eSShuo Chen
224b7a333eSShuo Chenclass Channel;
234b7a333eSShuo Chen
244b7a333eSShuo Chen///
254b7a333eSShuo Chen/// IO Multiplexing with poll(2).
264b7a333eSShuo Chen///
274b7a333eSShuo Chen/// This class doesn't own the Channel objects.
284b7a333eSShuo Chenclass Poller : boost::noncopyable
294b7a333eSShuo Chen{
304b7a333eSShuo Chen public:
314b7a333eSShuo Chen  typedef std::vector<Channel*> ChannelList;
324b7a333eSShuo Chen
334b7a333eSShuo Chen  Poller(EventLoop* loop);
344b7a333eSShuo Chen  ~Poller();
354b7a333eSShuo Chen
364b7a333eSShuo Chen  /// Polls the I/O events.
374b7a333eSShuo Chen  /// Must be called in the loop thread.
384b7a333eSShuo Chen  Timestamp poll(int timeoutMs, ChannelList* activeChannels);
394b7a333eSShuo Chen
404b7a333eSShuo Chen  /// Changes the interested I/O events.
414b7a333eSShuo Chen  /// Must be called in the loop thread.
424b7a333eSShuo Chen  void updateChannel(Channel* channel);
434b7a333eSShuo Chen
444b7a333eSShuo Chen  /// Remove the channel, when it destructs.
454b7a333eSShuo Chen  /// Must be called in the loop thread.
464b7a333eSShuo Chen  void removeChannel(Channel* channel);
474b7a333eSShuo Chen
484b7a333eSShuo Chen  void assertInLoopThread()
494b7a333eSShuo Chen  {
504b7a333eSShuo Chen    ownerLoop_->assertInLoopThread();
514b7a333eSShuo Chen  }
524b7a333eSShuo Chen
534b7a333eSShuo Chen private:
544b7a333eSShuo Chen  void fillActiveChannels(int numEvents,
554b7a333eSShuo Chen                          ChannelList* activeChannels) const;
564b7a333eSShuo Chen
574b7a333eSShuo Chen  typedef std::vector<struct pollfd> PollFdList;
584b7a333eSShuo Chen  typedef std::map<int, Channel*> ChannelMap;
594b7a333eSShuo Chen
604b7a333eSShuo Chen  EventLoop* ownerLoop_;
614b7a333eSShuo Chen  PollFdList pollfds_;
624b7a333eSShuo Chen  ChannelMap channels_;
634b7a333eSShuo Chen};
644b7a333eSShuo Chen
654b7a333eSShuo Chen}
664b7a333eSShuo Chen#endif  // MUDUO_NET_POLLER_H
67