未验证 提交 02fc38af 编写于 作者: O openharmony_ci 提交者: Gitee

!665 decouple data in ipc_core and ipc_single

Merge pull request !665 from wanghaoxu/master
......@@ -196,7 +196,6 @@ public:
* @since 9
*/
virtual int GetObjectType() const;
#ifndef CONFIG_IPC_SINGLE
/**
* @brief Invoker the calling thread.
......
......@@ -45,7 +45,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE {
#endif
#ifndef CONFIG_IPC_SINGLE
struct SocketThreadLockInfo {
std::mutex mutex;
......@@ -90,27 +89,25 @@ public:
static bool IsHandleMadeByUser(uint32_t handle);
#endif
bool SetMaxWorkThread(int maxThreadNum);
std::u16string MakeHandleDescriptor(int handle);
sptr<IRemoteObject> GetRegistryObject();
bool OnThreadTerminated(const std::string &threadName);
bool SpawnThread(int policy = IPCWorkThread::SPAWN_PASSIVE, int proto = IRemoteObject::IF_PROT_DEFAULT);
std::u16string MakeHandleDescriptor(int handle);
sptr<IRemoteObject> FindOrNewObject(int handle);
bool IsContainsObject(IRemoteObject *object);
sptr<IRemoteObject> QueryObject(const std::u16string &descriptor);
bool AttachObject(IRemoteObject *object);
bool DetachObject(IRemoteObject *object);
bool OnThreadTerminated(const std::string &threadName);
sptr<IRemoteObject> GetRegistryObject();
bool SetRegistryObject(sptr<IRemoteObject> &object);
#ifndef CONFIG_IPC_SINGLE
bool AttachRawData(uint32_t fd, std::shared_ptr<InvokerRawData> rawData);
bool DetachRawData(uint32_t fd);
std::shared_ptr<InvokerRawData> QueryRawData(uint32_t fd);
#ifndef CONFIG_IPC_SINGLE
sptr<IRemoteObject> GetSAMgrObject();
std::shared_ptr<DBinderSessionObject> ProxyDetachDBinderSession(uint32_t handle, IPCObjectProxy *proxy);
bool ProxyAttachDBinderSession(uint32_t handle, std::shared_ptr<DBinderSessionObject> object);
......@@ -127,7 +124,6 @@ public:
std::shared_ptr<ThreadMessageInfo> QueryThreadBySeqNumber(uint64_t seqNumber);
bool AddSendThreadInWait(uint64_t seqNumber, std::shared_ptr<ThreadMessageInfo> messageInfo, int userWaitTime);
std::thread::id GetIdleSocketThread();
int GetSocketIdleThreadNum() const;
int GetSocketTotalThreadNum() const;
int PopSocketIdFromThread(const std::thread::id &threadId);
......@@ -195,10 +191,8 @@ private:
IPCProcessSkeleton();
static IPCProcessSkeleton *instance_;
static std::mutex procMutex_;
std::shared_mutex mutex_;
std::shared_mutex rawDataMutex_;
std::map<std::u16string, wptr<IRemoteObject>> objects_;
std::map<IRemoteObject *, bool> isContainStub_;
std::map<uint32_t, std::shared_ptr<InvokerRawData>> rawData_;
IPCWorkThreadPool *threadPool_ = nullptr;
......
......@@ -16,13 +16,14 @@
#ifndef OHOS_IPC_PROCESS_SKELETON_H
#define OHOS_IPC_PROCESS_SKELETON_H
#include <map>
#include <mutex>
#include <shared_mutex>
#include <refbase.h>
#include "iremote_object.h"
namespace OHOS {
class ProcessSkeleton : public virtual RefBase {
public:
......@@ -32,15 +33,24 @@ public:
void SetSamgrFlag(bool flag);
bool GetSamgrFlag();
bool IsContainsObject(IRemoteObject *object);
sptr<IRemoteObject> QueryObject(const std::u16string &descriptor);
bool AttachObject(IRemoteObject *object, const std::u16string &descriptor);
bool DetachObject(IRemoteObject *object, const std::u16string &descriptor);
private:
DISALLOW_COPY_AND_MOVE(ProcessSkeleton);
ProcessSkeleton() = default;
~ProcessSkeleton() = default;
~ProcessSkeleton();
static sptr<ProcessSkeleton> instance_;
static std::mutex mutex_;
std::shared_mutex objMutex_;
sptr<IRemoteObject> registryObject_ = nullptr;
bool isSamgr_ = false;
std::map<std::u16string, wptr<IRemoteObject>> objects_;
std::map<IRemoteObject *, bool> isContainStub_;
};
} // namespace OHOS
#endif // OHOS_IPC_PROCESS_SKELETON_H
......@@ -49,7 +49,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
using namespace IPC_SINGLE;
#endif
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCObjectProxy" };
IPCObjectProxy::IPCObjectProxy(int handle, std::u16string descriptor, int proto)
......
......@@ -49,7 +49,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
using namespace IPC_SINGLE;
#endif
using namespace OHOS::HiviewDFX;
static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCObjectStub" };
#ifndef CONFIG_IPC_SINGLE
......
......@@ -15,6 +15,7 @@
#include "ipc_process_skeleton.h"
#include <functional>
#include <securec.h>
#include <unistd.h>
#include <random>
......@@ -23,6 +24,7 @@
#include "ipc_debug.h"
#include "ipc_types.h"
#include "ipc_object_proxy.h"
#include "ipc_thread_skeleton.h"
#include "process_skeleton.h"
#include "sys_binder.h"
......@@ -37,7 +39,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE {
#endif
#ifndef CONFIG_IPC_SINGLE
using namespace Communication;
#endif
......@@ -93,8 +94,6 @@ IPCProcessSkeleton::~IPCProcessSkeleton()
delete threadPool_;
threadPool_ = nullptr;
objects_.clear();
isContainStub_.clear();
rawData_.clear();
#ifndef CONFIG_IPC_SINGLE
threadLockInfo_.clear();
......@@ -138,10 +137,11 @@ std::u16string IPCProcessSkeleton::MakeHandleDescriptor(int handle)
sptr<IRemoteObject> IPCProcessSkeleton::FindOrNewObject(int handle)
{
sptr<IRemoteObject> result = nullptr;
std::u16string descriptor = MakeHandleDescriptor(handle);
if (descriptor.length() == 0) {
ZLOGE(LOG_LABEL, "make handle descriptor failed");
return nullptr;
return result;
}
{
result = QueryObject(descriptor);
......@@ -161,13 +161,14 @@ sptr<IRemoteObject> IPCProcessSkeleton::FindOrNewObject(int handle)
}
// OnFirstStrongRef will be called.
result = new (std::nothrow) IPCObjectProxy(handle, descriptor);
if (result == nullptr) {
ZLOGE(LOG_LABEL, "new IPCObjectProxy failed!");
return result;
}
AttachObject(result.GetRefPtr());
}
}
sptr<IPCObjectProxy> proxy = reinterpret_cast<IPCObjectProxy *>(result.GetRefPtr());
// When a new proxy is initializing, other thread will find an existed proxy and need to wait,
// this makes sure proxy has been initialized when ReadRemoteObject return.
proxy->WaitForInit();
#ifndef CONFIG_IPC_SINGLE
if (proxy->GetProto() == IRemoteObject::IF_PROT_ERROR) {
......@@ -178,29 +179,6 @@ sptr<IRemoteObject> IPCProcessSkeleton::FindOrNewObject(int handle)
return result;
}
bool IPCProcessSkeleton::SetMaxWorkThread(int maxThreadNum)
{
if (maxThreadNum <= 0) {
ZLOGE(LOG_LABEL, "Set Invalid thread Number %d", maxThreadNum);
return false;
}
if (threadPool_ == nullptr) {
threadPool_ = new (std::nothrow) IPCWorkThreadPool(maxThreadNum);
if (threadPool_ == nullptr) {
ZLOGE(LOG_LABEL, "create IPCWorkThreadPool object failed");
return false;
}
}
threadPool_->UpdateMaxThreadNum(maxThreadNum);
IRemoteInvoker *invoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DEFAULT);
if (invoker != nullptr) {
return invoker->SetMaxWorkThread(maxThreadNum);
}
return false;
}
bool IPCProcessSkeleton::SetRegistryObject(sptr<IRemoteObject> &object)
{
if (object == nullptr) {
......@@ -226,6 +204,29 @@ bool IPCProcessSkeleton::SetRegistryObject(sptr<IRemoteObject> &object)
return ret;
}
bool IPCProcessSkeleton::SetMaxWorkThread(int maxThreadNum)
{
if (maxThreadNum <= 0) {
ZLOGE(LOG_LABEL, "Set Invalid thread Number %d", maxThreadNum);
return false;
}
if (threadPool_ == nullptr) {
threadPool_ = new (std::nothrow) IPCWorkThreadPool(maxThreadNum);
if (threadPool_ == nullptr) {
ZLOGE(LOG_LABEL, "create IPCWorkThreadPool object failed");
return false;
}
}
threadPool_->UpdateMaxThreadNum(maxThreadNum);
IRemoteInvoker *invoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DEFAULT);
if (invoker != nullptr) {
return invoker->SetMaxWorkThread(maxThreadNum);
}
return false;
}
bool IPCProcessSkeleton::SpawnThread(int policy, int proto)
{
if (threadPool_ != nullptr) {
......@@ -245,72 +246,69 @@ bool IPCProcessSkeleton::OnThreadTerminated(const std::string &threadName)
return true;
}
bool IPCProcessSkeleton::IsContainsObject(IRemoteObject *object)
{
// check whether it is a valid IPCObjectStub object.
std::shared_lock<std::shared_mutex> lockGuard(mutex_);
auto it = isContainStub_.find(object);
if (it != isContainStub_.end()) {
return it->second;
if (object == nullptr) {
ZLOGE(LOG_LABEL, "object is null");
return false;
}
return false;
auto current = ProcessSkeleton::GetInstance();
if (current == nullptr) {
ZLOGE(LOG_LABEL, "get process skeleton failed");
return false;
}
return current->IsContainsObject(object);
}
bool IPCProcessSkeleton::DetachObject(IRemoteObject *object)
{
std::unique_lock<std::shared_mutex> lockGuard(mutex_);
(void)isContainStub_.erase(object);
if (object == nullptr) {
ZLOGE(LOG_LABEL, "object is null");
return false;
}
std::u16string descriptor = object->GetObjectDescriptor();
if (descriptor.empty()) {
return false;
}
// This handle may have already been replaced with a new IPCObjectProxy,
// if someone failed the AttemptIncStrong.
auto iterator = objects_.find(descriptor);
if (iterator->second == object) {
objects_.erase(iterator);
return true;
auto current = ProcessSkeleton::GetInstance();
if (current == nullptr) {
ZLOGE(LOG_LABEL, "get process skeleton failed");
return false;
}
return false;
return current->DetachObject(object, descriptor);
}
bool IPCProcessSkeleton::AttachObject(IRemoteObject *object)
{
std::unique_lock<std::shared_mutex> lockGuard(mutex_);
(void)isContainStub_.insert(std::pair<IRemoteObject *, bool>(object, true));
if (object == nullptr) {
ZLOGE(LOG_LABEL, "object is null");
return false;
}
std::u16string descriptor = object->GetObjectDescriptor();
if (descriptor.empty()) {
return false;
}
// If attemptIncStrong failed, old proxy might still exist, replace it with the new proxy.
wptr<IRemoteObject> wp = object;
auto result = objects_.insert_or_assign(descriptor, wp);
return result.second;
auto current = ProcessSkeleton::GetInstance();
if (current == nullptr) {
ZLOGE(LOG_LABEL, "get process skeleton failed");
return false;
}
return current->AttachObject(object, descriptor);
}
sptr<IRemoteObject> IPCProcessSkeleton::QueryObject(const std::u16string &descriptor)
{
sptr<IRemoteObject> result = nullptr;
if (descriptor.empty()) {
return result;
}
std::shared_lock<std::shared_mutex> lockGuard(mutex_);
IRemoteObject *remoteObject = nullptr;
auto it = objects_.find(descriptor);
if (it != objects_.end()) {
// Life-time of IPCObjectProxy is extended to WEAK
// now it's weak reference counted, so it's safe to get raw pointer
remoteObject = it->second.GetRefPtr();
if (descriptor.length() == 0) {
ZLOGE(LOG_LABEL, "enter descriptor is empty");
return nullptr;
}
if (remoteObject == nullptr || !remoteObject->AttemptIncStrong(this)) {
return result;
auto current = ProcessSkeleton::GetInstance();
if (current == nullptr) {
ZLOGE(LOG_LABEL, "get process skeleton failed");
return nullptr;
}
result = remoteObject;
return result;
return current->QueryObject(descriptor);
}
#ifndef CONFIG_IPC_SINGLE
......@@ -595,12 +593,12 @@ int IPCProcessSkeleton::GetSocketIdleThreadNum() const
return 0;
}
int IPCProcessSkeleton::GetSocketTotalThreadNum() const
{
if (threadPool_ != nullptr) {
return threadPool_->GetSocketTotalThreadNum();
}
return 0;
}
......@@ -1181,7 +1179,6 @@ sptr<IRemoteObject> IPCProcessSkeleton::QueryDBinderCallbackProxy(sptr<IRemoteOb
return nullptr;
}
#endif
#ifdef CONFIG_IPC_SINGLE
} // namespace IPC_SINGLE
......
......@@ -33,7 +33,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
using namespace IPC_SINGLE;
#endif
void IPCSkeleton::JoinWorkThread()
{
IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
......
......@@ -33,7 +33,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE {
#endif
using namespace OHOS::HiviewDFX;
pthread_key_t IPCThreadSkeleton::TLSKey_ = 0;
pthread_once_t IPCThreadSkeleton::TLSKeyOnce_ = PTHREAD_ONCE_INIT;
......@@ -82,8 +81,8 @@ IPCThreadSkeleton::IPCThreadSkeleton()
IPCThreadSkeleton::~IPCThreadSkeleton()
{
ZLOGE(LABEL, "IPCThreadSkeleton delete");
std::lock_guard<std::recursive_mutex> lockGuard(mutex_);
ZLOGE(LABEL, "IPCThreadSkeleton delete");
for (auto it = invokers_.begin(); it != invokers_.end();) {
delete it->second;
it = invokers_.erase(it);
......@@ -97,7 +96,6 @@ IRemoteInvoker *IPCThreadSkeleton::GetRemoteInvoker(int proto)
if (current == nullptr) {
return nullptr;
}
std::lock_guard<std::recursive_mutex> lockGuard(mutex_);
auto it = current->invokers_.find(proto);
if (it != current->invokers_.end()) {
......
......@@ -25,7 +25,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
using namespace IPC_SINGLE;
#endif
bool IRemoteObject::CheckObjectLegality() const
{
return false;
......
......@@ -17,6 +17,7 @@
#include "log_tags.h"
#include "ipc_debug.h"
#include "string_ex.h"
namespace OHOS {
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "ProcessSkeleton" };
......@@ -39,6 +40,13 @@ sptr<ProcessSkeleton> ProcessSkeleton::GetInstance()
return instance_;
}
ProcessSkeleton::~ProcessSkeleton()
{
std::lock_guard<std::shared_mutex> lockGuard(objMutex_);
objects_.clear();
isContainStub_.clear();
}
sptr<IRemoteObject> ProcessSkeleton::GetRegistryObject()
{
std::lock_guard<std::mutex> lockGuard(mutex_);
......@@ -60,4 +68,64 @@ bool ProcessSkeleton::GetSamgrFlag()
{
return isSamgr_;
}
bool ProcessSkeleton::IsContainsObject(IRemoteObject *object)
{
// check whether it is a valid IPCObjectStub object.
std::shared_lock<std::shared_mutex> lockGuard(objMutex_);
auto it = isContainStub_.find(object);
if (it != isContainStub_.end()) {
return it->second;
}
return false;
}
bool ProcessSkeleton::DetachObject(IRemoteObject *object, const std::u16string &descriptor)
{
std::unique_lock<std::shared_mutex> lockGuard(objMutex_);
(void)isContainStub_.erase(object);
// This handle may have already been replaced with a new IPCObjectProxy,
// if someone failed the AttemptIncStrong.
auto iterator = objects_.find(descriptor);
if (iterator != objects_.end()) {
objects_.erase(iterator);
return true;
}
return false;
}
bool ProcessSkeleton::AttachObject(IRemoteObject *object, const std::u16string &descriptor)
{
std::unique_lock<std::shared_mutex> lockGuard(objMutex_);
(void)isContainStub_.insert(std::pair<IRemoteObject *, bool>(object, true));
// If attemptIncStrong failed, old proxy might still exist, replace it with the new proxy.
wptr<IRemoteObject> wp = object;
auto result = objects_.insert_or_assign(descriptor, wp);
return result.second;
}
sptr<IRemoteObject> ProcessSkeleton::QueryObject(const std::u16string &descriptor)
{
sptr<IRemoteObject> result = nullptr;
if (descriptor.empty()) {
return result;
}
std::shared_lock<std::shared_mutex> lockGuard(objMutex_);
IRemoteObject *remoteObject = nullptr;
auto it = objects_.find(descriptor);
if (it != objects_.end()) {
// Life-time of IPCObjectProxy is extended to WEAK
// now it's weak reference counted, so it's safe to get raw pointer
remoteObject = it->second.GetRefPtr();
}
if (remoteObject == nullptr || !remoteObject->AttemptIncStrong(this)) {
return result;
}
result = remoteObject;
return result;
}
} // namespace OHOS
\ No newline at end of file
......@@ -23,7 +23,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE {
#endif
class BinderConnector {
public:
static BinderConnector *GetInstance();
......
......@@ -41,7 +41,6 @@ private:
std::mutex factoryMutex_;
std::unordered_map<int, InvokerCreator> creators_;
};
template <typename T> class InvokerDelegator {
public:
InvokerDelegator(int prot);
......
......@@ -27,7 +27,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE {
#endif
class IRemoteInvoker {
public:
enum {
......
......@@ -37,7 +37,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE {
#endif
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "BinderConnector" };
std::mutex BinderConnector::skeletonMutex;
constexpr int SZ_1_M = 1048576;
......
......@@ -45,6 +45,7 @@ ohos_unittest("IPCNativeUnitTest") {
"ipc_thread_skeleton_unittest.cpp",
"iremote_object_unitest.cpp",
"message_parcel_unittest.cpp",
"process_skeleton_unittest.cpp",
]
configs = [
......@@ -53,6 +54,7 @@ ohos_unittest("IPCNativeUnitTest") {
]
deps = [
"$IPC_CORE_ROOT/src/core:ipc_common",
"$IPC_TEST_ROOT/auxiliary/native:TestAssistance",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
......
......@@ -18,6 +18,7 @@
#define private public
#define protected public
#include "binder_invoker.h"
#include "dbinder_callback_stub.h"
#include "ipc_types.h"
#include "ipc_thread_skeleton.h"
......
......@@ -1693,22 +1693,6 @@ HWTEST_F(IPCProcessSkeletonUnitTest, QueryDBinderCallbackProxyTest002, TestSize.
EXPECT_EQ(ret, nullptr);
}
/**
* @tc.name: IsContainsObjectTest002
* @tc.desc: Verify the IsContainsObject function
* @tc.type: FUNC
*/
HWTEST_F(IPCProcessSkeletonUnitTest, IsContainsObjectTest002, TestSize.Level1)
{
IPCProcessSkeleton *skeleton = IPCProcessSkeleton::GetCurrent();
ASSERT_TRUE(skeleton != nullptr);
sptr<IRemoteObject> object = new IPCObjectStub(u"testObject");
skeleton->isContainStub_.clear();
bool ret = skeleton->IsContainsObject(object.GetRefPtr());
EXPECT_EQ(ret, false);
}
/**
* @tc.name: ProxyQueryDBinderSessionTest002
* @tc.desc: Verify the ProxyQueryDBinderSession function
......
/*
* Copyright (C) 2022 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 <gtest/gtest.h>
#include <gmock/gmock.h>
#define private public
#include "process_skeleton.h"
#include "ipc_types.h"
#include "iremote_object.h"
#include "ipc_object_stub.h"
#undef private
using namespace testing::ext;
using namespace OHOS;
class ProcessSkeletonUnitTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void ProcessSkeletonUnitTest::SetUpTestCase()
{
}
void ProcessSkeletonUnitTest::TearDownTestCase()
{
}
void ProcessSkeletonUnitTest::SetUp() {}
void ProcessSkeletonUnitTest::TearDown() {}
/**
* @tc.name: IsContainsObjectTest001
* @tc.desc: Verify the IsContainsObject function
* @tc.type: FUNC
*/
HWTEST_F(ProcessSkeletonUnitTest, IsContainsObjectTest001, TestSize.Level1)
{
ProcessSkeleton *skeleton = ProcessSkeleton::GetInstance();
ASSERT_TRUE(skeleton != nullptr);
sptr<IRemoteObject> object = new IPCObjectStub(u"testObject");
skeleton->isContainStub_.clear();
bool ret = skeleton->IsContainsObject(object.GetRefPtr());
EXPECT_EQ(ret, false);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册