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 "datetime/Timestamp.h"
12#include "thread/Thread.h"
13#include "Callbacks.h"
14#include "TimerId.h"
15
16#include <boost/scoped_ptr.hpp>
17#include <vector>
18
19namespace muduo
20{
21
22class Channel;
23class Poller;
24class TimerQueue;
25
26class EventLoop : boost::noncopyable
27{
28 public:
29
30  EventLoop();
31
32  // force out-line dtor, for scoped_ptr members.
33  ~EventLoop();
34
35  ///
36  /// Loops forever.
37  ///
38  /// Must be called in the same thread as creation of the object.
39  ///
40  void loop();
41
42  void quit();
43
44  ///
45  /// Time when poll returns, usually means data arrivial.
46  ///
47  Timestamp pollReturnTime() const { return pollReturnTime_; }
48
49  // timers
50
51  ///
52  /// Runs callback at 'time'.
53  ///
54  TimerId runAt(const Timestamp& time, const TimerCallback& cb);
55  ///
56  /// Runs callback after @c delay seconds.
57  ///
58  TimerId runAfter(double delay, const TimerCallback& cb);
59  ///
60  /// Runs callback every @c interval seconds.
61  ///
62  TimerId runEvery(double interval, const TimerCallback& cb);
63
64  // void cancel(TimerId timerId);
65
66  // internal use only
67  void updateChannel(Channel* channel);
68  // void removeChannel(Channel* channel);
69
70  void assertInLoopThread()
71  {
72    if (!isInLoopThread())
73    {
74      abortNotInLoopThread();
75    }
76  }
77
78  bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }
79
80 private:
81
82  void abortNotInLoopThread();
83
84  typedef std::vector<Channel*> ChannelList;
85
86  bool looping_; /* atomic */
87  bool quit_; /* atomic */
88  const pid_t threadId_;
89  Timestamp pollReturnTime_;
90  boost::scoped_ptr<Poller> poller_;
91  boost::scoped_ptr<TimerQueue> timerQueue_;
92  ChannelList activeChannels_;
93};
94
95}
96
97#endif  // MUDUO_NET_EVENTLOOP_H
98