1// excerpts from http://code.google.com/p/muduo/ 2// 3// Use of this source code is governed by a BSD-style license 4// that can be found in the License file. 5// 6// Author: Shuo Chen (chenshuo at chenshuo dot com) 7 8#ifndef MUDUO_NET_EVENTLOOP_H 9#define MUDUO_NET_EVENTLOOP_H 10 11#include "thread/Thread.h" 12 13#include <boost/scoped_ptr.hpp> 14#include <vector> 15 16namespace muduo 17{ 18 19class Channel; 20class Poller; 21 22class EventLoop : boost::noncopyable 23{ 24 public: 25 26 EventLoop(); 27 28 // force out-line dtor, for scoped_ptr members. 29 ~EventLoop(); 30 31 /// 32 /// Loops forever. 33 /// 34 /// Must be called in the same thread as creation of the object. 35 /// 36 void loop(); 37 38 void quit(); 39 40 // internal use only 41 void updateChannel(Channel* channel); 42 // void removeChannel(Channel* channel); 43 44 void assertInLoopThread() 45 { 46 if (!isInLoopThread()) 47 { 48 abortNotInLoopThread(); 49 } 50 } 51 52 bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); } 53 54 private: 55 56 void abortNotInLoopThread(); 57 58 typedef std::vector<Channel*> ChannelList; 59 60 bool looping_; /* atomic */ 61 bool quit_; /* atomic */ 62 const pid_t threadId_; 63 boost::scoped_ptr<Poller> poller_; 64 ChannelList activeChannels_; 65}; 66 67} 68 69#endif // MUDUO_NET_EVENTLOOP_H 70