diff options
| author | Alan Conway <aconway@apache.org> | 2010-01-29 22:59:09 +0000 |
|---|---|---|
| committer | Alan Conway <aconway@apache.org> | 2010-01-29 22:59:09 +0000 |
| commit | a78bf7b9144ed3db8e798124595f48fc75231cce (patch) | |
| tree | b7284043fe639a2c6a880fc33836ce0b51d21b7e /cpp/src/qpid/sys | |
| parent | 726b23f43478a85b961365e4de3a9302a261f6b3 (diff) | |
| download | qpid-python-a78bf7b9144ed3db8e798124595f48fc75231cce.tar.gz | |
Replace PeriodicTimer with ClusterTimer, which inherits from Timer.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@904656 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/sys')
| -rw-r--r-- | cpp/src/qpid/sys/Timer.cpp | 20 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/Timer.h | 39 |
2 files changed, 36 insertions, 23 deletions
diff --git a/cpp/src/qpid/sys/Timer.cpp b/cpp/src/qpid/sys/Timer.cpp index c18fd93538..fcd58b187f 100644 --- a/cpp/src/qpid/sys/Timer.cpp +++ b/cpp/src/qpid/sys/Timer.cpp @@ -30,14 +30,16 @@ using std::max; namespace qpid { namespace sys { -TimerTask::TimerTask(Duration timeout) : +TimerTask::TimerTask(Duration timeout, const std::string& n) : + name(n), sortTime(AbsTime::FarFuture()), period(timeout), nextFireTime(AbsTime::now(), timeout), cancelled(false) {} -TimerTask::TimerTask(AbsTime time) : +TimerTask::TimerTask(AbsTime time, const std::string& n) : + name(n), sortTime(AbsTime::FarFuture()), period(0), nextFireTime(time), @@ -102,13 +104,15 @@ void Timer::run() { ScopedLock<Mutex> l(t->callbackLock); if (t->cancelled) { + drop(t); if (delay > 500 * TIME_MSEC) { - QPID_LOG(debug, "cancelled Timer woken up " << delay / TIME_MSEC << "ms late"); + QPID_LOG(debug, "cancelled Timer woken up " << delay / TIME_MSEC + << "ms late"); } continue; } else if(Duration(t->nextFireTime, start) >= 0) { Monitor::ScopedUnlock u(monitor); - t->fireTask(); + fire(t); // Warn on callback overrun AbsTime end(AbsTime::now()); Duration overrun(tasks.top()->nextFireTime, end); @@ -169,6 +173,14 @@ void Timer::stop() runner.join(); } +// Allow subclasses to override behavior when firing a task. +void Timer::fire(boost::intrusive_ptr<TimerTask> t) { + t->fireTask(); +} + +// Provided for subclasses: called when a task is droped. +void Timer::drop(boost::intrusive_ptr<TimerTask>) {} + bool operator<(const intrusive_ptr<TimerTask>& a, const intrusive_ptr<TimerTask>& b) { diff --git a/cpp/src/qpid/sys/Timer.h b/cpp/src/qpid/sys/Timer.h index 303d44a299..4a579fe032 100644 --- a/cpp/src/qpid/sys/Timer.h +++ b/cpp/src/qpid/sys/Timer.h @@ -38,10 +38,11 @@ namespace sys { class Timer; class TimerTask : public RefCounted { - friend class Timer; - friend bool operator<(const boost::intrusive_ptr<TimerTask>&, - const boost::intrusive_ptr<TimerTask>&); + friend class Timer; + friend bool operator<(const boost::intrusive_ptr<TimerTask>&, + const boost::intrusive_ptr<TimerTask>&); + std::string name; AbsTime sortTime; Duration period; AbsTime nextFireTime; @@ -51,30 +52,26 @@ class TimerTask : public RefCounted { bool readyToFire() const; void fireTask(); -public: - QPID_COMMON_EXTERN TimerTask(Duration period); - QPID_COMMON_EXTERN TimerTask(AbsTime fireTime); + public: + QPID_COMMON_EXTERN TimerTask(Duration period, const std::string& name=std::string()); + QPID_COMMON_EXTERN TimerTask(AbsTime fireTime, const std::string& name=std::string()); QPID_COMMON_EXTERN virtual ~TimerTask(); QPID_COMMON_EXTERN void setupNextFire(); QPID_COMMON_EXTERN void restart(); QPID_COMMON_EXTERN void cancel(); -protected: + std::string getName() const { return name; } + + protected: // Must be overridden with callback virtual void fire() = 0; }; // For the priority_queue order bool operator<(const boost::intrusive_ptr<TimerTask>& a, - const boost::intrusive_ptr<TimerTask>& b); - -/** - A timer to trigger tasks that are local to one broker. + const boost::intrusive_ptr<TimerTask>& b); - For periodic tasks that should be synchronized across all brokers - in a cluster, use qpid::sys::PeriodicTimer. - */ class Timer : private Runnable { qpid::sys::Monitor monitor; std::priority_queue<boost::intrusive_ptr<TimerTask> > tasks; @@ -84,13 +81,17 @@ class Timer : private Runnable { // Runnable interface void run(); -public: + public: QPID_COMMON_EXTERN Timer(); - QPID_COMMON_EXTERN ~Timer(); + QPID_COMMON_EXTERN virtual ~Timer(); + + QPID_COMMON_EXTERN virtual void add(boost::intrusive_ptr<TimerTask> task); + QPID_COMMON_EXTERN virtual void start(); + QPID_COMMON_EXTERN virtual void stop(); - QPID_COMMON_EXTERN void add(boost::intrusive_ptr<TimerTask> task); - QPID_COMMON_EXTERN void start(); - QPID_COMMON_EXTERN void stop(); + protected: + QPID_COMMON_EXTERN virtual void fire(boost::intrusive_ptr<TimerTask> task); + QPID_COMMON_EXTERN virtual void drop(boost::intrusive_ptr<TimerTask> task); }; |
