You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by as...@apache.org on 2009/01/13 19:58:17 UTC
svn commit: r734216 - in /qpid/trunk/qpid/cpp/src: Makefile.am
qpid/sys/Timer.cpp qpid/sys/Timer.h
Author: astitcher
Date: Tue Jan 13 10:58:03 2009
New Revision: 734216
URL: http://svn.apache.org/viewvc?rev=734216&view=rev
Log:
Created new Timer code in qpid::sys
- Necessary for a timer accessible to the client code
- The interface is slightly different from the existing
Timer code in an attempt to fix some issues with it
Added:
qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp
qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h
Modified:
qpid/trunk/qpid/cpp/src/Makefile.am
Modified: qpid/trunk/qpid/cpp/src/Makefile.am
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/Makefile.am?rev=734216&r1=734215&r2=734216&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/Makefile.am (original)
+++ qpid/trunk/qpid/cpp/src/Makefile.am Tue Jan 13 10:58:03 2009
@@ -352,7 +352,8 @@
qpid/sys/PollableCondition.h \
qpid/sys/PollableQueue.h \
qpid/sys/Runnable.cpp \
- qpid/sys/Shlib.cpp
+ qpid/sys/Shlib.cpp \
+ qpid/sys/Timer.cpp
if HAVE_SASL
libqpidcommon_la_SOURCES += qpid/sys/cyrus/CyrusSecurityLayer.h
@@ -715,6 +716,7 @@
qpid/sys/Waitable.h \
qpid/sys/Thread.h \
qpid/sys/Time.h \
+ qpid/sys/Timer.h \
qpid/sys/TimeoutHandler.h \
qpid/sys/uuid.h
Added: qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp?rev=734216&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp (added)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/Timer.cpp Tue Jan 13 10:58:03 2009
@@ -0,0 +1,136 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+#include "Timer.h"
+#include <iostream>
+#include <numeric>
+
+using boost::intrusive_ptr;
+using std::max;
+
+namespace qpid {
+namespace sys {
+
+TimerTask::TimerTask(Duration timeout) :
+ period(timeout),
+ nextFireTime(AbsTime::now(), timeout),
+ cancelled(false)
+{}
+
+TimerTask::TimerTask(AbsTime time) :
+ period(0),
+ nextFireTime(time),
+ cancelled(false)
+{}
+
+TimerTask::~TimerTask() {}
+
+bool TimerTask::readyToFire() const {
+ return !(nextFireTime > AbsTime::now());
+}
+
+void TimerTask::fireTask() {
+ cancelled = true;
+ fire();
+}
+
+void TimerTask::setupNextFire() {
+ if (period && readyToFire()) {
+ nextFireTime = AbsTime(nextFireTime, period);
+ cancelled = false;
+ }
+}
+
+// Only allow tasks to be delayed
+void TimerTask::restart() { nextFireTime = AbsTime(AbsTime::now(), period); }
+void TimerTask::delayTill(AbsTime time) { period = 0; nextFireTime = max(nextFireTime, time); }
+
+void TimerTask::cancel() { cancelled = true; }
+bool TimerTask::isCancelled() const { return cancelled; }
+
+Timer::Timer() :
+ active(false)
+{
+ start();
+}
+
+Timer::~Timer()
+{
+ stop();
+}
+
+void Timer::run()
+{
+ Monitor::ScopedLock l(monitor);
+ while (active) {
+ if (tasks.empty()) {
+ monitor.wait();
+ } else {
+ intrusive_ptr<TimerTask> t = tasks.top();
+ tasks.pop();
+ if (t->isCancelled()) {
+ } else if(t->readyToFire()) {
+ Monitor::ScopedUnlock u(monitor);
+ t->fireTask();
+ } else {
+ // If the timer was adjusted into the future it might no longer
+ // be the next event, so push and then get top to make sure
+ tasks.push(t);
+ monitor.wait(tasks.top()->nextFireTime);
+ }
+ }
+ }
+}
+
+void Timer::add(intrusive_ptr<TimerTask> task)
+{
+ Monitor::ScopedLock l(monitor);
+ tasks.push(task);
+ monitor.notify();
+}
+
+void Timer::start()
+{
+ Monitor::ScopedLock l(monitor);
+ if (!active) {
+ active = true;
+ runner = Thread(this);
+ }
+}
+
+void Timer::stop()
+{
+ {
+ Monitor::ScopedLock l(monitor);
+ if (!active) return;
+ active = false;
+ monitor.notifyAll();
+ }
+ runner.join();
+}
+
+bool operator<(const intrusive_ptr<TimerTask>& a,
+ const intrusive_ptr<TimerTask>& b)
+{
+ // Lower priority if time is later
+ return a.get() && b.get() && a->nextFireTime > b->nextFireTime;
+}
+
+}}
Added: qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h?rev=734216&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h (added)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/Timer.h Tue Jan 13 10:58:03 2009
@@ -0,0 +1,93 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+#ifndef sys_Timer
+#define sys_Timer
+
+#include "qpid/sys/Monitor.h"
+#include "qpid/sys/Thread.h"
+#include "qpid/sys/Runnable.h"
+#include "qpid/RefCounted.h"
+
+#include <memory>
+#include <queue>
+
+#include <boost/intrusive_ptr.hpp>
+
+namespace qpid {
+namespace sys {
+
+class Timer;
+
+class TimerTask : public RefCounted {
+ friend class Timer;
+ friend bool operator<(const boost::intrusive_ptr<TimerTask>&,
+ const boost::intrusive_ptr<TimerTask>&);
+
+ Duration period;
+ AbsTime nextFireTime;
+ volatile bool cancelled;
+
+ bool readyToFire() const;
+ void fireTask();
+
+public:
+ TimerTask(Duration period);
+ TimerTask(AbsTime fireTime);
+ virtual ~TimerTask();
+
+ void setupNextFire();
+ void restart();
+ void delayTill(AbsTime fireTime);
+ void cancel();
+ bool isCancelled() const;
+
+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);
+
+class Timer : private Runnable {
+ qpid::sys::Monitor monitor;
+ std::priority_queue<boost::intrusive_ptr<TimerTask> > tasks;
+ qpid::sys::Thread runner;
+ bool active;
+
+ // Runnable interface
+ void run();
+
+public:
+ Timer();
+ ~Timer();
+
+ void add(boost::intrusive_ptr<TimerTask> task);
+ void start();
+ void stop();
+};
+
+
+}}
+
+
+#endif