You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by br...@apache.org on 2014/03/27 19:43:07 UTC
[2/6] git commit: TS-2652: Providing a true cancel() in AsyncTimer
TS-2652: Providing a true cancel() in AsyncTimer
Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/521b6e11
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/521b6e11
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/521b6e11
Branch: refs/heads/master
Commit: 521b6e11d4357ccaae348c4714b1084b39a199d0
Parents: af12e77
Author: Manjesh Nilange <ma...@yahoo.com>
Authored: Wed Mar 26 11:22:23 2014 -0700
Committer: Manjesh Nilange <ma...@yahoo.com>
Committed: Wed Mar 26 11:22:23 2014 -0700
----------------------------------------------------------------------
lib/atscppapi/examples/async_timer/AsyncTimer.cc | 17 ++++++++++++++---
lib/atscppapi/src/AsyncTimer.cc | 8 ++++++--
lib/atscppapi/src/include/atscppapi/AsyncTimer.h | 2 ++
3 files changed, 22 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/521b6e11/lib/atscppapi/examples/async_timer/AsyncTimer.cc
----------------------------------------------------------------------
diff --git a/lib/atscppapi/examples/async_timer/AsyncTimer.cc b/lib/atscppapi/examples/async_timer/AsyncTimer.cc
index 738a536..c802462 100644
--- a/lib/atscppapi/examples/async_timer/AsyncTimer.cc
+++ b/lib/atscppapi/examples/async_timer/AsyncTimer.cc
@@ -28,8 +28,9 @@ using std::string;
class TimerEventReceiver : public AsyncReceiver<AsyncTimer> {
public:
- TimerEventReceiver(AsyncTimer::Type type, int period_in_ms, int initial_period_in_ms = 0, int max_instances = 0)
- : max_instances_(max_instances), instance_count_(0), type_(type) {
+ TimerEventReceiver(AsyncTimer::Type type, int period_in_ms, int initial_period_in_ms = 0, int max_instances = 0,
+ bool cancel = false)
+ : max_instances_(max_instances), instance_count_(0), type_(type), cancel_(cancel) {
timer_ = new AsyncTimer(type, period_in_ms, initial_period_in_ms);
Async::execute<AsyncTimer>(this, timer_, shared_ptr<Mutex>()); // letting the system create the mutex
}
@@ -38,7 +39,7 @@ public:
TS_DEBUG(TAG, "Got timer event in object %p!", this);
if ((type_ == AsyncTimer::TYPE_ONE_OFF) || (max_instances_ && (++instance_count_ == max_instances_))) {
TS_DEBUG(TAG, "Stopping timer in object %p!", this);
- delete this;
+ cancel_ ? timer_->cancel() : delete this;
}
}
@@ -51,6 +52,7 @@ private:
int instance_count_;
AsyncTimer::Type type_;
AsyncTimer *timer_;
+ bool cancel_;
};
void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED) {
@@ -58,11 +60,13 @@ void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED
TimerEventReceiver *timer1 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms);
TS_DEBUG(TAG, "Created periodic timer %p with initial period 0, regular period %d and max instances 0", timer1,
period_in_ms);
+
int initial_period_in_ms = 100;
TimerEventReceiver *timer2 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms,
initial_period_in_ms);
TS_DEBUG(TAG, "Created periodic timer %p with initial period %d, regular period %d and max instances 0", timer2,
initial_period_in_ms, period_in_ms);
+
initial_period_in_ms = 200;
int max_instances = 10;
TimerEventReceiver *timer3 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms, initial_period_in_ms,
@@ -72,4 +76,11 @@ void TSPluginInit(int argc ATSCPPAPI_UNUSED, const char *argv[] ATSCPPAPI_UNUSED
TimerEventReceiver *timer4 = new TimerEventReceiver(AsyncTimer::TYPE_ONE_OFF, period_in_ms);
TS_DEBUG(TAG, "Created one-off timer %p with period %d", timer4, period_in_ms);
+
+ initial_period_in_ms = 0;
+ max_instances = 5;
+ TimerEventReceiver *timer5 = new TimerEventReceiver(AsyncTimer::TYPE_PERIODIC, period_in_ms, initial_period_in_ms,
+ max_instances, true /* cancel */);
+ TS_DEBUG(TAG, "Created canceling timer %p with initial period %d, regular period %d and max instances %d", timer5,
+ initial_period_in_ms, period_in_ms, max_instances);
}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/521b6e11/lib/atscppapi/src/AsyncTimer.cc
----------------------------------------------------------------------
diff --git a/lib/atscppapi/src/AsyncTimer.cc b/lib/atscppapi/src/AsyncTimer.cc
index c7548ce..d07b617 100644
--- a/lib/atscppapi/src/AsyncTimer.cc
+++ b/lib/atscppapi/src/AsyncTimer.cc
@@ -90,8 +90,8 @@ void AsyncTimer::run() {
}
}
-AsyncTimer::~AsyncTimer() {
- TSMutexLock(TSContMutexGet(state_->cont_));
+void AsyncTimer::cancel() {
+ TSMutexLock(TSContMutexGet(state_->cont_)); // mutex will be unlocked in destroy
if (state_->initial_timer_action_) {
LOG_DEBUG("Canceling initial timer action");
TSActionCancel(state_->initial_timer_action_);
@@ -102,5 +102,9 @@ AsyncTimer::~AsyncTimer() {
}
LOG_DEBUG("Destroying cont");
TSContDestroy(state_->cont_);
+}
+
+AsyncTimer::~AsyncTimer() {
+ cancel();
delete state_;
}
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/521b6e11/lib/atscppapi/src/include/atscppapi/AsyncTimer.h
----------------------------------------------------------------------
diff --git a/lib/atscppapi/src/include/atscppapi/AsyncTimer.h b/lib/atscppapi/src/include/atscppapi/AsyncTimer.h
index 5e61e1d..5b5f6d5 100644
--- a/lib/atscppapi/src/include/atscppapi/AsyncTimer.h
+++ b/lib/atscppapi/src/include/atscppapi/AsyncTimer.h
@@ -69,6 +69,8 @@ public:
*/
void run();
+ void cancel();
+
private:
AsyncTimerState *state_;
};