!850 非foundation进程使用numExecuting_导致死锁

Merge pull request !850 from hhl/master
This commit is contained in:
openharmony_ci 2023-11-09 07:16:26 +00:00 committed by Gitee
commit 8c9d5f052d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 36 additions and 13 deletions

View File

@ -33,6 +33,8 @@
OHOS::InvokerFactory*;
OHOS::IPCDfx*;
OHOS::IPCSkeleton*;
OHOS::IPCProcessSkeleton::LockForNumExecuting;
OHOS::IPCProcessSkeleton::UnlockForNumExecuting;
OHOS::IRemoteObject*;
OHOS::BinderInvoker*;
OHOS::BrokerRegistration*;

View File

@ -24,6 +24,8 @@
OHOS::BrokerRegistration::Unregister*;
OHOS::BrokerRegistration::NewInstance*;
OHOS::IPCSkeleton*;
OHOS::IPC_SINGLE::IPCProcessSkeleton::LockForNumExecuting;
OHOS::IPC_SINGLE::IPCProcessSkeleton::UnlockForNumExecuting;
OHOS::IPC_SINGLE::IPCThreadSkeleton*;
OHOS::IRemoteObject*;
"OHOS::IPC_SINGLE::BinderInvoker::BinderInvoker()";

View File

@ -103,6 +103,8 @@ public:
sptr<IRemoteObject> GetRegistryObject();
bool SetRegistryObject(sptr<IRemoteObject> &object);
void BlockUntilThreadAvailable();
void LockForNumExecuting();
void UnlockForNumExecuting();
#ifndef CONFIG_IPC_SINGLE
bool AttachRawData(uint32_t fd, std::shared_ptr<InvokerRawData> rawData);
@ -188,17 +190,18 @@ public:
static constexpr uint32_t DBINDER_HANDLE_BASE = 100000 * DBINDER_HANDLE_MAGIC;
static constexpr uint32_t DBINDER_HANDLE_COUNT = 100000;
static constexpr uint32_t DBINDER_HANDLE_RANG = 100;
static constexpr int32_t FOUNDATION_UID = 5523;
static constexpr int ENCRYPT_LENGTH = 4;
std::mutex mutex_;
std::condition_variable cv_;
int numExecuting_ = 0;
int numWaitingForThreads_ = 0;
private:
DISALLOW_COPY_AND_MOVE(IPCProcessSkeleton);
IPCProcessSkeleton();
static IPCProcessSkeleton *instance_;
static std::mutex procMutex_;
std::shared_mutex rawDataMutex_;
std::mutex mutex_;
std::condition_variable cv_;
int numExecuting_ = 0;
int numWaitingForThreads_ = 0;
std::map<uint32_t, std::shared_ptr<InvokerRawData>> rawData_;
IPCWorkThreadPool *threadPool_ = nullptr;

View File

@ -321,6 +321,27 @@ void IPCProcessSkeleton::BlockUntilThreadAvailable()
numWaitingForThreads_--;
}
void IPCProcessSkeleton::LockForNumExecuting()
{
if (getuid() != FOUNDATION_UID) {
return;
}
std::lock_guard<std::mutex> lockGuard(mutex_);
numExecuting_++;
}
void IPCProcessSkeleton::UnlockForNumExecuting()
{
if (getuid() != FOUNDATION_UID) {
return;
}
std::lock_guard<std::mutex> lockGuard(mutex_);
numExecuting_--;
if (numWaitingForThreads_ > 0) {
cv_.notify_all();
}
}
#ifndef CONFIG_IPC_SINGLE
sptr<IRemoteObject> IPCProcessSkeleton::GetSAMgrObject()
{

View File

@ -346,17 +346,12 @@ void BinderInvoker::StartWorkLoop()
}
uint32_t cmd = input_.ReadUint32();
IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
{
std::lock_guard<std::mutex> lockGuard(current->mutex_);
current->numExecuting_++;
if (current != nullptr) {
current->LockForNumExecuting();
}
int userError = HandleCommands(cmd);
{
std::lock_guard<std::mutex> lockGuard(current->mutex_);
current->numExecuting_--;
if (current->numWaitingForThreads_ > 0) {
current->cv_.notify_all();
}
if (current != nullptr) {
current->UnlockForNumExecuting();
}
if ((userError == -ERR_TIMED_OUT || userError == IPC_INVOKER_INVALID_DATA_ERR) && !isMainWorkThread) {
ZLOGW(LABEL, "exit, userError:%{public}d", userError);