diff options
author | Alan Conway <aconway@apache.org> | 2007-12-06 18:22:17 +0000 |
---|---|---|
committer | Alan Conway <aconway@apache.org> | 2007-12-06 18:22:17 +0000 |
commit | 430e644a4a647c256ae51682d7230c35d954073e (patch) | |
tree | ef03ef6d4f1e1d7451bf26b943a325e425ef2b58 /cpp | |
parent | 8a3e8a01188a500401196601d78ecf146ab8ba66 (diff) | |
download | qpid-python-430e644a4a647c256ae51682d7230c35d954073e.tar.gz |
Removed redundant TimerA, use intrusive_ptr for Timer.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@601803 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/src/qpid/broker/DtxManager.cpp | 8 | ||||
-rw-r--r-- | cpp/src/qpid/broker/DtxTimeout.h | 1 | ||||
-rw-r--r-- | cpp/src/qpid/broker/DtxWorkRecord.h | 6 | ||||
-rw-r--r-- | cpp/src/qpid/broker/Timer.cpp | 100 | ||||
-rw-r--r-- | cpp/src/qpid/broker/Timer.h | 79 | ||||
-rw-r--r-- | cpp/src/qpid/management/ManagementAgent.cpp | 4 | ||||
-rw-r--r-- | cpp/src/tests/TimerTest.cpp | 9 |
7 files changed, 41 insertions, 166 deletions
diff --git a/cpp/src/qpid/broker/DtxManager.cpp b/cpp/src/qpid/broker/DtxManager.cpp index 1d8b64eb5b..039a52cf2c 100644 --- a/cpp/src/qpid/broker/DtxManager.cpp +++ b/cpp/src/qpid/broker/DtxManager.cpp @@ -119,12 +119,12 @@ DtxWorkRecord* DtxManager::createWork(std::string xid) void DtxManager::setTimeout(const std::string& xid, uint32_t secs) { DtxWorkRecord* record = getWork(xid); - DtxTimeout::shared_ptr timeout = record->getTimeout(); + intrusive_ptr<DtxTimeout> timeout = record->getTimeout(); if (timeout.get()) { if (timeout->timeout == secs) return;//no need to do anything further if timeout hasn't changed timeout->cancelled = true; } - timeout = DtxTimeout::shared_ptr(new DtxTimeout(secs, *this, xid)); + timeout = intrusive_ptr<DtxTimeout>(new DtxTimeout(secs, *this, xid)); record->setTimeout(timeout); timer.add(boost::static_pointer_cast<TimerTask>(timeout)); @@ -132,7 +132,7 @@ void DtxManager::setTimeout(const std::string& xid, uint32_t secs) uint32_t DtxManager::getTimeout(const std::string& xid) { - DtxTimeout::shared_ptr timeout = getWork(xid)->getTimeout(); + intrusive_ptr<DtxTimeout> timeout = getWork(xid)->getTimeout(); return !timeout ? 0 : timeout->timeout; } @@ -145,7 +145,7 @@ void DtxManager::timedout(const std::string& xid) } else { get_pointer(i)->timedout(); //TODO: do we want to have a timed task to cleanup, or can we rely on an explicit completion? - //timer.add(TimerTask::shared_ptr(new DtxCleanup(60*30/*30 mins*/, *this, xid))); + //timer.add(intrusive_ptr<TimerTask>(new DtxCleanup(60*30/*30 mins*/, *this, xid))); } } diff --git a/cpp/src/qpid/broker/DtxTimeout.h b/cpp/src/qpid/broker/DtxTimeout.h index 7d0b8622d0..6e949eab0d 100644 --- a/cpp/src/qpid/broker/DtxTimeout.h +++ b/cpp/src/qpid/broker/DtxTimeout.h @@ -33,7 +33,6 @@ struct DtxTimeoutException : public Exception {}; struct DtxTimeout : public TimerTask { - typedef boost::shared_ptr<DtxTimeout> shared_ptr; const uint32_t timeout; DtxManager& mgr; const std::string xid; diff --git a/cpp/src/qpid/broker/DtxWorkRecord.h b/cpp/src/qpid/broker/DtxWorkRecord.h index 1e26dbebcd..cd0e13e991 100644 --- a/cpp/src/qpid/broker/DtxWorkRecord.h +++ b/cpp/src/qpid/broker/DtxWorkRecord.h @@ -48,7 +48,7 @@ class DtxWorkRecord bool rolledback; bool prepared; bool expired; - DtxTimeout::shared_ptr timeout; + intrusive_ptr<DtxTimeout> timeout; Work work; std::auto_ptr<TPCTransactionContext> txn; qpid::sys::Mutex lock; @@ -65,8 +65,8 @@ public: void add(DtxBuffer::shared_ptr ops); void recover(std::auto_ptr<TPCTransactionContext> txn, DtxBuffer::shared_ptr ops); void timedout(); - void setTimeout(DtxTimeout::shared_ptr t) { timeout = t; } - DtxTimeout::shared_ptr getTimeout() { return timeout; } + void setTimeout(intrusive_ptr<DtxTimeout> t) { timeout = t; } + intrusive_ptr<DtxTimeout> getTimeout() { return timeout; } }; } diff --git a/cpp/src/qpid/broker/Timer.cpp b/cpp/src/qpid/broker/Timer.cpp index 653e1dffd1..28b1aa56d7 100644 --- a/cpp/src/qpid/broker/Timer.cpp +++ b/cpp/src/qpid/broker/Timer.cpp @@ -27,9 +27,14 @@ using qpid::sys::Monitor; using qpid::sys::Thread; using namespace qpid::broker; -TimerTask::TimerTask(Duration timeout) : duration(timeout), time(AbsTime::now(), timeout), cancelled(false) {} -TimerTask::TimerTask(AbsTime _time) : duration(0), time(_time), cancelled(false) {} +TimerTask::TimerTask(Duration timeout) : + duration(timeout), time(AbsTime::now(), timeout), cancelled(false) {} + +TimerTask::TimerTask(AbsTime _time) : + duration(0), time(_time), cancelled(false) {} + TimerTask::~TimerTask(){} + void TimerTask::reset() { time.reset(AbsTime::now(), duration); } Timer::Timer() : active(false) @@ -49,7 +54,7 @@ void Timer::run() if (tasks.empty()) { monitor.wait(); } else { - TimerTask::shared_ptr t = tasks.top(); + intrusive_ptr<TimerTask> t = tasks.top(); if (t->cancelled) { tasks.pop(); } else if(t->time < AbsTime::now()) { @@ -62,7 +67,7 @@ void Timer::run() } } -void Timer::add(TimerTask::shared_ptr task) +void Timer::add(intrusive_ptr<TimerTask> task) { Monitor::ScopedLock l(monitor); tasks.push(task); @@ -93,92 +98,9 @@ void Timer::signalStop() } } -bool Later::operator()(const TimerTask::shared_ptr& a, const TimerTask::shared_ptr& b) const +bool Later::operator()(const intrusive_ptr<TimerTask>& a, + const intrusive_ptr<TimerTask>& b) const { return a.get() && b.get() && a->time > b->time; } -bool LaterA::operator()(const TimerTaskA::intrusive_ptr& a, const TimerTaskA::intrusive_ptr& b) const -{ - return a.get() && b.get() && a->time > b->time; -} - -TimerA::TimerA(): active(false) -{ - start(); -} - -TimerA::~TimerA() -{ - stop(); -} - -void TimerA::run() -{ - Monitor::ScopedLock l(monitor); - while(active){ - if (itasks.empty()) { - monitor.wait(); - } else { - TimerTaskA::intrusive_ptr t = itasks.top(); - if (t->cancelled) { - itasks.pop(); - } else if(t->time < AbsTime::now()) { - itasks.pop(); - t->fire(); - } else { - monitor.wait(t->time); - } - } - } -// ::run(); -} - -TimerTaskA::TimerTaskA(qpid::sys::Duration timeout): TimerTask(timeout), ref_cnt(0) {} -TimerTaskA::TimerTaskA(qpid::sys::AbsTime time): TimerTask(time), ref_cnt(0) {} -TimerTaskA::~TimerTaskA() {} - - -void TimerA::add(TimerTaskA::intrusive_ptr& task) -{ - Monitor::ScopedLock l(monitor); - itasks.push(task); - monitor.notify(); -} - -void TimerA::start() -{ - Monitor::ScopedLock l(monitor); - if (!active) { - active = true; - runner = Thread(this); - } -} - -void TimerA::stop() -{ - signalStop(); - runner.join(); -} - -void TimerA::signalStop() -{ - Monitor::ScopedLock l(monitor); - if (active) { - active = false; - monitor.notifyAll(); - } -} - -void qpid::broker::intrusive_ptr_add_ref(TimerTaskA* fe) -{ - fe->ref(); -} - -void qpid::broker::intrusive_ptr_release(TimerTaskA* fe) -{ - fe->unref(); - if (fe->refcnt() == 0) delete fe; -} - - diff --git a/cpp/src/qpid/broker/Timer.h b/cpp/src/qpid/broker/Timer.h index aa17525f95..d1f606f326 100644 --- a/cpp/src/qpid/broker/Timer.h +++ b/cpp/src/qpid/broker/Timer.h @@ -21,22 +21,19 @@ #ifndef _Timer_ #define _Timer_ -#include <memory> -#include <queue> -#include <boost/shared_ptr.hpp> -#include <boost/intrusive_ptr.hpp> #include "qpid/sys/Monitor.h" #include "qpid/sys/Thread.h" #include "qpid/sys/Runnable.h" +#include "qpid/RefCounted.h" + +#include <memory> +#include <queue> namespace qpid { namespace broker { -struct TimerTask -{ +struct TimerTask : public RefCounted { const qpid::sys::Duration duration; - typedef boost::shared_ptr<TimerTask> shared_ptr; - qpid::sys::AbsTime time; volatile bool cancelled; @@ -47,79 +44,35 @@ struct TimerTask virtual void fire() = 0; }; -struct TimerTaskA : public TimerTask -{ - typedef boost::intrusive_ptr<TimerTaskA> intrusive_ptr; - - TimerTaskA(qpid::sys::Duration timeout); - TimerTaskA(qpid::sys::AbsTime time); - virtual ~TimerTaskA(); - - size_t ref_cnt; - inline size_t refcnt(void) { return ref_cnt;} - inline void ref(void) { ref_cnt++; } - inline void unref(void) { ref_cnt--; } -}; - -struct Later -{ - bool operator()(const TimerTask::shared_ptr& a, const TimerTask::shared_ptr& b) const; +struct Later { + bool operator()(const intrusive_ptr<TimerTask>& a, + const intrusive_ptr<TimerTask>& b) const; }; -struct LaterA -{ - bool operator()(const TimerTaskA::intrusive_ptr& a, const TimerTaskA::intrusive_ptr& b) const; -}; - - -class Timer : private qpid::sys::Runnable -{ -protected: +class Timer : private qpid::sys::Runnable { + protected: qpid::sys::Monitor monitor; - std::priority_queue<TimerTask::shared_ptr, std::vector<TimerTask::shared_ptr>, Later> tasks; + std::priority_queue<intrusive_ptr<TimerTask>, + std::vector<intrusive_ptr<TimerTask> >, + Later> tasks; qpid::sys::Thread runner; bool active; virtual void run(); void signalStop(); -public: + public: Timer(); virtual ~Timer(); - void add(TimerTask::shared_ptr task); + void add(intrusive_ptr<TimerTask> task); void start(); void stop(); }; -class TimerA : private qpid::sys::Runnable -{ -protected: - qpid::sys::Monitor monitor; - std::priority_queue<TimerTaskA::intrusive_ptr&, std::vector<TimerTaskA::intrusive_ptr>, - LaterA> itasks; - qpid::sys::Thread runner; - bool active; - - virtual void run(); - void signalStop(); - -public: - TimerA(); - virtual ~TimerA(); - - void add(TimerTaskA::intrusive_ptr& task); - void start(); - void stop(); -}; - -void intrusive_ptr_add_ref(TimerTaskA* r); -void intrusive_ptr_release(TimerTaskA* r); - -} -} +}} #endif diff --git a/cpp/src/qpid/management/ManagementAgent.cpp b/cpp/src/qpid/management/ManagementAgent.cpp index fa51a6b82a..85c2acce1d 100644 --- a/cpp/src/qpid/management/ManagementAgent.cpp +++ b/cpp/src/qpid/management/ManagementAgent.cpp @@ -36,7 +36,7 @@ ManagementAgent::shared_ptr ManagementAgent::agent; ManagementAgent::ManagementAgent (uint16_t _interval) : interval (_interval) { - timer.add (TimerTask::shared_ptr (new Periodic(*this, interval))); + timer.add (intrusive_ptr<TimerTask> (new Periodic(*this, interval))); nextObjectId = uint64_t (qpid::sys::Duration (qpid::sys::now ())); } @@ -68,7 +68,7 @@ ManagementAgent::Periodic::Periodic (ManagementAgent& _agent, uint32_t _seconds) void ManagementAgent::Periodic::fire () { - agent.timer.add (TimerTask::shared_ptr (new Periodic (agent, agent.interval))); + agent.timer.add (intrusive_ptr<TimerTask> (new Periodic (agent, agent.interval))); agent.PeriodicProcessing (); } diff --git a/cpp/src/tests/TimerTest.cpp b/cpp/src/tests/TimerTest.cpp index 3f2a1c57ec..864ddaf702 100644 --- a/cpp/src/tests/TimerTest.cpp +++ b/cpp/src/tests/TimerTest.cpp @@ -30,6 +30,7 @@ using namespace qpid::broker; using namespace qpid::sys; +using qpid::intrusive_ptr; using boost::dynamic_pointer_cast; class TimerTest : public CppUnit::TestCase @@ -104,10 +105,10 @@ public: { Counter counter; Timer timer; - TestTask::shared_ptr task1(new TestTask(Duration(3 * TIME_SEC), counter)); - TestTask::shared_ptr task2(new TestTask(Duration(1 * TIME_SEC), counter)); - TestTask::shared_ptr task3(new TestTask(Duration(4 * TIME_SEC), counter)); - TestTask::shared_ptr task4(new TestTask(Duration(2 * TIME_SEC), counter)); + intrusive_ptr<TestTask> task1(new TestTask(Duration(3 * TIME_SEC), counter)); + intrusive_ptr<TestTask> task2(new TestTask(Duration(1 * TIME_SEC), counter)); + intrusive_ptr<TestTask> task3(new TestTask(Duration(4 * TIME_SEC), counter)); + intrusive_ptr<TestTask> task4(new TestTask(Duration(2 * TIME_SEC), counter)); timer.add(task1); timer.add(task2); |