EventLoop.h revision 4b7a333e
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();
27c903e3e8SShuo Chen  ~EventLoop();
28c903e3e8SShuo Chen
29c903e3e8SShuo Chen  void loop();
304b7a333eSShuo Chen  void quit();
314b7a333eSShuo Chen
324b7a333eSShuo Chen  // internal use only
334b7a333eSShuo Chen  void updateChannel(Channel* channel);
344b7a333eSShuo Chen  void removeChannel(Channel* channel);
35c903e3e8SShuo Chen
36c903e3e8SShuo Chen  void assertInLoopThread()
37c903e3e8SShuo Chen  {
38c903e3e8SShuo Chen    if (!isInLoopThread())
39c903e3e8SShuo Chen    {
40c903e3e8SShuo Chen      abortNotInLoopThread();
41c903e3e8SShuo Chen    }
42c903e3e8SShuo Chen  }
43c903e3e8SShuo Chen
44c903e3e8SShuo Chen  bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }
45c903e3e8SShuo Chen
46c903e3e8SShuo Chen private:
47c903e3e8SShuo Chen
484b7a333eSShuo Chen  typedef std::vector<Channel*> ChannelList;
494b7a333eSShuo Chen
50c903e3e8SShuo Chen  void abortNotInLoopThread();
51c903e3e8SShuo Chen
52c903e3e8SShuo Chen  bool looping_; /* atomic */
534b7a333eSShuo Chen  bool quit_; /* atomic */
54c903e3e8SShuo Chen  const pid_t threadId_;
554b7a333eSShuo Chen  boost::scoped_ptr<Poller> poller_;
564b7a333eSShuo Chen  ChannelList activeChannels_;
57c903e3e8SShuo Chen
58c903e3e8SShuo Chen};
59c903e3e8SShuo Chen
60c903e3e8SShuo Chen}
61c903e3e8SShuo Chen
62c903e3e8SShuo Chen#endif  // MUDUO_NET_EVENTLOOP_H
63