Signed-off-by: 史晓晓 <shixiaoxiao.30044129@huawei.com>
This commit is contained in:
史晓晓 2024-09-12 10:50:31 +08:00
parent 79c64ae6cd
commit 5db0721499
15 changed files with 35 additions and 30 deletions

View File

@ -79,6 +79,7 @@ ohos_shared_library("devicemanagerext_pin_auth") {
"device_info_manager:distributed_device_profile_sdk",
"dsoftbus:softbus_client",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"init:libbegetutil",
"ipc:ipc_single",

View File

@ -74,6 +74,7 @@ ohos_fuzztest("AuthenticateDeviceServiceFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -75,6 +75,7 @@ ohos_fuzztest("AuthenticateDeviceServiceImplFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -53,6 +53,7 @@ ohos_fuzztest("DmAuthManagerFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -57,6 +57,7 @@ ohos_fuzztest("DmDiscoveryManagerFuzzTest") {
external_deps = [
"device_auth:deviceauth_sdk",
"ffrt:libffrt",
"safwk:system_ability_fwk",
]
}

View File

@ -53,6 +53,7 @@ ohos_fuzztest("HichainConnectorFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -53,6 +53,7 @@ ohos_fuzztest("OnDataReceivedFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -52,6 +52,7 @@ ohos_fuzztest("OnFinishFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -53,6 +53,7 @@ ohos_fuzztest("OnRequestFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -72,6 +72,7 @@ ohos_fuzztest("DeviceManagerServiceFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -66,6 +66,7 @@ ohos_fuzztest("GetTrustedDeviceListServiceFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -66,6 +66,7 @@ ohos_fuzztest("ShiftLNNGearDeviceServiceFuzzTest") {
"device_auth:deviceauth_sdk",
"device_info_manager:distributed_device_profile_common",
"device_info_manager:distributed_device_profile_sdk",
"ffrt:libffrt",
"ipc:ipc_single",
"safwk:system_ability_fwk",
]

View File

@ -122,7 +122,6 @@ if (defined(ohos_lite)) {
"bundle_framework:appexecfwk_core",
"cJSON:cjson",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"ipc:ipc_core",

View File

@ -19,15 +19,12 @@
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <ffrt>
#include <functional>
#include <map>
#include <mutex>
#include <queue>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include "event_handler.h"
#include "ffrt"
namespace OHOS {
namespace DistributedHardware {
@ -73,7 +70,7 @@ public:
private:
mutable std::mutex timerMutex_;
std::unordered_map<std::string, ffrt::task_handle> timerVec_ = {};
std::shared_ptr<ffrt::queue> queue_;
ffrt::queue queue_;
};
}
}

View File

@ -27,11 +27,6 @@ constexpr const char* TIMER_TASK = "TimerTask";
DmTimer::DmTimer()
{
LOGI("DmTimer constructor");
if (queue_ != nullptr) {
LOGI("DmTimer already init.");
return;
}
queue_ = std::make_shared<ffrt::queue>(TIMER_TASK);
}
DmTimer::~DmTimer()
@ -43,19 +38,19 @@ DmTimer::~DmTimer()
int32_t DmTimer::StartTimer(std::string name, int32_t timeOut, TimerCallback callback)
{
if (name.empty() || timeOut <= MIN_TIME_OUT || timeOut > MAX_TIME_OUT || callback == nullptr) {
LOGI("DmTimer StartTimer input value invalid");
LOGE("DmTimer StartTimer input value invalid");
return ERR_DM_INPUT_PARA_INVALID;
}
LOGI("DmTimer StartTimer start name: %{public}s", name.c_str());
std::lock_guard<std::mutex> locker(timerMutex_);
auto taskFunc = [callback, name] () { callback(name); };
if (queue_ != nullptr) {
ffrt::task_handle h = queue_->submit_h(taskFunc, ffrt::task_attr().delay(timeOut * MILLISECOND_TO_SECOND));
if (h != nullptr) {
timerVec_.insert(name, h);
}
ffrt::task_handle handle = queue_.submit_h(taskFunc, ffrt::task_attr().delay(timeOut * MILLISECOND_TO_SECOND));
if (handle == nullptr) {
LOGE("handle is nullptr.");
return ERR_DM_FAILED;
}
timerVec_[name] = std::move(handle);
return DM_OK;
}
@ -67,13 +62,16 @@ int32_t DmTimer::DeleteTimer(std::string timerName)
}
LOGI("DmTimer DeleteTimer start name: %{public}s", timerName.c_str());
std::lock_guard<std::mutex> locker(timerMutex_);
if (timerVec_.empty() || timerVec_.find(timerName) == timerVec_.end()) {
LOGI("DmTimer DeleteTimer is not exist.");
auto item = timerVec_.find(timerName);
if (item == timerVec_.end()) {
LOGI("Invalid task.");
return ERR_DM_FAILED;
}
ffrt::task_handle h = timerVec_.find(timerName);
if (queue_ != nullptr && h != nullptr) {
queue_->skip(h);
if (item->second != nullptr) {
int32_t ret = queue_.cancel(item->second);
if (ret != 0) {
LOGE("Cancel failed, errCode: %{public}d.", ret);
}
}
timerVec_.erase(timerName);
return DM_OK;
@ -83,17 +81,16 @@ int32_t DmTimer::DeleteAll()
{
LOGI("DmTimer DeleteAll start");
std::lock_guard<std::mutex> locker(timerMutex_);
if (queue_ == nullptr) {
return ERR_DM_FAILED;
}
if (timerVec_.empty()) {
LOGI("DmTimer is empty");
return DM_OK;
}
for (auto name : timerVec_) {
ffrt::task_handle h = timerVec_.find(name);
if (queue_ != nullptr && h != nullptr) {
queue_->skip(h);
for (const auto &name : timerVec_) {
if (name.second != nullptr) {
int32_t ret = queue_.cancel(name.second);
if (ret != 0) {
LOGE("Cancel failed, errCode: %{public}d.", ret);
}
}
}
timerVec_.clear();