1f4e8e3d3SShuo Chen // excerpts from http://code.google.com/p/muduo/
2f4e8e3d3SShuo Chen //
3f4e8e3d3SShuo Chen // Use of this source code is governed by a BSD-style license
4f4e8e3d3SShuo Chen // that can be found in the License file.
5f4e8e3d3SShuo Chen //
6f4e8e3d3SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
7f4e8e3d3SShuo Chen 
8f4e8e3d3SShuo Chen #ifndef MUDUO_NET_TIMER_H
9f4e8e3d3SShuo Chen #define MUDUO_NET_TIMER_H
10f4e8e3d3SShuo Chen 
11f4e8e3d3SShuo Chen #include <boost/noncopyable.hpp>
12f4e8e3d3SShuo Chen 
13f4e8e3d3SShuo Chen #include "datetime/Timestamp.h"
14f4e8e3d3SShuo Chen+#include "thread/Atomic.h"
15f4e8e3d3SShuo Chen #include "Callbacks.h"
16f4e8e3d3SShuo Chen 
17f4e8e3d3SShuo Chen namespace muduo
18f4e8e3d3SShuo Chen {
19f4e8e3d3SShuo Chen 
20f4e8e3d3SShuo Chen ///
21f4e8e3d3SShuo Chen /// Internal class for timer event.
22f4e8e3d3SShuo Chen ///
23f4e8e3d3SShuo Chen class Timer : boost::noncopyable
24f4e8e3d3SShuo Chen {
25f4e8e3d3SShuo Chen  public:
26f4e8e3d3SShuo Chen   Timer(const TimerCallback& cb, Timestamp when, double interval)
27f4e8e3d3SShuo Chen     : callback_(cb),
28f4e8e3d3SShuo Chen       expiration_(when),
29f4e8e3d3SShuo Chen       interval_(interval),
30db9c5511SShuo Chen       repeat_(interval > 0.0),
31f4e8e3d3SShuo Chen+      sequence_(s_numCreated_.incrementAndGet())
32db9c5511SShuo Chen   {
33db9c5511SShuo Chen   }
34f4e8e3d3SShuo Chen 
35f4e8e3d3SShuo Chen   void run() const
36f4e8e3d3SShuo Chen   {
37f4e8e3d3SShuo Chen     callback_();
38f4e8e3d3SShuo Chen   }
39f4e8e3d3SShuo Chen 
40f4e8e3d3SShuo Chen   Timestamp expiration() const  { return expiration_; }
41f4e8e3d3SShuo Chen   bool repeat() const { return repeat_; }
42f4e8e3d3SShuo Chen+  int64_t sequence() const { return sequence_; }
43f4e8e3d3SShuo Chen 
44f4e8e3d3SShuo Chen   void restart(Timestamp now);
45f4e8e3d3SShuo Chen 
46f4e8e3d3SShuo Chen  private:
47f4e8e3d3SShuo Chen   const TimerCallback callback_;
48f4e8e3d3SShuo Chen   Timestamp expiration_;
49f4e8e3d3SShuo Chen   const double interval_;
50f4e8e3d3SShuo Chen   const bool repeat_;
51f4e8e3d3SShuo Chen+  const int64_t sequence_;
52f4e8e3d3SShuo Chen+
53f4e8e3d3SShuo Chen+  static AtomicInt64 s_numCreated_;
54f4e8e3d3SShuo Chen };
55f4e8e3d3SShuo Chen 
56f4e8e3d3SShuo Chen }
57f4e8e3d3SShuo Chen #endif  // MUDUO_NET_TIMER_H
58