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 "thread/Thread.h" 12566406ccSShuo Chen 13566406ccSShuo Chen+#include <boost/scoped_ptr.hpp> 14566406ccSShuo Chen+#include <vector> 15566406ccSShuo Chen+ 16566406ccSShuo Chen namespace muduo 17566406ccSShuo Chen { 18566406ccSShuo Chen 19566406ccSShuo Chen+class Channel; 20566406ccSShuo Chen+class Poller; 21566406ccSShuo Chen+ 22566406ccSShuo Chen class EventLoop : boost::noncopyable 23566406ccSShuo Chen { 24566406ccSShuo Chen public: 25566406ccSShuo Chen 26566406ccSShuo Chen EventLoop(); 27566406ccSShuo Chen+ 28566406ccSShuo Chen+ // force out-line dtor, for scoped_ptr members. 29566406ccSShuo Chen ~EventLoop(); 30566406ccSShuo Chen 31566406ccSShuo Chen+ /// 32566406ccSShuo Chen+ /// Loops forever. 33566406ccSShuo Chen+ /// 34566406ccSShuo Chen+ /// Must be called in the same thread as creation of the object. 35566406ccSShuo Chen+ /// 36566406ccSShuo Chen void loop(); 37566406ccSShuo Chen 38566406ccSShuo Chen+ void quit(); 39566406ccSShuo Chen+ 40566406ccSShuo Chen+ // internal use only 41566406ccSShuo Chen+ void updateChannel(Channel* channel); 42566406ccSShuo Chen+ // void removeChannel(Channel* channel); 43566406ccSShuo Chen+ 44566406ccSShuo Chen void assertInLoopThread() 45566406ccSShuo Chen { 46566406ccSShuo Chen if (!isInLoopThread()) 47566406ccSShuo Chen { 48566406ccSShuo Chen abortNotInLoopThread(); 49566406ccSShuo Chen } 50566406ccSShuo Chen } 51566406ccSShuo Chen 52566406ccSShuo Chen bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); } 53566406ccSShuo Chen 54566406ccSShuo Chen private: 55566406ccSShuo Chen 56566406ccSShuo Chen void abortNotInLoopThread(); 57566406ccSShuo Chen 58566406ccSShuo Chen+ typedef std::vector<Channel*> ChannelList; 59566406ccSShuo Chen+ 60566406ccSShuo Chen bool looping_; /* atomic */ 61566406ccSShuo Chen+ bool quit_; /* atomic */ 62566406ccSShuo Chen const pid_t threadId_; 63566406ccSShuo Chen+ boost::scoped_ptr<Poller> poller_; 64566406ccSShuo Chen+ ChannelList activeChannels_; 65566406ccSShuo Chen }; 66566406ccSShuo Chen 67566406ccSShuo Chen } 68566406ccSShuo Chen 69566406ccSShuo Chen #endif // MUDUO_NET_EVENTLOOP_H 70