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