1566406ccSShuo Chen // excerpts from http://code.google.com/p/muduo/ 2566406ccSShuo Chen // 3566406ccSShuo Chen // Use of this source code is governed by a BSD-style license 4566406ccSShuo Chen // that can be found in the License file. 5566406ccSShuo Chen // 6566406ccSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 7566406ccSShuo Chen 8566406ccSShuo Chen #ifndef MUDUO_NET_EVENTLOOP_H 9566406ccSShuo Chen #define MUDUO_NET_EVENTLOOP_H 10566406ccSShuo Chen 11566406ccSShuo Chen+#include "datetime/Timestamp.h" 12566406ccSShuo Chen #include "thread/Thread.h" 13566406ccSShuo Chen+#include "Callbacks.h" 14566406ccSShuo Chen+#include "TimerId.h" 15566406ccSShuo Chen 16566406ccSShuo Chen #include <boost/scoped_ptr.hpp> 17566406ccSShuo Chen #include <vector> 18566406ccSShuo Chen 19566406ccSShuo Chen namespace muduo 20566406ccSShuo Chen { 21566406ccSShuo Chen 22566406ccSShuo Chen class Channel; 23566406ccSShuo Chen class Poller; 24566406ccSShuo Chen+class TimerQueue; 25566406ccSShuo Chen 26566406ccSShuo Chen class EventLoop : boost::noncopyable 27566406ccSShuo Chen { 28566406ccSShuo Chen public: 29566406ccSShuo Chen 30566406ccSShuo Chen EventLoop(); 31566406ccSShuo Chen 32566406ccSShuo Chen // force out-line dtor, for scoped_ptr members. 33566406ccSShuo Chen ~EventLoop(); 34566406ccSShuo Chen 35566406ccSShuo Chen /// 36566406ccSShuo Chen /// Loops forever. 37566406ccSShuo Chen /// 38566406ccSShuo Chen /// Must be called in the same thread as creation of the object. 39566406ccSShuo Chen /// 40566406ccSShuo Chen void loop(); 41566406ccSShuo Chen 42566406ccSShuo Chen void quit(); 43566406ccSShuo Chen 44566406ccSShuo Chen+ /// 45566406ccSShuo Chen+ /// Time when poll returns, usually means data arrivial. 46566406ccSShuo Chen+ /// 47566406ccSShuo Chen+ Timestamp pollReturnTime() const { return pollReturnTime_; } 48566406ccSShuo Chen+ 49566406ccSShuo Chen+ // timers 50566406ccSShuo Chen+ 51566406ccSShuo Chen+ /// 52566406ccSShuo Chen+ /// Runs callback at 'time'. 53566406ccSShuo Chen+ /// 54566406ccSShuo Chen+ TimerId runAt(const Timestamp& time, const TimerCallback& cb); 55566406ccSShuo Chen+ /// 56566406ccSShuo Chen+ /// Runs callback after @c delay seconds. 57566406ccSShuo Chen+ /// 58566406ccSShuo Chen+ TimerId runAfter(double delay, const TimerCallback& cb); 59566406ccSShuo Chen+ /// 60566406ccSShuo Chen+ /// Runs callback every @c interval seconds. 61566406ccSShuo Chen+ /// 62566406ccSShuo Chen+ TimerId runEvery(double interval, const TimerCallback& cb); 63566406ccSShuo Chen+ 64566406ccSShuo Chen+ // void cancel(TimerId timerId); 65566406ccSShuo Chen+ 66566406ccSShuo Chen // internal use only 67566406ccSShuo Chen void updateChannel(Channel* channel); 68566406ccSShuo Chen // void removeChannel(Channel* channel); 69566406ccSShuo Chen 70566406ccSShuo Chen void assertInLoopThread() 71566406ccSShuo Chen { 72566406ccSShuo Chen if (!isInLoopThread()) 73566406ccSShuo Chen { 74566406ccSShuo Chen abortNotInLoopThread(); 75566406ccSShuo Chen } 76566406ccSShuo Chen } 77566406ccSShuo Chen 78566406ccSShuo Chen bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); } 79566406ccSShuo Chen 80566406ccSShuo Chen private: 81566406ccSShuo Chen 82566406ccSShuo Chen void abortNotInLoopThread(); 83566406ccSShuo Chen 84566406ccSShuo Chen typedef std::vector<Channel*> ChannelList; 85566406ccSShuo Chen 86566406ccSShuo Chen bool looping_; /* atomic */ 87566406ccSShuo Chen bool quit_; /* atomic */ 88566406ccSShuo Chen const pid_t threadId_; 89566406ccSShuo Chen+ Timestamp pollReturnTime_; 90566406ccSShuo Chen boost::scoped_ptr<Poller> poller_; 91566406ccSShuo Chen+ boost::scoped_ptr<TimerQueue> timerQueue_; 92566406ccSShuo Chen ChannelList activeChannels_; 93566406ccSShuo Chen }; 94566406ccSShuo Chen 95566406ccSShuo Chen } 96566406ccSShuo Chen 97566406ccSShuo Chen #endif // MUDUO_NET_EVENTLOOP_H 98