Timer.h revision 354280cf
1354280cfSShuo Chen// excerpts from http://code.google.com/p/muduo/ 2354280cfSShuo Chen// 3354280cfSShuo Chen// Use of this source code is governed by a BSD-style license 4354280cfSShuo Chen// that can be found in the License file. 5354280cfSShuo Chen// 6354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7354280cfSShuo Chen 8354280cfSShuo Chen#ifndef MUDUO_NET_TIMER_H 9354280cfSShuo Chen#define MUDUO_NET_TIMER_H 10354280cfSShuo Chen 11354280cfSShuo Chen#include <boost/noncopyable.hpp> 12354280cfSShuo Chen 13354280cfSShuo Chen#include "datetime/Timestamp.h" 14354280cfSShuo Chen#include "thread/Atomic.h" 15354280cfSShuo Chen#include "Callbacks.h" 16354280cfSShuo Chen 17354280cfSShuo Chennamespace muduo 18354280cfSShuo Chen{ 19354280cfSShuo Chen 20354280cfSShuo Chen/// 21354280cfSShuo Chen/// Internal class for timer event. 22354280cfSShuo Chen/// 23354280cfSShuo Chenclass Timer : boost::noncopyable 24354280cfSShuo Chen{ 25354280cfSShuo Chen public: 26354280cfSShuo Chen Timer(const TimerCallback& cb, Timestamp when, double interval) 27354280cfSShuo Chen : callback_(cb), 28354280cfSShuo Chen expiration_(when), 29354280cfSShuo Chen interval_(interval), 30354280cfSShuo Chen repeat_(interval > 0.0), 31354280cfSShuo Chen sequence_(s_numCreated_.incrementAndGet()) 32354280cfSShuo Chen { 33354280cfSShuo Chen } 34354280cfSShuo Chen 35354280cfSShuo Chen void run() const 36354280cfSShuo Chen { 37354280cfSShuo Chen callback_(); 38354280cfSShuo Chen } 39354280cfSShuo Chen 40354280cfSShuo Chen Timestamp expiration() const { return expiration_; } 41354280cfSShuo Chen bool repeat() const { return repeat_; } 42354280cfSShuo Chen int64_t sequence() const { return sequence_; } 43354280cfSShuo Chen 44354280cfSShuo Chen void restart(Timestamp now); 45354280cfSShuo Chen 46354280cfSShuo Chen private: 47354280cfSShuo Chen const TimerCallback callback_; 48354280cfSShuo Chen Timestamp expiration_; 49354280cfSShuo Chen const double interval_; 50354280cfSShuo Chen const bool repeat_; 51354280cfSShuo Chen const int64_t sequence_; 52354280cfSShuo Chen 53354280cfSShuo Chen static AtomicInt64 s_numCreated_; 54354280cfSShuo Chen}; 55354280cfSShuo Chen 56354280cfSShuo Chen} 57354280cfSShuo Chen#endif // MUDUO_NET_TIMER_H 58