EPoller.h revision 70523619
170523619SShuo Chen// excerpts from http://code.google.com/p/muduo/ 270523619SShuo Chen// 370523619SShuo Chen// Use of this source code is governed by a BSD-style license 470523619SShuo Chen// that can be found in the License file. 570523619SShuo Chen// 670523619SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 770523619SShuo Chen 870523619SShuo Chen#ifndef MUDUO_NET_EPOLLER_H 970523619SShuo Chen#define MUDUO_NET_EPOLLER_H 1070523619SShuo Chen 1170523619SShuo Chen#include <map> 1270523619SShuo Chen#include <vector> 1370523619SShuo Chen 1470523619SShuo Chen#include "datetime/Timestamp.h" 1570523619SShuo Chen#include "EventLoop.h" 1670523619SShuo Chen 1770523619SShuo Chenstruct epoll_event; 1870523619SShuo Chen 1970523619SShuo Chennamespace muduo 2070523619SShuo Chen{ 2170523619SShuo Chen 2270523619SShuo Chenclass Channel; 2370523619SShuo Chen 2470523619SShuo Chen/// 2570523619SShuo Chen/// IO Multiplexing with epoll(4). 2670523619SShuo Chen/// 2770523619SShuo Chen/// This class doesn't own the Channel objects. 2870523619SShuo Chenclass EPoller : boost::noncopyable 2970523619SShuo Chen{ 3070523619SShuo Chen public: 3170523619SShuo Chen typedef std::vector<Channel*> ChannelList; 3270523619SShuo Chen 3370523619SShuo Chen EPoller(EventLoop* loop); 3470523619SShuo Chen ~EPoller(); 3570523619SShuo Chen 3670523619SShuo Chen /// Polls the I/O events. 3770523619SShuo Chen /// Must be called in the loop thread. 3870523619SShuo Chen Timestamp poll(int timeoutMs, ChannelList* activeChannels); 3970523619SShuo Chen 4070523619SShuo Chen /// Changes the interested I/O events. 4170523619SShuo Chen /// Must be called in the loop thread. 4270523619SShuo Chen void updateChannel(Channel* channel); 4370523619SShuo Chen /// Remove the channel, when it destructs. 4470523619SShuo Chen /// Must be called in the loop thread. 4570523619SShuo Chen void removeChannel(Channel* channel); 4670523619SShuo Chen 4770523619SShuo Chen void assertInLoopThread() { ownerLoop_->assertInLoopThread(); } 4870523619SShuo Chen 4970523619SShuo Chen private: 5070523619SShuo Chen static const int kInitEventListSize = 16; 5170523619SShuo Chen 5270523619SShuo Chen void fillActiveChannels(int numEvents, 5370523619SShuo Chen ChannelList* activeChannels) const; 5470523619SShuo Chen void update(int operation, Channel* channel); 5570523619SShuo Chen 5670523619SShuo Chen typedef std::vector<struct epoll_event> EventList; 5770523619SShuo Chen typedef std::map<int, Channel*> ChannelMap; 5870523619SShuo Chen 5970523619SShuo Chen EventLoop* ownerLoop_; 6070523619SShuo Chen int epollfd_; 6170523619SShuo Chen EventList events_; 6270523619SShuo Chen ChannelMap channels_; 6370523619SShuo Chen}; 6470523619SShuo Chen 6570523619SShuo Chen} 6670523619SShuo Chen#endif // MUDUO_NET_EPOLLER_H 67