├── README.md ├── ydx_timer.cpp ├── ydx_timer.h ├── ydx_timer_set.cpp ├── ydx_timer_set.h ├── ydx_timer_wheel.cpp ├── ydx_timer_wheel.h └── ydx_timerid.h /README.md: -------------------------------------------------------------------------------- 1 | # ydx_timer_util 2 | 定时器方案,采用epoll的异步驱动和采用时间轮推动两种方案。测试在20万个定时器同时存在(以50us的速率仍然持续创建)的情况下内存为20M,cpu占用2%左右 3 | 1.timer_set为timerfd方式,用一个时间句柄控制所有定时器节点。 4 | 2.timer_wheel为时间轮推动方式。 5 | -------------------------------------------------------------------------------- /ydx_timer.cpp: -------------------------------------------------------------------------------- 1 | #include "ydx_timer.h" 2 | 3 | using namespace ydx; 4 | 5 | 6 | AtomicInt64 Timer::timer_sequence_creater; 7 | 8 | void Timer::restart(Timestamp now) 9 | { 10 | if(repeat_) 11 | { 12 | expiration_ = addTime(now, interval_); 13 | } 14 | else 15 | { 16 | expiration_ = Timestamp::invalid(); 17 | } 18 | } -------------------------------------------------------------------------------- /ydx_timer.h: -------------------------------------------------------------------------------- 1 | #ifndef __YDX_TIMER_H__ 2 | #define __YDX_TIMER_H__ 3 | 4 | #include 5 | #include "timestamp.h" 6 | #include "callback_types.h" 7 | #include "ydx_atomic.h" 8 | 9 | namespace ydx 10 | { 11 | 12 | class Timer : boost::noncopyable 13 | { 14 | public: 15 | 16 | Timer(const TimerCallback &cb, Timestamp when, double interval) 17 | : timer_func_(cb), 18 | expiration_(when), 19 | interval_(interval), 20 | repeat_(interval > 0.0), 21 | sequence_(timer_sequence_creater.incrementAndGet()) 22 | {} 23 | 24 | void run() const 25 | { 26 | timer_func_(); 27 | } 28 | 29 | Timestamp expiration() const { return expiration_; } 30 | bool repeat() const {return repeat_; } 31 | int64_t sequence() const {return sequence_; } 32 | void restart(Timestamp now); 33 | static int64_t index_create() {return timer_sequence_creater.get();} 34 | 35 | private: 36 | const TimerCallback timer_func_; 37 | Timestamp expiration_; 38 | const double interval_; 39 | const bool repeat_; 40 | const int64_t sequence_; 41 | 42 | 43 | static AtomicInt64 timer_sequence_creater; 44 | }; 45 | 46 | 47 | 48 | } 49 | 50 | #endif -------------------------------------------------------------------------------- /ydx_timer_set.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crspecter/ydx_timer_util/9c00becd0e2e4c38d7afd33166b7f22c0f73382d/ydx_timer_set.cpp -------------------------------------------------------------------------------- /ydx_timer_set.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crspecter/ydx_timer_util/9c00becd0e2e4c38d7afd33166b7f22c0f73382d/ydx_timer_set.h -------------------------------------------------------------------------------- /ydx_timer_wheel.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crspecter/ydx_timer_util/9c00becd0e2e4c38d7afd33166b7f22c0f73382d/ydx_timer_wheel.cpp -------------------------------------------------------------------------------- /ydx_timer_wheel.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crspecter/ydx_timer_util/9c00becd0e2e4c38d7afd33166b7f22c0f73382d/ydx_timer_wheel.h -------------------------------------------------------------------------------- /ydx_timerid.h: -------------------------------------------------------------------------------- 1 | #ifndef __YDX_TIMERID_H__ 2 | #define __YDX_TIMERID_H__ 3 | 4 | namespace ydx 5 | { 6 | class Timer; 7 | 8 | /// 9 | /// An opaque identifier, for canceling Timer. 10 | /// 11 | class TimerId 12 | { 13 | public: 14 | TimerId() 15 | : timer_(NULL), 16 | sequence_(0) 17 | { 18 | } 19 | 20 | TimerId(Timer* timer, int64_t seq) 21 | : timer_(timer), 22 | sequence_(seq) 23 | { 24 | } 25 | 26 | // default copy-ctor, dtor and assignment are okay 27 | 28 | friend class TimerSet; 29 | 30 | private: 31 | Timer* timer_; 32 | int64_t sequence_; 33 | }; 34 | 35 | } 36 | 37 | 38 | #endif 39 | --------------------------------------------------------------------------------