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