EventLoop.h revision c461658b
14b7a333eSShuo Chen// excerpts from http://code.google.com/p/muduo/
24b7a333eSShuo Chen//
34b7a333eSShuo Chen// Use of this source code is governed by a BSD-style license
44b7a333eSShuo Chen// that can be found in the License file.
54b7a333eSShuo Chen//
64b7a333eSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
74b7a333eSShuo Chen
8c903e3e8SShuo Chen#ifndef MUDUO_NET_EVENTLOOP_H
9c903e3e8SShuo Chen#define MUDUO_NET_EVENTLOOP_H
10c903e3e8SShuo Chen
11c903e3e8SShuo Chen#include "thread/Thread.h"
12c903e3e8SShuo Chen
134b7a333eSShuo Chen#include <boost/scoped_ptr.hpp>
144b7a333eSShuo Chen#include <vector>
154b7a333eSShuo Chen
16c903e3e8SShuo Chennamespace muduo
17c903e3e8SShuo Chen{
18c903e3e8SShuo Chen
194b7a333eSShuo Chenclass Channel;
204b7a333eSShuo Chenclass Poller;
214b7a333eSShuo Chen
22c903e3e8SShuo Chenclass EventLoop : boost::noncopyable
23c903e3e8SShuo Chen{
24c903e3e8SShuo Chen public:
25c903e3e8SShuo Chen
26c903e3e8SShuo Chen  EventLoop();
2742bf2220SShuo Chen
2842bf2220SShuo Chen  // force out-line dtor, for scoped_ptr members.
29c903e3e8SShuo Chen  ~EventLoop();
30c903e3e8SShuo Chen
31c903e3e8SShuo Chen  void loop();
324b7a333eSShuo Chen  void quit();
334b7a333eSShuo Chen
344b7a333eSShuo Chen  // internal use only
354b7a333eSShuo Chen  void updateChannel(Channel* channel);
364792573fSShuo Chen  // void removeChannel(Channel* channel);
37c903e3e8SShuo Chen
38c903e3e8SShuo Chen  void assertInLoopThread()
39c903e3e8SShuo Chen  {
40c903e3e8SShuo Chen    if (!isInLoopThread())
41c903e3e8SShuo Chen    {
42c903e3e8SShuo Chen      abortNotInLoopThread();
43c903e3e8SShuo Chen    }
44c903e3e8SShuo Chen  }
45c903e3e8SShuo Chen
46c903e3e8SShuo Chen  bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }
47c903e3e8SShuo Chen
48c903e3e8SShuo Chen private:
49c903e3e8SShuo Chen
50c903e3e8SShuo Chen  void abortNotInLoopThread();
51c903e3e8SShuo Chen
52c461658bSShuo Chen  typedef std::vector<Channel*> ChannelList;
53c461658bSShuo Chen
54c903e3e8SShuo Chen  bool looping_; /* atomic */
554b7a333eSShuo Chen  bool quit_; /* atomic */
56c903e3e8SShuo Chen  const pid_t threadId_;
574b7a333eSShuo Chen  boost::scoped_ptr<Poller> poller_;
584b7a333eSShuo Chen  ChannelList activeChannels_;
59c903e3e8SShuo Chen
60c903e3e8SShuo Chen};
61c903e3e8SShuo Chen
62c903e3e8SShuo Chen}
63c903e3e8SShuo Chen
64c903e3e8SShuo Chen#endif  // MUDUO_NET_EVENTLOOP_H
65