From 0e6f2c3cc59c4ee686f12887606a73d6c511c613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E4=BD=B3=E4=BC=9F?= Date: Wed, 2 Jul 2025 15:27:02 +0800 Subject: [PATCH] =?UTF-8?q?Description:ffrt=E5=8E=9F=E8=AF=AD=E6=95=B4?= =?UTF-8?q?=E6=94=B9=20IssueNo:https://gitee.com/openharmony/startup=5Fini?= =?UTF-8?q?t/issues/ICJG40=20Binary=20Source:No=20Signed-off-by:=20songjia?= =?UTF-8?q?wei9=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bundle.json | 3 +- interfaces/innerkits/BUILD.gn | 4 ++ services/param/watcher/BUILD.gn | 8 ++- .../watcher/agent/watcher_manager_kits.cpp | 44 +++++++++++++++ .../watcher/agent/watcher_manager_kits.h | 14 ++++- .../param/watcher/proxy/watcher_manager.cpp | 56 +++++++++++++++++++ .../param/watcher/proxy/watcher_manager.h | 8 +++ test/fuzztest/BUILD.gn | 30 ++++++++++ test/unittest/BUILD.gn | 3 + 9 files changed, 167 insertions(+), 3 deletions(-) diff --git a/bundle.json b/bundle.json index 4ea02f8e9..e50de7966 100755 --- a/bundle.json +++ b/bundle.json @@ -77,7 +77,8 @@ "hicollie", "drivers_interface_partitionslot", "code_signature", - "runtime_core" + "runtime_core", + "ffrt" ], "third_party": [ "mksh", diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index 68c8fa469..0c2ba61b4 100755 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -398,6 +398,10 @@ if (defined(ohos_lite)) { "ipc:ipc_single", "samgr:samgr_proxy", ] + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + defines += [ "FFRT_ENABLE" ] + } public_configs = [ ":exported_header_files" ] innerapi_tags = [ "chipsetsdk_sp", diff --git a/services/param/watcher/BUILD.gn b/services/param/watcher/BUILD.gn index 36a0a0b38..51eb4cc69 100644 --- a/services/param/watcher/BUILD.gn +++ b/services/param/watcher/BUILD.gn @@ -43,6 +43,8 @@ ohos_shared_library("param_watcher") { "//base/startup/init/services/log:agent_log", ] + defines = [] + branch_protector_ret = "pac_ret" sanitize = { cfi = true @@ -59,10 +61,14 @@ ohos_shared_library("param_watcher") { "safwk:system_ability_fwk", "samgr:samgr_proxy", ] + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + defines += [ "FFRT_ENABLE" ] + } if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] - defines = [ "HICOLLIE_ENABLE" ] + defines += [ "HICOLLIE_ENABLE" ] } shlib_type = "sa" install_images = [ "system" ] diff --git a/services/param/watcher/agent/watcher_manager_kits.cpp b/services/param/watcher/agent/watcher_manager_kits.cpp index cd6783076..f3721bc72 100644 --- a/services/param/watcher/agent/watcher_manager_kits.cpp +++ b/services/param/watcher/agent/watcher_manager_kits.cpp @@ -37,7 +37,11 @@ WatcherManagerKits::~WatcherManagerKits(void) {} void WatcherManagerKits::ResetService(const wptr &remote) { WATCHER_LOGI("Remote is dead, reset service instance"); +#ifdef FFRT_ENABLE + std::lock_guard lock(lock_); +#else std::lock_guard lock(lock_); +#endif if (watcherManager_ != nullptr) { sptr object = watcherManager_->AsObject(); if ((object != nullptr) && (remote == object)) { @@ -60,7 +64,11 @@ void WatcherManagerKits::ResetService(const wptr &remote) sptr WatcherManagerKits::GetService(void) { +#ifdef FFRT_ENABLE + std::lock_guard lock(lock_); +#else std::lock_guard lock(lock_); +#endif if (watcherManager_ != nullptr) { return watcherManager_; } @@ -97,7 +105,11 @@ void WatcherManagerKits::ReAddWatcher(void) return; } watcherManager = GetService(); +#ifdef FFRT_ENABLE + ffrt_usleep(sleepTime); +#else usleep(sleepTime); +#endif count++; } WATCHER_LOGV("ReAddWatcher count %d ", count); @@ -105,7 +117,11 @@ void WatcherManagerKits::ReAddWatcher(void) // add or get remote agent uint32_t remoteWatcherId = GetRemoteWatcher(); WATCHER_CHECK(remoteWatcherId > 0, return, "Failed to get remote agent"); +#ifdef FFRT_ENABLE + std::lock_guard lock(mutex_); +#else std::lock_guard lock(mutex_); +#endif for (auto iter = watchers_.begin(); iter != watchers_.end(); iter++) { WATCHER_LOGI("Add old watcher keyPrefix %s ", iter->first.c_str()); int ret = watcherManager->AddWatcher(iter->first, remoteWatcherId); @@ -115,7 +131,11 @@ void WatcherManagerKits::ReAddWatcher(void) WatcherManagerKits::ParamWatcher *WatcherManagerKits::GetParamWatcher(const std::string &keyPrefix) { +#ifdef FFRT_ENABLE + std::lock_guard lock(mutex_); +#else std::lock_guard lock(mutex_); +#endif auto iter = watchers_.find(keyPrefix); if (iter != watchers_.end()) { return iter->second.get(); @@ -125,7 +145,11 @@ WatcherManagerKits::ParamWatcher *WatcherManagerKits::GetParamWatcher(const std: uint32_t WatcherManagerKits::GetRemoteWatcher(void) { +#ifdef FFRT_ENABLE + std::lock_guard lock(mutex_); +#else std::lock_guard lock(mutex_); +#endif if (remoteWatcher_ != nullptr) { return remoteWatcherId_; } @@ -148,7 +172,11 @@ int32_t WatcherManagerKits::AddWatcher(const std::string &keyPrefix, ParameterCh WATCHER_CHECK(remoteWatcherId > 0, return -1, "Failed to get remote agent"); ParamWatcherKitPtr watcher = nullptr; { +#ifdef FFRT_ENABLE + std::lock_guard lock(mutex_); +#else std::lock_guard lock(mutex_); +#endif // must check WATCHER_CHECK(remoteWatcherId > 0, return -1, "Failed to get remote agent"); if (watchers_.find(keyPrefix) == watchers_.end()) { @@ -189,7 +217,11 @@ int32_t WatcherManagerKits::DelWatcher(const std::string &keyPrefix, ParameterCh int ret = watcherManager->DelWatcher(keyPrefix, remoteWatcherId_); WATCHER_CHECK(ret == 0, return -1, "Failed to delete watcher for %s", keyPrefix.c_str()); { +#ifdef FFRT_ENABLE + std::lock_guard lock(mutex_); +#else std::lock_guard lock(mutex_); +#endif auto it = watchers_.find(keyPrefix); // delete watcher if (it != watchers_.end()) { watchers_.erase(it); @@ -230,7 +262,11 @@ void WatcherManagerKits::ParamWatcher::RemoveParameterListener(uint32_t idx) int WatcherManagerKits::ParamWatcher::AddParameterListener(ParameterChangePtr callback, void *context) { +#ifdef FFRT_ENABLE + std::lock_guard lock(mutex_); +#else std::lock_guard lock(mutex_); +#endif WATCHER_CHECK(callback != nullptr, return -1, "Invalid callback "); WATCHER_LOGV("AddParameterListener %s listenerId_ %d", keyPrefix_.c_str(), listenerId_); for (auto it = parameterChangeListeners.begin(); it != parameterChangeListeners.end(); it++) { @@ -251,7 +287,11 @@ int WatcherManagerKits::ParamWatcher::AddParameterListener(ParameterChangePtr ca int WatcherManagerKits::ParamWatcher::DelParameterListener(ParameterChangePtr callback, void *context) { +#ifdef FFRT_ENABLE + std::lock_guard lock(mutex_); +#else std::lock_guard lock(mutex_); +#endif if (callback == nullptr) { parameterChangeListeners.clear(); return 0; @@ -285,7 +325,11 @@ int32_t WatcherManagerKits::RemoteWatcher::OnParameterChange( void WatcherManagerKits::ParamWatcher::OnParameterChange(const std::string &name, const std::string &value) { +#ifdef FFRT_ENABLE + std::lock_guard lock(mutex_); +#else std::lock_guard lock(mutex_); +#endif WATCHER_LOGV("OnParameterChange name %s value %s", name.c_str(), value.c_str()); uint32_t index = 0; ParameterChangeListener *listener = GetParameterListener(&index); diff --git a/services/param/watcher/agent/watcher_manager_kits.h b/services/param/watcher/agent/watcher_manager_kits.h index 48439f0e4..e13f7b999 100644 --- a/services/param/watcher/agent/watcher_manager_kits.h +++ b/services/param/watcher/agent/watcher_manager_kits.h @@ -20,6 +20,9 @@ #include #include +#ifdef FFRT_ENABLE +#include "ffrt_inner.h" +#endif #include "iwatcher.h" #include "iwatcher_manager.h" #include "singleton.h" @@ -79,7 +82,11 @@ private: ParameterChangeListener *GetParameterListener(uint32_t *idx); void RemoveParameterListener(uint32_t idx); std::string keyPrefix_ {}; +#ifdef FFRT_ENABLE + ffrt::mutex mutex_; +#else std::mutex mutex_; +#endif uint32_t listenerId_ { 0 }; std::map> parameterChangeListeners; }; @@ -112,11 +119,16 @@ private: ParamWatcher *GetParamWatcher(const std::string &keyPrefix); void ResetService(const wptr &remote); sptr GetService(void); +#ifdef FFRT_ENABLE + ffrt::mutex lock_; + ffrt::mutex mutex_; +#else std::mutex lock_; + std::mutex mutex_; +#endif sptr watcherManager_ {}; sptr deathRecipient_ {}; - std::mutex mutex_; uint32_t remoteWatcherId_ = { 0 }; sptr remoteWatcher_ = { nullptr }; std::map watchers_; diff --git a/services/param/watcher/proxy/watcher_manager.cpp b/services/param/watcher/proxy/watcher_manager.cpp index 035a8e3ce..277ea061e 100644 --- a/services/param/watcher/proxy/watcher_manager.cpp +++ b/services/param/watcher/proxy/watcher_manager.cpp @@ -56,7 +56,11 @@ int32_t WatcherManager::AddRemoteWatcher(uint32_t id, uint32_t &watcherId, const } uint32_t remoteWatcherId = 0; { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif // check watcher id int ret = GetRemoteWatcherId(remoteWatcherId); WATCHER_CHECK(ret == 0, return ERR_FAIL, "Failed to get watcher id for %u", id); @@ -75,7 +79,11 @@ int32_t WatcherManager::DelRemoteWatcher(uint32_t remoteWatcherId) { sptr watcher = {0}; { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif RemoteWatcher *remoteWatcher = GetRemoteWatcher(remoteWatcherId); WATCHER_CHECK(remoteWatcher != nullptr, return 0, "Can not find watcher %u", remoteWatcherId); WATCHER_CHECK(remoteWatcher->CheckAgent(GetCallingPid()), return 0, @@ -128,7 +136,11 @@ int32_t WatcherManager::CheckAppWatchPermission(const std::string &keyPrefix) int32_t WatcherManager::AddWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif // get remote watcher and group WATCHER_CHECK((keyPrefix != "*") && (keyPrefix.size() < PARAM_NAME_LEN_MAX), return -1, "Failed to verify keyPrefix."); @@ -166,7 +178,11 @@ int32_t WatcherManager::AddWatcher(const std::string &keyPrefix, uint32_t remote int32_t WatcherManager::DelWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif WATCHER_CHECK(keyPrefix.size() < PARAM_NAME_LEN_MAX, return -1, "Failed to verify keyPrefix."); auto group = GetWatcherGroup(keyPrefix); WATCHER_CHECK(group != nullptr, return 0, "Can not find group %s", keyPrefix.c_str()); @@ -188,7 +204,11 @@ int32_t WatcherManager::DelWatcher(const std::string &keyPrefix, uint32_t remote int32_t WatcherManager::RefreshWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif WATCHER_CHECK(keyPrefix.size() < PARAM_NAME_LEN_MAX, return -1, "Failed to verify keyPrefix."); WATCHER_LOGV("Refresh watcher %s remoteWatcherId: %u", keyPrefix.c_str(), remoteWatcherId); auto remoteWatcher = GetRemoteWatcher(remoteWatcherId); @@ -282,7 +302,11 @@ void WatcherManager::ProcessWatcherMessage(const ParamMessage *msg) { uint32_t offset = 0; if (msg->type == MSG_ADD_WATCHER || msg->type == MSG_DEL_WATCHER) { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif WATCHER_LOGV("ProcessWatcherMessage key %s, type %d", msg->key, msg->type); AddRealWatcherGroup(msg->key, msg->type); return; @@ -294,7 +318,11 @@ void WatcherManager::ProcessWatcherMessage(const ParamMessage *msg) WATCHER_CHECK(valueContent != NULL, return, "Invalid msg "); WATCHER_LOGV("Process watcher message name '%s' group id %u ", msg->key, msg->id.watcherId); { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif WatcherGroupPtr group = GetWatcherGroup(msg->id.watcherId); WATCHER_CHECK(group != NULL, return, "Can not find group for %u %s", msg->id.watcherId, msg->key); if (!FilterParam(msg->key, group->GetKeyPrefix())) { @@ -393,7 +421,11 @@ int WatcherManager::GetServerFd(bool retry) { const int32_t sleepTime = 200; const int32_t maxRetry = 10; +#ifdef FFRT_ENABLE + std::lock_guard lock(mutex_); +#else std::lock_guard lock(mutex_); +#endif if (retry && serverFd_ != INVALID_SOCKET) { close(serverFd_); serverFd_ = INVALID_SOCKET; @@ -412,7 +444,11 @@ int WatcherManager::GetServerFd(bool retry) } close(serverFd_); serverFd_ = INVALID_SOCKET; +#ifdef FFRT_ENABLE + ffrt_usleep(sleepTime); +#else usleep(sleepTime); +#endif retryCount++; if (stop_) { break; @@ -457,7 +493,11 @@ void WatcherManager::StopLoop() void WatcherManager::OnStop() { if (remoteWatchers_ != nullptr) { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif remoteWatchers_->TraversalNodeSafe([this](ParamWatcherListPtr list, WatcherNodePtr node, uint32_t index) { RemoteWatcherPtr remoteWatcher = ConvertTo(node); OnRemoteDied(remoteWatcher); @@ -469,7 +509,11 @@ void WatcherManager::OnStop() void WatcherManager::OnRemoteDied(const wptr &remote) { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif WATCHER_CHECK(remote != nullptr, return, "Invalid remote obj"); auto remoteWatcher = GetRemoteWatcher(remote); WATCHER_CHECK(remoteWatcher != nullptr, return, "Failed to get remote watcher info "); @@ -542,7 +586,11 @@ void WatcherManager::DumpAllGroup(int fd, ParamWatcherProcessor dumpHandle) { // all output uint32_t count = 0; +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif for (auto it = groupMap_.begin(); it != groupMap_.end(); ++it) { auto group = it->second; dprintf(fd, "Watch prefix : %s \n", group->GetKeyPrefix().c_str()); @@ -589,7 +637,11 @@ int WatcherManager::Dump(int fd, const std::vector& args) }; if (params.size() > 1 && params[0] == "-k") { +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif auto group = GetWatcherGroup(params[1]); if (group == NULL) { dprintf(fd, "Prefix %s not found in watcher list\n", params[1].c_str()); @@ -607,7 +659,11 @@ int WatcherManager::Dump(int fd, const std::vector& args) void WatcherManager::Clear(void) { WATCHER_LOGV("Clear"); +#ifdef FFRT_ENABLE + std::lock_guard lock(watcherMutex_); +#else std::lock_guard lock(watcherMutex_); +#endif remoteWatchers_->TraversalNodeSafe([](ParamWatcherListPtr list, WatcherNodePtr node, uint32_t index) { list->RemoveNode(node); auto group = ConvertTo(node); diff --git a/services/param/watcher/proxy/watcher_manager.h b/services/param/watcher/proxy/watcher_manager.h index 93ba13f4f..824f71fe4 100644 --- a/services/param/watcher/proxy/watcher_manager.h +++ b/services/param/watcher/proxy/watcher_manager.h @@ -21,6 +21,9 @@ #include #include +#ifdef FFRT_ENABLE +#include "ffrt_inner.h" +#endif #include "iremote_stub.h" #include "iwatcher.h" #include "list.h" @@ -119,8 +122,13 @@ private: private: std::atomic remoteWatcherId_ { 0 }; std::atomic groupId_ { 0 }; +#ifdef FFRT_ENABLE + ffrt::mutex mutex_; + ffrt::mutex watcherMutex_; +#else std::mutex mutex_; std::mutex watcherMutex_; +#endif int serverFd_ { -1 }; std::thread *pRecvThread_ { nullptr }; std::atomic stop_ { false }; diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 1a5d171b6..905a7322b 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -2994,6 +2994,9 @@ ohos_fuzztest("CheckAppWatchPermissionFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", @@ -3052,6 +3055,9 @@ ohos_fuzztest("OnStopFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", @@ -3205,6 +3211,9 @@ ohos_fuzztest("AddRemoteWatcherFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", @@ -3256,6 +3265,9 @@ ohos_fuzztest("DelRemoteWatcherFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", @@ -3306,6 +3318,9 @@ ohos_fuzztest("AddWatcherFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", @@ -3355,6 +3370,9 @@ ohos_fuzztest("DelWatcherFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", @@ -3404,6 +3422,9 @@ ohos_fuzztest("RefreshWatcherFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", @@ -3834,6 +3855,9 @@ ohos_fuzztest("SendLocalChangeFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", @@ -3884,6 +3908,9 @@ ohos_fuzztest("AddWatcherGroupFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", @@ -3934,6 +3961,9 @@ ohos_fuzztest("DUMPFuzzTest") { if (init_paramwatcher_hicollie_enable) { external_deps += [ "hicollie:libhicollie" ] } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } cflags = [ "-g", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 511921163..073951d48 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -427,6 +427,9 @@ ohos_unittest("init_unittest") { defines += [ "PARAM_FEATURE_GET_DEVICE_SN" ] } } + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + external_deps += [ "ffrt:libffrt" ] + } sources += [ "//base/startup/init/services/modules/trace/init_trace.c", -- Gitee