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/Mutex.h"
13 #include "thread/Thread.h"
14 #include "Callbacks.h"
15 #include "TimerId.h"
16 
17 #include <boost/scoped_ptr.hpp>
18 #include <vector>
19 
20 namespace muduo
21 {
22 
23 class Channel;
24 class Poller;
25 class TimerQueue;
26 
27 class EventLoop : boost::noncopyable
28 {
29  public:
30   typedef boost::function<void()> Functor;
31 
32   EventLoop();
33 
34   // force out-line dtor, for scoped_ptr members.
35   ~EventLoop();
36 
37   ///
38   /// Loops forever.
39   ///
40   /// Must be called in the same thread as creation of the object.
41   ///
42   void loop();
43 
44   void quit();
45 
46   ///
47   /// Time when poll returns, usually means data arrivial.
48   ///
49   Timestamp pollReturnTime() const { return pollReturnTime_; }
50 
51   /// Runs callback immediately in the loop thread.
52   /// It wakes up the loop, and run the cb.
53   /// If in the same loop thread, cb is run within the function.
54   /// Safe to call from other threads.
55   void runInLoop(const Functor& cb);
56   /// Queues callback in the loop thread.
57   /// Runs after finish pooling.
58   /// Safe to call from other threads.
59   void queueInLoop(const Functor& cb);
60 
61   // timers
62 
63   ///
64   /// Runs callback at 'time'.
65   /// Safe to call from other threads.
66   ///
67   TimerId runAt(const Timestamp& time, const TimerCallback& cb);
68   ///
69   /// Runs callback after @c delay seconds.
70   /// Safe to call from other threads.
71   ///
72   TimerId runAfter(double delay, const TimerCallback& cb);
73   ///
74   /// Runs callback every @c interval seconds.
75   /// Safe to call from other threads.
76   ///
77   TimerId runEvery(double interval, const TimerCallback& cb);
78 
79-  // void cancel(TimerId timerId);
80+  void cancel(TimerId timerId);
81 
82   // internal use only
83   void wakeup();
84   void updateChannel(Channel* channel);
85   void removeChannel(Channel* channel);
86 
87   void assertInLoopThread()
88   {
89     if (!isInLoopThread())
90     {
91       abortNotInLoopThread();
92     }
93   }
94 
95   bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }
96 
97  private:
98 
99   void abortNotInLoopThread();
100   void handleRead();  // waked up
101   void doPendingFunctors();
102 
103   typedef std::vector<Channel*> ChannelList;
104 
105   bool looping_; /* atomic */
106   bool quit_; /* atomic */
107   bool callingPendingFunctors_; /* atomic */
108   const pid_t threadId_;
109   Timestamp pollReturnTime_;
110   boost::scoped_ptr<Poller> poller_;
111   boost::scoped_ptr<TimerQueue> timerQueue_;
112   int wakeupFd_;
113   // unlike in TimerQueue, which is an internal class,
114   // we don't expose Channel to client.
115   boost::scoped_ptr<Channel> wakeupChannel_;
116   ChannelList activeChannels_;
117   MutexLock mutex_;
118   std::vector<Functor> pendingFunctors_; // @BuardedBy mutex_
119 };
120 
121 }
122 
123 #endif  // MUDUO_NET_EVENTLOOP_H
124