summaryrefslogtreecommitdiff
path: root/common/action_processor.cc
diff options
context:
space:
mode:
authorAmin Hassani <ahassani@chromium.org>2018-04-30 14:52:40 -0700
committerchrome-bot <chrome-bot@chromium.org>2018-07-25 00:15:01 -0700
commitd3f4bea600867b4ee85074869d723ba070db0ae7 (patch)
treed325bfcdce617380d61c31582ced486652fd9a7d /common/action_processor.cc
parent94ffe135dd1956d5abf3bdf41503d3d32c15be3b (diff)
update_engine: Pass Action ownership to ActionProcessor
Currently, an object that uses an ActionProcessor for processing one or more actions has to own the Actions. This is problematic, because if we want to create an action on the fly and use an ActionProcessor to perform it, we have to own the Action until it is finished. Furthermore, if someone forget to own the action, there will be memory leaks because ActionProcessor does not delete the Action. This patch passes the ownership of the Actions to the ActionProcessor through unique pointers. If an object wants to have access to the Action, it can get it when ActionComplete() is called. BUG=chromium:807976 TEST=unittests TEST=cros flash TEST=precq Change-Id: I28f7e9fd3425f17cc51b4db4a4abc130a7d6ef8f Reviewed-on: https://chromium-review.googlesource.com/1065113 Commit-Ready: Amin Hassani <ahassani@chromium.org> Tested-by: Amin Hassani <ahassani@chromium.org> Reviewed-by: Xiaochu Liu <xiaochu@chromium.org>
Diffstat (limited to 'common/action_processor.cc')
-rw-r--r--common/action_processor.cc28
1 files changed, 14 insertions, 14 deletions
diff --git a/common/action_processor.cc b/common/action_processor.cc
index 3549e080..ead99c44 100644
--- a/common/action_processor.cc
+++ b/common/action_processor.cc
@@ -17,6 +17,7 @@
#include "update_engine/common/action_processor.h"
#include <string>
+#include <utility>
#include <base/logging.h>
@@ -24,27 +25,30 @@
#include "update_engine/common/error_code_utils.h"
using std::string;
+using std::unique_ptr;
namespace chromeos_update_engine {
ActionProcessor::~ActionProcessor() {
if (IsRunning())
StopProcessing();
- for (auto action : actions_)
- action->SetProcessor(nullptr);
}
-void ActionProcessor::EnqueueAction(AbstractAction* action) {
- actions_.push_back(action);
+void ActionProcessor::EnqueueAction(unique_ptr<AbstractAction> action) {
action->SetProcessor(this);
+ actions_.push_back(std::move(action));
+}
+
+bool ActionProcessor::IsRunning() const {
+ return current_action_ != nullptr || suspended_;
}
void ActionProcessor::StartProcessing() {
CHECK(!IsRunning());
if (!actions_.empty()) {
- current_action_ = actions_.front();
- LOG(INFO) << "ActionProcessor: starting " << current_action_->Type();
+ current_action_ = std::move(actions_.front());
actions_.pop_front();
+ LOG(INFO) << "ActionProcessor: starting " << current_action_->Type();
current_action_->PerformAction();
}
}
@@ -53,16 +57,13 @@ void ActionProcessor::StopProcessing() {
CHECK(IsRunning());
if (current_action_) {
current_action_->TerminateProcessing();
- current_action_->SetProcessor(nullptr);
}
LOG(INFO) << "ActionProcessor: aborted "
<< (current_action_ ? current_action_->Type() : "")
<< (suspended_ ? " while suspended" : "");
- current_action_ = nullptr;
+ current_action_.reset();
suspended_ = false;
// Delete all the actions before calling the delegate.
- for (auto action : actions_)
- action->SetProcessor(nullptr);
actions_.clear();
if (delegate_)
delegate_->ProcessingStopped(this);
@@ -106,13 +107,12 @@ void ActionProcessor::ResumeProcessing() {
void ActionProcessor::ActionComplete(AbstractAction* actionptr,
ErrorCode code) {
- CHECK_EQ(actionptr, current_action_);
+ CHECK_EQ(actionptr, current_action_.get());
if (delegate_)
delegate_->ActionCompleted(this, actionptr, code);
string old_type = current_action_->Type();
current_action_->ActionCompleted(code);
- current_action_->SetProcessor(nullptr);
- current_action_ = nullptr;
+ current_action_.reset();
LOG(INFO) << "ActionProcessor: finished "
<< (actions_.empty() ? "last action " : "") << old_type
<< (suspended_ ? " while suspended" : "")
@@ -138,7 +138,7 @@ void ActionProcessor::StartNextActionOrFinish(ErrorCode code) {
}
return;
}
- current_action_ = actions_.front();
+ current_action_ = std::move(actions_.front());
actions_.pop_front();
LOG(INFO) << "ActionProcessor: starting " << current_action_->Type();
current_action_->PerformAction();