EventLoop.h revision 42bf2220
1cbe8e7d0SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2cbe8e7d0SShuo Chen// 3cbe8e7d0SShuo Chen// Use of this source code is governed by a BSD-style license 4cbe8e7d0SShuo Chen// that can be found in the License file. 5cbe8e7d0SShuo Chen// 6cbe8e7d0SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7cbe8e7d0SShuo Chen 8cbe8e7d0SShuo Chen#ifndef MUDUO_NET_EVENTLOOP_H 9cbe8e7d0SShuo Chen#define MUDUO_NET_EVENTLOOP_H 10cbe8e7d0SShuo Chen 1142bf2220SShuo Chen#include "datetime/Timestamp.h" 12cbe8e7d0SShuo Chen#include "thread/Thread.h" 1342bf2220SShuo Chen#include "Callbacks.h" 1442bf2220SShuo Chen#include "TimerId.h" 15cbe8e7d0SShuo Chen 16cbe8e7d0SShuo Chen#include <boost/scoped_ptr.hpp> 17cbe8e7d0SShuo Chen#include <vector> 18cbe8e7d0SShuo Chen 19cbe8e7d0SShuo Chennamespace muduo 20cbe8e7d0SShuo Chen{ 21cbe8e7d0SShuo Chen 22cbe8e7d0SShuo Chenclass Channel; 23cbe8e7d0SShuo Chenclass Poller; 2442bf2220SShuo Chenclass TimerQueue; 25cbe8e7d0SShuo Chen 26cbe8e7d0SShuo Chenclass EventLoop : boost::noncopyable 27cbe8e7d0SShuo Chen{ 28cbe8e7d0SShuo Chen public: 29cbe8e7d0SShuo Chen 30cbe8e7d0SShuo Chen EventLoop(); 3142bf2220SShuo Chen 3242bf2220SShuo Chen // force out-line dtor, for scoped_ptr members. 33cbe8e7d0SShuo Chen ~EventLoop(); 34cbe8e7d0SShuo Chen 3542bf2220SShuo Chen /// 3642bf2220SShuo Chen /// Loops forever. 3742bf2220SShuo Chen /// 3842bf2220SShuo Chen /// Must be called in the same thread as creation of the object. 3942bf2220SShuo Chen /// 40cbe8e7d0SShuo Chen void loop(); 4142bf2220SShuo Chen 42cbe8e7d0SShuo Chen void quit(); 43cbe8e7d0SShuo Chen 4442bf2220SShuo Chen /// 4542bf2220SShuo Chen /// Time when poll returns, usually means data arrivial. 4642bf2220SShuo Chen /// 4742bf2220SShuo Chen Timestamp pollReturnTime() const { return pollReturnTime_; } 4842bf2220SShuo Chen 4942bf2220SShuo Chen // timers 5042bf2220SShuo Chen 5142bf2220SShuo Chen /// 5242bf2220SShuo Chen /// Runs callback at 'time'. 5342bf2220SShuo Chen /// 5442bf2220SShuo Chen TimerId runAt(const Timestamp& time, const TimerCallback& cb); 5542bf2220SShuo Chen /// 5642bf2220SShuo Chen /// Runs callback after @c delay seconds. 5742bf2220SShuo Chen /// 5842bf2220SShuo Chen TimerId runAfter(double delay, const TimerCallback& cb); 5942bf2220SShuo Chen /// 6042bf2220SShuo Chen /// Runs callback every @c interval seconds. 6142bf2220SShuo Chen /// 6242bf2220SShuo Chen TimerId runEvery(double interval, const TimerCallback& cb); 6342bf2220SShuo Chen 6442bf2220SShuo Chen // void cancel(TimerId timerId); 6542bf2220SShuo Chen 66cbe8e7d0SShuo Chen // internal use only 67cbe8e7d0SShuo Chen void updateChannel(Channel* channel); 68cbe8e7d0SShuo Chen // void removeChannel(Channel* channel); 69cbe8e7d0SShuo Chen 70cbe8e7d0SShuo Chen void assertInLoopThread() 71cbe8e7d0SShuo Chen { 72cbe8e7d0SShuo Chen if (!isInLoopThread()) 73cbe8e7d0SShuo Chen { 74cbe8e7d0SShuo Chen abortNotInLoopThread(); 75cbe8e7d0SShuo Chen } 76cbe8e7d0SShuo Chen } 77cbe8e7d0SShuo Chen 78cbe8e7d0SShuo Chen bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); } 79cbe8e7d0SShuo Chen 80cbe8e7d0SShuo Chen private: 81cbe8e7d0SShuo Chen 82cbe8e7d0SShuo Chen typedef std::vector<Channel*> ChannelList; 83cbe8e7d0SShuo Chen 84cbe8e7d0SShuo Chen void abortNotInLoopThread(); 85cbe8e7d0SShuo Chen 86cbe8e7d0SShuo Chen bool looping_; /* atomic */ 87cbe8e7d0SShuo Chen bool quit_; /* atomic */ 88cbe8e7d0SShuo Chen const pid_t threadId_; 8942bf2220SShuo Chen Timestamp pollReturnTime_; 90cbe8e7d0SShuo Chen boost::scoped_ptr<Poller> poller_; 9142bf2220SShuo Chen boost::scoped_ptr<TimerQueue> timerQueue_; 92cbe8e7d0SShuo Chen ChannelList activeChannels_; 93cbe8e7d0SShuo Chen 94cbe8e7d0SShuo Chen}; 95cbe8e7d0SShuo Chen 96cbe8e7d0SShuo Chen} 97cbe8e7d0SShuo Chen 98cbe8e7d0SShuo Chen#endif // MUDUO_NET_EVENTLOOP_H 99