access ffrt

Signed-off-by: c00382521 <chenyouan@huawei.com>
Change-Id: Ib14b69db38ab3d2883c0a8c56ed9de0b5e73dffc
This commit is contained in:
c00382521 2024-06-03 22:55:12 +08:00
parent dd23c5591b
commit 2f1398cf4c
12 changed files with 184 additions and 46 deletions

View File

@ -24,8 +24,7 @@ namespace FileManagement {
namespace CloudSync {
using namespace std;
const string TASK_ID = "unload";
const int32_t DELAY_TIME = 180000;
const int32_t DELAY_TIME = 90000; // ms
TaskStateManager &TaskStateManager::GetInstance()
{
@ -33,20 +32,13 @@ TaskStateManager &TaskStateManager::GetInstance()
return instance;
}
TaskStateManager::TaskStateManager()
TaskStateManager::TaskStateManager() : queue_("unloadTask")
{
auto runner = AppExecFwk::EventRunner::Create(TASK_ID);
if (unloadHandler_ == nullptr) {
unloadHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
}
if (unloadHandler_ == nullptr) {
LOGE("init unload handler failed");
}
}
void TaskStateManager::StartTask(string bundleName, TaskType task)
{
unloadHandler_->RemoveTask(TASK_ID);
CancelUnloadTask();
std::lock_guard<std::mutex> lock(taskMapsMutex_);
auto iterator = taskMaps_.find(bundleName);
if (iterator == taskMaps_.end()) {
@ -94,11 +86,27 @@ bool TaskStateManager::HasTask(const string bundleName, TaskType task)
return false;
}
void TaskStateManager::CancelUnloadTask()
{
std::lock_guard<ffrt::mutex> lock(unloadTaskMutex_);
if (unloadTaskHandle_ == nullptr) {
return;
}
LOGD("cancel unload task");
queue_.cancel(unloadTaskHandle_);
unloadTaskHandle_ = nullptr;
}
void TaskStateManager::DelayUnloadTask()
{
LOGI("delay unload task begin");
auto task = [this]() {
LOGI("do unload task");
{
std::lock_guard<ffrt::mutex> lock(unloadTaskMutex_);
unloadTaskHandle_ = nullptr;
}
auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgrProxy == nullptr) {
LOGE("get samgr failed");
@ -110,8 +118,12 @@ void TaskStateManager::DelayUnloadTask()
return;
}
};
unloadHandler_->RemoveTask(TASK_ID);
unloadHandler_->PostTask(task, TASK_ID, DELAY_TIME);
CancelUnloadTask();
std::lock_guard<ffrt::mutex> lock(unloadTaskMutex_);
std::chrono::milliseconds ms(DELAY_TIME);
auto us = std::chrono::duration_cast<std::chrono::microseconds>(ms);
unloadTaskHandle_ = queue_.submit_h(task, ffrt::task_attr().delay(us.count()));
}
} // namespace CloudSync
} // namespace FileManagement

View File

@ -78,6 +78,7 @@ ohos_shared_library("cloudfile_kit") {
"common_event_service:cesfwk_innerkits",
"data_share:datashare_consumer",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"init:libbegetutil",
"ipc:ipc_single",

View File

@ -17,7 +17,7 @@
#define OHOS_FILEMGMT_TASK_STATE_MANAGER_H
#include <nocopyable.h>
#include "event_handler.h"
#include "ffrt_inner.h"
namespace OHOS::FileManagement::CloudSync {
enum class TaskType : uint64_t {
@ -40,10 +40,14 @@ public:
void StartTask();
private:
TaskStateManager();
void DelayUnloadTask();
void CancelUnloadTask();
std::mutex taskMapsMutex_;
std::unordered_map<std::string, uint64_t> taskMaps_;
std::shared_ptr<AppExecFwk::EventHandler> unloadHandler_;
void DelayUnloadTask();
ffrt::queue queue_;
ffrt::task_handle unloadTaskHandle_;
ffrt::mutex unloadTaskMutex_;
};
}
#endif // OHOS_FILEMGMT_TASK_STATE_MANAGER_H

View File

@ -21,8 +21,7 @@
#include "clouddisk_notify_const.h"
#include "dataobs_mgr_client.h"
#include "timer.h"
#include "nocopyable.h"
namespace OHOS::FileManagement::CloudDisk {
using namespace std;
@ -34,7 +33,7 @@ struct CacheNotifyInfo {
mutable list<bool> isDirList;
};
class CloudDiskNotify final {
class CloudDiskNotify final : public NoCopyable {
public:
static CloudDiskNotify &GetInstance();
void TryNotify(const NotifyParamDisk &paramDisk, const ParamDiskOthers &paramOthers = {});
@ -46,14 +45,10 @@ public:
void NotifyChangeOuter();
private:
CloudDiskNotify();
~CloudDiskNotify();
CloudDiskNotify() = default;
~CloudDiskNotify() = default;
list<CacheNotifyInfo> nfList_;
mutex mutex_;
uint32_t timerId_;
unique_ptr<Utils::Timer> timer_;
Utils::Timer::TimerCallback timerCallback_;
uint32_t notifyCount_;
};
} // namespace OHOS::FileManagement::CloudDisk

View File

@ -37,23 +37,6 @@ CloudDiskNotify &CloudDiskNotify::GetInstance()
return instance_;
}
CloudDiskNotify::CloudDiskNotify()
{
if (timer_ == nullptr) {
timer_ = make_unique<Utils::Timer>("CloudDisk_Notify");
}
notifyCount_ = 0;
timerCallback_ = [this]() { NotifyChangeOuter(); };
timer_->Setup();
timerId_ = timer_->Register(timerCallback_, MNOTIFY_TIME_INTERVAL, true);
}
CloudDiskNotify::~CloudDiskNotify()
{
timer_->Unregister(timerId_);
timer_->Shutdown();
}
static int32_t GetDataInner(const NotifyParamDisk &paramDisk, NotifyData &notifyData)
{
int32_t ret;

View File

@ -93,6 +93,7 @@ ohos_shared_library("cloudsync_sa") {
"app_file_service:sandbox_helper_native",
"common_event_service:cesfwk_innerkits",
"dsoftbus:softbus_client",
"ffrt:libffrt",
"hilog:libhilog",
"hitrace:hitrace_meter",
"init:libbegetutil",
@ -239,6 +240,7 @@ ohos_static_library("cloudsync_sa_static") {
"common_event_service:cesfwk_innerkits",
"dsoftbus:softbus_client",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hitrace:hitrace_meter",
"init:libbegetutil",

View File

@ -37,10 +37,6 @@ CloudDiskNotify &CloudDiskNotify::GetInstance()
return instance_;
}
CloudDiskNotify::CloudDiskNotify() {}
CloudDiskNotify::~CloudDiskNotify() {}
void CloudDiskNotify::TryNotify(const NotifyParamDisk &paramDisk, const ParamDiskOthers &paramOthers) {}
void CloudDiskNotify::TryNotifyService(const NotifyParamService &paramService, const ParamServiceOther &paramOthers) {}

View File

@ -91,6 +91,7 @@ ohos_unittest("cloud_disk_data_convertor_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -197,6 +198,7 @@ ohos_unittest("album_data_handler_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -303,6 +305,7 @@ ohos_unittest("data_sync_manager_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -407,6 +410,7 @@ ohos_unittest("data_handler_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -537,6 +541,7 @@ ohos_unittest("file_data_handler_test") {
external_deps = [
"ability_base:zuri",
"ability_runtime:dataobs_manager",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -626,6 +631,7 @@ ohos_unittest("gallery_data_syncer_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -736,6 +742,7 @@ ohos_unittest("data_syncer_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -830,6 +837,7 @@ ohos_unittest("file_data_convertor_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -932,6 +940,7 @@ ohos_unittest("sdk_helper_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -1036,6 +1045,7 @@ ohos_unittest("sdk_helper_unnormal_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -1177,6 +1187,7 @@ ohos_unittest("task_test") {
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -1434,6 +1445,7 @@ ohos_unittest("cloud_disk_data_handler_test") {
external_deps = [
"ability_runtime:dataobs_manager",
"app_file_service:sandbox_helper_native",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",

View File

@ -42,6 +42,7 @@ ohos_unittest("cloud_sync_service_stub_test") {
"ability_base:zuri",
"ability_runtime:dataobs_manager",
"dfs_service:cloudsync_kit_inner",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"ipc:ipc_single",
@ -168,6 +169,7 @@ ohos_unittest("cloud_sync_service_test") {
"dfs_service:cloudsync_kit_inner",
"dsoftbus:softbus_client",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
@ -333,6 +335,7 @@ ohos_unittest("cloud_download_callback_manager_test") {
"ability_runtime:dataobs_manager",
"dfs_service:cloudsync_kit_inner",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
"init:libbegetutil",

View File

@ -68,6 +68,7 @@ ohos_shared_library("libdistributedfileutils") {
sources = [
"cloud_disk/src/cloud_file_utils.cpp",
"dfx/src/xcollie_helper.cpp",
"ffrt/src/ffrt_timer.cpp",
"load/src/plugin_loader.cpp",
"log/src/dfs_error.cpp",
"log/src/utils_log.cpp",
@ -104,6 +105,7 @@ ohos_shared_library("libdistributedfileutils") {
"access_token:libtokenid_sdk",
"c_utils:utils",
"c_utils:utilsbase",
"ffrt:libffrt",
"file_api:filemgmt_libhilog",
"file_api:filemgmt_libn",
"hilog:libhilog",
@ -200,6 +202,7 @@ ohos_shared_library("libdistributedfileutils_lite") {
"dentry/src/meta_file.cpp",
"dentry/src/meta_file_clouddisk.cpp",
"dfx/src/xcollie_helper.cpp",
"ffrt/src/ffrt_timer.cpp",
"load/src/plugin_loader.cpp",
"log/src/dfs_error.cpp",
"log/src/utils_log.cpp",
@ -220,6 +223,7 @@ ohos_shared_library("libdistributedfileutils_lite") {
external_deps = [
"c_utils:utils",
"c_utils:utilsbase",
"ffrt:libffrt",
"hilog:libhilog",
"hisysevent:libhisysevent",
]

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ffrt_timer.h"
#include "cloud_file_log.h"
namespace OHOS::FileManagement::CloudFile {
static constexpr uint32_t TIMER_MAX_INTERVAL_MS = 200;
using namespace std;
FfrtTimer::FfrtTimer(const std::string &name) : name_(name) {}
FfrtTimer ::~FfrtTimer()
{
Stop();
}
void FfrtTimer::Start(const TimerCallback &callback, uint32_t interval, uint32_t repatTimes)
{
unique_lock<ffrt::mutex> lock(taskMutex_);
if (running_ == true) {
LOGE("timer may be is running, timerName:%{public}s", name_.c_str());
return;
}
LOGD("start timer, timerName:%{public}s", name_.c_str());
running_ = true;
auto task = [this, interval, callback, repatTimes]() {
LOGD("task entering loop");
uint32_t times = repatTimes;
while (times > 0) {
callback();
times--;
unique_lock<ffrt::mutex> lock(sleepMutex_);
bool stop =
sleepCv_.wait_for(lock, std::chrono::milliseconds(interval), [this]() { return !this->running_; });
if (stop) { // is stopped
break;
}
}
LOGD("task leaving loop");
};
isJoinable_ = std::make_unique<bool>(true);
ffrt::submit(task, {}, {isJoinable_.get()}, ffrt::task_attr().name(name_.c_str()));
}
void FfrtTimer::Stop()
{
unique_lock<ffrt::mutex> lock(taskMutex_);
if (running_ == false) {
LOGE("timer may be is stopped, timerName:%{public}s", name_.c_str());
return;
}
LOGD("stop timer, timerName:%{public}s", name_.c_str());
{
std::unique_lock<ffrt::mutex> lock(sleepMutex_);
running_ = false;
sleepCv_.notify_one();
}
if (isJoinable_.get() && *isJoinable_) {
ffrt::wait({isJoinable_.get()});
*isJoinable_ = false;
}
LOGD("timer is stoped, timerName:%{public}s", name_.c_str());
}
} // namespace OHOS::FileManagement::CloudFile

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_CLOUD_FILE_FFRT_TIMER_H
#define OHOS_CLOUD_FILE_FFRT_TIMER_H
#include <atomic>
#include <functional>
#include <string>
#include "ffrt_inner.h"
namespace OHOS::FileManagement::CloudFile {
using TimerCallback = std::function<void()>;
class FfrtTimer {
public:
explicit FfrtTimer(const std::string &name);
~FfrtTimer();
void Start(const TimerCallback &callback, uint32_t interval, uint32_t repatTimes = UINT32_MAX);
void Stop();
private:
std::string name_;
ffrt::mutex taskMutex_;
std::atomic_bool running_{false};
ffrt::mutex sleepMutex_;
ffrt::condition_variable sleepCv_;
std::unique_ptr<bool> isJoinable_{nullptr};
};
} // namespace OHOS::FileManagement::CloudFile
#endif // OHOS_CLOUD_FILE_FFRT_TIMER_H