140161064SShuo Chen// excerpts from http://code.google.com/p/muduo/
240161064SShuo Chen//
340161064SShuo Chen// Use of this source code is governed by a BSD-style license
440161064SShuo Chen// that can be found in the License file.
540161064SShuo Chen//
640161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
740161064SShuo Chen
840161064SShuo Chen#ifndef MUDUO_NET_TIMER_H
940161064SShuo Chen#define MUDUO_NET_TIMER_H
1040161064SShuo Chen
1140161064SShuo Chen#include <boost/noncopyable.hpp>
1240161064SShuo Chen
1340161064SShuo Chen#include "datetime/Timestamp.h"
14f4e8e3d3SShuo Chen#include "thread/Atomic.h"
1540161064SShuo Chen#include "Callbacks.h"
1640161064SShuo Chen
1740161064SShuo Chennamespace muduo
1840161064SShuo Chen{
1940161064SShuo Chen
2040161064SShuo Chen///
2140161064SShuo Chen/// Internal class for timer event.
2240161064SShuo Chen///
2340161064SShuo Chenclass Timer : boost::noncopyable
2440161064SShuo Chen{
2540161064SShuo Chen public:
2640161064SShuo Chen  Timer(const TimerCallback& cb, Timestamp when, double interval)
2740161064SShuo Chen    : callback_(cb),
2840161064SShuo Chen      expiration_(when),
2940161064SShuo Chen      interval_(interval),
30f4e8e3d3SShuo Chen      repeat_(interval > 0.0),
31f4e8e3d3SShuo Chen      sequence_(s_numCreated_.incrementAndGet())
32f4e8e3d3SShuo Chen  {
33f4e8e3d3SShuo Chen  }
3440161064SShuo Chen
3540161064SShuo Chen  void run() const
3640161064SShuo Chen  {
3740161064SShuo Chen    callback_();
3840161064SShuo Chen  }
3940161064SShuo Chen
4040161064SShuo Chen  Timestamp expiration() const  { return expiration_; }
4140161064SShuo Chen  bool repeat() const { return repeat_; }
42f4e8e3d3SShuo Chen  int64_t sequence() const { return sequence_; }
4340161064SShuo Chen
4440161064SShuo Chen  void restart(Timestamp now);
4540161064SShuo Chen
4640161064SShuo Chen private:
4740161064SShuo Chen  const TimerCallback callback_;
4840161064SShuo Chen  Timestamp expiration_;
4940161064SShuo Chen  const double interval_;
5040161064SShuo Chen  const bool repeat_;
51f4e8e3d3SShuo Chen  const int64_t sequence_;
52f4e8e3d3SShuo Chen
53f4e8e3d3SShuo Chen  static AtomicInt64 s_numCreated_;
5440161064SShuo Chen};
5540161064SShuo Chen
5640161064SShuo Chen}
5740161064SShuo Chen#endif  // MUDUO_NET_TIMER_H
58