From c2232759525f48736289ca4b8d6ddb1541d292cf Mon Sep 17 00:00:00 2001 From: njupthan Date: Mon, 18 Apr 2022 14:01:04 +0000 Subject: [PATCH] =?UTF-8?q?shellcommand=20wait=E9=80=BB=E8=BE=91=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: njupthan --- tools/aa/include/ability_command.h | 3 ++- tools/aa/include/shell_command_executor.h | 2 +- tools/aa/src/ability_command.cpp | 6 +++--- tools/aa/src/shell_command_executor.cpp | 20 +++++++++++--------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index 3eb02b681f..bd05498b03 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -133,8 +133,9 @@ const std::string STRING_SCREEN_POWER_OFF_NG = "error: failed to power off scree const std::string STRING_FORCE_STOP_OK = "force stop process successfully."; const std::string STRING_FORCE_STOP_NG = "error: failed to force stop process."; -const std::string STRING_START_USER_TEST_OK = "start user test successfully."; const std::string STRING_START_USER_TEST_NG = "error: failed to start user test."; +const std::string STRING_USER_TEST_STARTED = "user test started."; +const std::string STRING_USER_TEST_FINISHED = "user test finished."; const std::string STRING_BLOCK_ABILITY_OK = "block ability successfully."; diff --git a/tools/aa/include/shell_command_executor.h b/tools/aa/include/shell_command_executor.h index a3d4f74d0b..7327348d5b 100644 --- a/tools/aa/include/shell_command_executor.h +++ b/tools/aa/include/shell_command_executor.h @@ -53,11 +53,11 @@ private: std::string cmd_; int64_t timeoutSec_ {0}; ShellCommandResult cmdResult_; + bool isDone_ {false}; std::shared_ptr handler_; std::condition_variable cvWork_; std::mutex mtxWork_; - std::mutex mtxSyncWork_; std::mutex mtxCopy_; }; } // namespace AAFwk diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 6ddac4f649..8d9c38361f 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -1183,7 +1183,7 @@ ErrCode AbilityManagerShellCommand::StartUserTest(const std::map syncLock(mtxSyncWork_); - if (!DoWork()) { HILOG_INFO("Failed to execute command : \"%{public}s\"", cmd_.data()); return cmdResult_; } std::unique_lock workLock(mtxWork_); - syncLock.unlock(); + auto condition = [this]() { return isDone_; }; if (timeoutSec_ <= 0) { - while (timeoutSec_ <= 0) { - cvWork_.wait(workLock); - } - } else if (cvWork_.wait_for(workLock, timeoutSec_ * 1s) == std::cv_status::timeout) { + cvWork_.wait(workLock, condition); + } else if (!cvWork_.wait_for(workLock, timeoutSec_ * 1s, condition)) { HILOG_WARN("Command execution timed out! cmd : \"%{public}s\", timeoutSec : %{public}" PRId64, cmd_.data(), timeoutSec_); std::cout << "Warning! Command execution timed out! cmd : " << cmd_ << ", timeoutSec : " << timeoutSec_ @@ -86,12 +82,14 @@ bool ShellCommandExecutor::DoWork() handler_->PostTask([this, self]() { HILOG_INFO("DoWork async task begin, cmd : \"%{public}s\"", cmd_.data()); - std::unique_lock syncLock(mtxSyncWork_); - FILE *file = popen(cmd_.c_str(), "r"); if (!file) { HILOG_ERROR("Failed to call popen, cmd : \"%{public}s\"", cmd_.data()); + { + std::unique_lock workLock(mtxWork_); + isDone_ = true; + } cvWork_.notify_one(); HILOG_INFO("DoWork async task end, cmd : \"%{public}s\"", cmd_.data()); return; @@ -109,6 +107,10 @@ bool ShellCommandExecutor::DoWork() cmdResult_.exitCode = pclose(file); file = nullptr; + { + std::unique_lock workLock(mtxWork_); + isDone_ = true; + } cvWork_.notify_one(); HILOG_INFO("DoWork async task end, cmd : \"%{public}s\"", cmd_.data()); });