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", "device_info_manager:distributed_device_profile_sdk",
"dsoftbus:softbus_client", "dsoftbus:softbus_client",
"eventhandler:libeventhandler", "eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog", "hilog:libhilog",
"init:libbegetutil", "init:libbegetutil",
"ipc:ipc_single", "ipc:ipc_single",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -19,15 +19,12 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <condition_variable> #include <condition_variable>
#include <ffrt>
#include <functional> #include <functional>
#include <map> #include <map>
#include <mutex> #include <mutex>
#include <queue> #include <unordered_map>
#include <vector>
#include <unordered_set>
#include "event_handler.h" #include "ffrt"
namespace OHOS { namespace OHOS {
namespace DistributedHardware { namespace DistributedHardware {
@ -73,7 +70,7 @@ public:
private: private:
mutable std::mutex timerMutex_; mutable std::mutex timerMutex_;
std::unordered_map<std::string, ffrt::task_handle> timerVec_ = {}; 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() DmTimer::DmTimer()
{ {
LOGI("DmTimer constructor"); LOGI("DmTimer constructor");
if (queue_ != nullptr) {
LOGI("DmTimer already init.");
return;
}
queue_ = std::make_shared<ffrt::queue>(TIMER_TASK);
} }
DmTimer::~DmTimer() DmTimer::~DmTimer()
@ -43,19 +38,19 @@ DmTimer::~DmTimer()
int32_t DmTimer::StartTimer(std::string name, int32_t timeOut, TimerCallback callback) 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) { 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; return ERR_DM_INPUT_PARA_INVALID;
} }
LOGI("DmTimer StartTimer start name: %{public}s", name.c_str()); LOGI("DmTimer StartTimer start name: %{public}s", name.c_str());
std::lock_guard<std::mutex> locker(timerMutex_); std::lock_guard<std::mutex> locker(timerMutex_);
auto taskFunc = [callback, name] () { callback(name); }; auto taskFunc = [callback, name] () { callback(name); };
if (queue_ != nullptr) { ffrt::task_handle handle = queue_.submit_h(taskFunc, ffrt::task_attr().delay(timeOut * MILLISECOND_TO_SECOND));
ffrt::task_handle h = queue_->submit_h(taskFunc, ffrt::task_attr().delay(timeOut * MILLISECOND_TO_SECOND)); if (handle == nullptr) {
if (h != nullptr) { LOGE("handle is nullptr.");
timerVec_.insert(name, h); return ERR_DM_FAILED;
}
} }
timerVec_[name] = std::move(handle);
return DM_OK; return DM_OK;
} }
@ -67,13 +62,16 @@ int32_t DmTimer::DeleteTimer(std::string timerName)
} }
LOGI("DmTimer DeleteTimer start name: %{public}s", timerName.c_str()); LOGI("DmTimer DeleteTimer start name: %{public}s", timerName.c_str());
std::lock_guard<std::mutex> locker(timerMutex_); std::lock_guard<std::mutex> locker(timerMutex_);
if (timerVec_.empty() || timerVec_.find(timerName) == timerVec_.end()) { auto item = timerVec_.find(timerName);
LOGI("DmTimer DeleteTimer is not exist."); if (item == timerVec_.end()) {
LOGI("Invalid task.");
return ERR_DM_FAILED; return ERR_DM_FAILED;
} }
ffrt::task_handle h = timerVec_.find(timerName); if (item->second != nullptr) {
if (queue_ != nullptr && h != nullptr) { int32_t ret = queue_.cancel(item->second);
queue_->skip(h); if (ret != 0) {
LOGE("Cancel failed, errCode: %{public}d.", ret);
}
} }
timerVec_.erase(timerName); timerVec_.erase(timerName);
return DM_OK; return DM_OK;
@ -83,17 +81,16 @@ int32_t DmTimer::DeleteAll()
{ {
LOGI("DmTimer DeleteAll start"); LOGI("DmTimer DeleteAll start");
std::lock_guard<std::mutex> locker(timerMutex_); std::lock_guard<std::mutex> locker(timerMutex_);
if (queue_ == nullptr) {
return ERR_DM_FAILED;
}
if (timerVec_.empty()) { if (timerVec_.empty()) {
LOGI("DmTimer is empty"); LOGI("DmTimer is empty");
return DM_OK; return DM_OK;
} }
for (auto name : timerVec_) { for (const auto &name : timerVec_) {
ffrt::task_handle h = timerVec_.find(name); if (name.second != nullptr) {
if (queue_ != nullptr && h != nullptr) { int32_t ret = queue_.cancel(name.second);
queue_->skip(h); if (ret != 0) {
LOGE("Cancel failed, errCode: %{public}d.", ret);
}
} }
} }
timerVec_.clear(); timerVec_.clear();