Poller.h revision 2745a763
12745a763SShuo Chen// excerpts from http://code.google.com/p/muduo/
22745a763SShuo Chen//
32745a763SShuo Chen// Use of this source code is governed by a BSD-style license
42745a763SShuo Chen// that can be found in the License file.
52745a763SShuo Chen//
62745a763SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
72745a763SShuo Chen
82745a763SShuo Chen#ifndef MUDUO_NET_POLLER_H
92745a763SShuo Chen#define MUDUO_NET_POLLER_H
102745a763SShuo Chen
112745a763SShuo Chen#include <map>
122745a763SShuo Chen#include <vector>
132745a763SShuo Chen
142745a763SShuo Chen#include "datetime/Timestamp.h"
152745a763SShuo Chen#include "EventLoop.h"
162745a763SShuo Chen
172745a763SShuo Chenstruct pollfd;
182745a763SShuo Chen
192745a763SShuo Chennamespace muduo
202745a763SShuo Chen{
212745a763SShuo Chen
222745a763SShuo Chenclass Channel;
232745a763SShuo Chen
242745a763SShuo Chen///
252745a763SShuo Chen/// IO Multiplexing with poll(2).
262745a763SShuo Chen///
272745a763SShuo Chen/// This class doesn't own the Channel objects.
282745a763SShuo Chenclass Poller : boost::noncopyable
292745a763SShuo Chen{
302745a763SShuo Chen public:
312745a763SShuo Chen  typedef std::vector<Channel*> ChannelList;
322745a763SShuo Chen
332745a763SShuo Chen  Poller(EventLoop* loop);
342745a763SShuo Chen  ~Poller();
352745a763SShuo Chen
362745a763SShuo Chen  /// Polls the I/O events.
372745a763SShuo Chen  /// Must be called in the loop thread.
382745a763SShuo Chen  Timestamp poll(int timeoutMs, ChannelList* activeChannels);
392745a763SShuo Chen
402745a763SShuo Chen  /// Changes the interested I/O events.
412745a763SShuo Chen  /// Must be called in the loop thread.
422745a763SShuo Chen  void updateChannel(Channel* channel);
432745a763SShuo Chen
442745a763SShuo Chen  void assertInLoopThread()
452745a763SShuo Chen  {
462745a763SShuo Chen    ownerLoop_->assertInLoopThread();
472745a763SShuo Chen  }
482745a763SShuo Chen
492745a763SShuo Chen private:
502745a763SShuo Chen  void fillActiveChannels(int numEvents,
512745a763SShuo Chen                          ChannelList* activeChannels) const;
522745a763SShuo Chen
532745a763SShuo Chen  typedef std::vector<struct pollfd> PollFdList;
542745a763SShuo Chen  typedef std::map<int, Channel*> ChannelMap;
552745a763SShuo Chen
562745a763SShuo Chen  EventLoop* ownerLoop_;
572745a763SShuo Chen  PollFdList pollfds_;
582745a763SShuo Chen  ChannelMap channels_;
592745a763SShuo Chen};
602745a763SShuo Chen
612745a763SShuo Chen}
622745a763SShuo Chen#endif  // MUDUO_NET_POLLER_H
63