From fd9b67fe1e8116084cb07b5c0ce151fc89461590 Mon Sep 17 00:00:00 2001 From: wangyikai Date: Thu, 7 Aug 2025 21:31:17 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90hdf=5Fcore=E3=80=91libhdi=E7=9A=84ffrt?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=8E=9F=E8=AF=AD=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyikai --- adapter/uhdf2/hdi/BUILD.gn | 13 ++++++++++-- adapter/uhdf2/hdi/src/hdi_support.cpp | 8 +++---- adapter/uhdf2/hdi/src/idevmgr_client.cpp | 16 +++++++------- adapter/uhdf2/hdi/src/iservmgr_client.cpp | 16 +++++++------- adapter/uhdf2/hdi/src/stub_collector.cpp | 19 +++++++++-------- adapter/uhdf2/uhdf.gni | 5 +++++ bundle.json | 3 ++- interfaces/inner_api/hdi/base/hdi_mutex.h | 26 +++++++++++++++++++++++ 8 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 interfaces/inner_api/hdi/base/hdi_mutex.h diff --git a/adapter/uhdf2/hdi/BUILD.gn b/adapter/uhdf2/hdi/BUILD.gn index 43786376b..11bef242f 100644 --- a/adapter/uhdf2/hdi/BUILD.gn +++ b/adapter/uhdf2/hdi/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2021-2025 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 @@ -61,8 +61,10 @@ if (defined(ohos_lite)) { } else { ohos_shared_library("libhdi") { branch_protector_ret = "pac_ret" + + defines = [ ] if (target_cpu == "arm64" || target_cpu == "x86_64") { - defines = [ "__ARCH64__" ] + defines += [ "__ARCH64__" ] } include_dirs = [ @@ -121,6 +123,13 @@ if (defined(ohos_lite)) { ] } + if (with_ffrt) { + external_deps += [ + "ffrt:libffrt" + ] + defines += [ "__FFRT__" ] + } + innerapi_tags = [ "chipsetsdk_sp", "platformsdk_indirect", diff --git a/adapter/uhdf2/hdi/src/hdi_support.cpp b/adapter/uhdf2/hdi/src/hdi_support.cpp index febe1f619..4e570238e 100644 --- a/adapter/uhdf2/hdi/src/hdi_support.cpp +++ b/adapter/uhdf2/hdi/src/hdi_support.cpp @@ -23,7 +23,7 @@ #include #include #include - +#include "hdi_mutex.h" #include "hdf_base.h" #include "hdf_core_log.h" @@ -93,7 +93,7 @@ struct HdiImpl { }; static std::map g_hdiConstructorMap; -static std::mutex g_loaderMutex; +static mutex_t g_loaderMutex; static int32_t ParseInterface( const std::string &desc, std::string &interface, std::string &libName, const char *serviceName) @@ -152,7 +152,7 @@ void *LoadHdiImpl(const char *desc, const char *serviceName) return nullptr; } - std::lock_guard lock(g_loaderMutex); + std::lock_guard lock(g_loaderMutex); auto constructor = g_hdiConstructorMap.find(libName); if (constructor != g_hdiConstructorMap.end()) { return constructor->second.constructor(); @@ -200,7 +200,7 @@ void UnloadHdiImpl(const char *desc, const char *serviceName, void *impl) HDF_LOGE("%{public}s: failed to parse hdi interface info from '%{public}s'", __func__, desc); return; } - std::lock_guard lock(g_loaderMutex); + std::lock_guard lock(g_loaderMutex); auto constructor = g_hdiConstructorMap.find(libName); if (constructor != g_hdiConstructorMap.end() && constructor->second.destructor != nullptr) { constructor->second.destructor(impl); diff --git a/adapter/uhdf2/hdi/src/idevmgr_client.cpp b/adapter/uhdf2/hdi/src/idevmgr_client.cpp index 8fe1caf98..c631ab981 100644 --- a/adapter/uhdf2/hdi/src/idevmgr_client.cpp +++ b/adapter/uhdf2/hdi/src/idevmgr_client.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 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 @@ -20,7 +20,7 @@ #include #include #include - +#include "hdi_mutex.h" #include "idevmgr_hdi.h" #include "iservmgr_hdi.h" @@ -32,7 +32,7 @@ namespace DeviceManager { namespace V1_0 { #define HDF_MAX_HOST_COUNT 0xFF -std::mutex g_remoteMutex; +mutex_t g_remoteMutex; enum DevmgrCmdId : uint32_t { DEVMGR_SERVICE_ATTACH_DEVICE_HOST = 1, @@ -71,7 +71,7 @@ int32_t DeviceManagerProxy::LoadDevice(const std::string &serviceName) return HDF_FAILURE; } - std::unique_lock lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); if (Remote() == nullptr) { HDF_LOGE("invalid param Remote()"); return HDF_ERR_INVALID_PARAM; @@ -97,7 +97,7 @@ int32_t DeviceManagerProxy::UnloadDevice(const std::string &serviceName) return HDF_FAILURE; } - std::unique_lock lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); if (Remote() == nullptr) { HDF_LOGE("invalid param Remote()"); return HDF_ERR_INVALID_PARAM; @@ -168,7 +168,7 @@ int32_t DeviceManagerProxy::ListAllDevice(std::vector &deviceInf } MessageOption option; - std::unique_lock lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); if (Remote() == nullptr) { HDF_LOGE("invalid param Remote()"); return HDF_ERR_INVALID_PARAM; @@ -221,7 +221,7 @@ int32_t DeviceManagerProxy::ListAllHost(std::vector &pidList) } MessageOption option; - std::unique_lock lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); if (Remote() == nullptr) { HDF_LOGE("invalid param Remote()"); return HDF_ERR_INVALID_PARAM; @@ -250,7 +250,7 @@ sptr IDeviceManager::Get() return nullptr; } - std::unique_lock lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); sptr remote = servmgr->GetService("hdf_device_manager"); if (remote != nullptr) { return hdi_facecast(remote); diff --git a/adapter/uhdf2/hdi/src/iservmgr_client.cpp b/adapter/uhdf2/hdi/src/iservmgr_client.cpp index e8b3fdb95..e85553060 100644 --- a/adapter/uhdf2/hdi/src/iservmgr_client.cpp +++ b/adapter/uhdf2/hdi/src/iservmgr_client.cpp @@ -22,7 +22,7 @@ #include #include #include - +#include "hdi_mutex.h" #include "hdf_device_manager_interface_code.h" #include "iservmgr_hdi.h" @@ -35,7 +35,7 @@ namespace HDI { namespace ServiceManager { namespace V1_0 { constexpr int DEVICE_SERVICE_MANAGER_SA_ID = 5100; -std::mutex g_remoteMutex; +mutex_t g_remoteMutex; class ServiceManagerProxy : public IProxyBroker { public: @@ -60,7 +60,7 @@ sptr IServiceManager::Get() return nullptr; } - std::unique_lock lock(g_remoteMutex); + std::lock_guard lock(g_remoteMutex); sptr remote = saManager->GetSystemAbility(DEVICE_SERVICE_MANAGER_SA_ID); if (remote != nullptr) { return new ServiceManagerProxy(remote); @@ -82,7 +82,7 @@ int32_t ServiceManagerProxy::RegisterServiceStatusListener( return HDF_FAILURE; } - std::unique_lock lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); if (Remote() == nullptr) { HDF_LOGE("invalid param Remote()"); return HDF_ERR_INVALID_PARAM; @@ -106,7 +106,7 @@ int32_t ServiceManagerProxy::UnregisterServiceStatusListener(::OHOS::sptr lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); if (Remote() == nullptr) { HDF_LOGE("invalid param Remote()"); return HDF_ERR_INVALID_PARAM; @@ -135,7 +135,7 @@ sptr ServiceManagerProxy::GetService(const char *serviceName) } MessageOption option; - std::unique_lock lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); if (Remote() == nullptr) { HDF_LOGE("invalid param Remote()"); return nullptr; @@ -176,7 +176,7 @@ int32_t ServiceManagerProxy::ListAllService(std::vector &service } MessageOption option; - std::unique_lock lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); if (Remote() == nullptr) { HDF_LOGE("invalid param Remote()"); return HDF_ERR_INVALID_PARAM; @@ -208,7 +208,7 @@ int32_t ServiceManagerProxy::ListServiceByInterfaceDesc( } MessageOption option; - std::unique_lock lock(g_remoteMutex); + std::unique_lock lock(g_remoteMutex); if (Remote() == nullptr) { HDF_LOGE("invalid param Remote()"); return HDF_ERR_INVALID_PARAM; diff --git a/adapter/uhdf2/hdi/src/stub_collector.cpp b/adapter/uhdf2/hdi/src/stub_collector.cpp index cee5af8e9..e51f0053b 100644 --- a/adapter/uhdf2/hdi/src/stub_collector.cpp +++ b/adapter/uhdf2/hdi/src/stub_collector.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -17,11 +17,12 @@ #include #include #include +#include "hdi_mutex.h" static std::map g_constructorMap; static std::map g_stubMap; -static std::mutex g_consMapLock; -static std::mutex g_stubMapLock; +static mutex_t g_consMapLock; +static mutex_t g_stubMapLock; void StubConstructorRegister(const char *ifDesc, struct StubConstructor *constructor) { @@ -29,7 +30,7 @@ void StubConstructorRegister(const char *ifDesc, struct StubConstructor *constru return; } - const std::lock_guard lock(g_consMapLock); + const std::lock_guard lock(g_consMapLock); if (g_constructorMap.find(ifDesc) != g_constructorMap.end()) { HDF_LOGE("repeat registration stub constructor for if %{public}s", ifDesc); return; @@ -44,7 +45,7 @@ void StubConstructorUnregister(const char *ifDesc, const struct StubConstructor return; } - const std::lock_guard lock(g_consMapLock); + const std::lock_guard lock(g_consMapLock); g_constructorMap.erase(ifDesc); return; } @@ -54,14 +55,14 @@ struct HdfRemoteService **StubCollectorGetOrNewObject(const char *ifDesc, void * if (ifDesc == nullptr || servPtr == nullptr) { return nullptr; } - const std::lock_guard stublock(g_stubMapLock); + const std::lock_guard stublock(g_stubMapLock); auto stub = g_stubMap.find(servPtr); if (stub != g_stubMap.end()) { return stub->second; } HDF_LOGI("g_constructorMap size %{public}zu", g_constructorMap.size()); - const std::lock_guard lock(g_consMapLock); + const std::lock_guard lock(g_consMapLock); for (auto &consruct : g_constructorMap) { HDF_LOGI("g_constructorMap it: %{public}s", consruct.first.c_str()); } @@ -92,12 +93,12 @@ void StubCollectorRemoveObject(const char *ifDesc, void *servPtr) return; } - const std::lock_guard stublock(g_stubMapLock); + const std::lock_guard stublock(g_stubMapLock); auto stub = g_stubMap.find(servPtr); if (stub == g_stubMap.end()) { return; } - const std::lock_guard lock(g_consMapLock); + const std::lock_guard lock(g_consMapLock); auto constructor = g_constructorMap.find(ifDesc); if (constructor == g_constructorMap.end()) { HDF_LOGE("no stub constructor for %{public}s", ifDesc); diff --git a/adapter/uhdf2/uhdf.gni b/adapter/uhdf2/uhdf.gni index 9de2161f4..5bdbde19e 100644 --- a/adapter/uhdf2/uhdf.gni +++ b/adapter/uhdf2/uhdf.gni @@ -26,4 +26,9 @@ declare_args() { with_sample = false hdf_core_default_peripheral_config = true + + with_ffrt = false + if (defined(global_parts_info) && defined(global_parts_info.resourceschedule_ffrt)) { + with_ffrt = true + } } diff --git a/bundle.json b/bundle.json index 7b2f6d0ce..32835a25e 100644 --- a/bundle.json +++ b/bundle.json @@ -31,7 +31,8 @@ "samgr", "selinux_adapter", "hilog_lite", - "hicollie" + "hicollie", + "ffrt" ], "third_party": [ "bounds_checking_function" diff --git a/interfaces/inner_api/hdi/base/hdi_mutex.h b/interfaces/inner_api/hdi/base/hdi_mutex.h new file mode 100644 index 000000000..3a75b281d --- /dev/null +++ b/interfaces/inner_api/hdi/base/hdi_mutex.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 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 HDI_MUTEX_H +#define HDI_MUTEX_H + +#ifdef __FFRT__ +#include "ffrt.h" +typedef ffrt::mutex mutex_t; +#else +#include +typedef std::mutex mutex_t; +#endif + +#endif /* HDI_MUTEX_H */ -- Gitee