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
31566406ccSShuo Chen  ///
32566406ccSShuo Chen  /// Loops forever.
33566406ccSShuo Chen  ///
34566406ccSShuo Chen  /// Must be called in the same thread as creation of the object.
35566406ccSShuo Chen  ///
36c903e3e8SShuo Chen  void loop();
37566406ccSShuo Chen
384b7a333eSShuo Chen  void quit();
394b7a333eSShuo Chen
404b7a333eSShuo Chen  // internal use only
414b7a333eSShuo Chen  void updateChannel(Channel* channel);
424792573fSShuo Chen  // void removeChannel(Channel* channel);
43c903e3e8SShuo Chen
44c903e3e8SShuo Chen  void assertInLoopThread()
45c903e3e8SShuo Chen  {
46c903e3e8SShuo Chen    if (!isInLoopThread())
47c903e3e8SShuo Chen    {
48c903e3e8SShuo Chen      abortNotInLoopThread();
49c903e3e8SShuo Chen    }
50c903e3e8SShuo Chen  }
51c903e3e8SShuo Chen
52c903e3e8SShuo Chen  bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }
53c903e3e8SShuo Chen
54c903e3e8SShuo Chen private:
55c903e3e8SShuo Chen
56c903e3e8SShuo Chen  void abortNotInLoopThread();
57c903e3e8SShuo Chen
58c461658bSShuo Chen  typedef std::vector<Channel*> ChannelList;
59c461658bSShuo Chen
60c903e3e8SShuo Chen  bool looping_; /* atomic */
614b7a333eSShuo Chen  bool quit_; /* atomic */
62c903e3e8SShuo Chen  const pid_t threadId_;
634b7a333eSShuo Chen  boost::scoped_ptr<Poller> poller_;
644b7a333eSShuo Chen  ChannelList activeChannels_;
65c903e3e8SShuo Chen};
66c903e3e8SShuo Chen
67c903e3e8SShuo Chen}
68c903e3e8SShuo Chen
69c903e3e8SShuo Chen#endif  // MUDUO_NET_EVENTLOOP_H
70