From 8b7b2ecb4fc868564a26f487b672d995f9fc01c6 Mon Sep 17 00:00:00 2001 From: chenkeyu Date: Tue, 16 Apr 2024 09:57:59 +0800 Subject: [PATCH] Add attempt acquire set Issue:https://gitee.com/openharmony/communication_ipc/issues/I9GX5O Signed-off-by: chenkeyu --- base/include/refbase.h | 14 ++++++++++++++ base/src/refbase.cpp | 16 ++++++++++++++++ base/test/unittest/common/utils_refbase_test.cpp | 4 ++++ 3 files changed, 34 insertions(+) diff --git a/base/include/refbase.h b/base/include/refbase.h index a49bfcc..b3fef00 100644 --- a/base/include/refbase.h +++ b/base/include/refbase.h @@ -184,6 +184,11 @@ public: */ void SetAttemptAcquire(); + /** + * @brief Get the current times of attempts. + */ + int GetAttemptAcquire(); + /** * @brief Check if the number of attempts is greater than 0. * @@ -490,6 +495,15 @@ public: */ bool AttemptAcquire(const void *objectId); + // Only for IPC use. + /** + * @brief Check attempt acquire is setted + * + * @note This fuction is extracted from `IncStrongRef()` + * It is only for IPC use. + */ + void CheckIsAttemptAcquireSet(const void *objectId); + /** * @brief Attempts to increment the count of strong references. * diff --git a/base/src/refbase.cpp b/base/src/refbase.cpp index 3b71cf7..ffeed57 100644 --- a/base/src/refbase.cpp +++ b/base/src/refbase.cpp @@ -319,6 +319,11 @@ int RefCounter::GetWeakRefCount() return atomicWeak_.load(std::memory_order_relaxed); } +int RefCounter::GetAttemptAcquire() +{ + return atomicAttempt_.load(std::memory_order_relaxed); +} + void RefCounter::SetAttemptAcquire() { (void)atomicAttempt_.fetch_add(1, std::memory_order_relaxed); @@ -496,8 +501,19 @@ void RefBase::IncStrongRef(const void *objectId) if (curCount == INITIAL_PRIMARY_VALUE) { OnFirstStrongRef(objectId); } +} + +void RefBase::CheckIsAttemptAcquireSet(const void *objectId) +{ if (refs_->IsAttemptAcquireSet()) { refs_->ClearAttemptAcquire(); +#ifdef DEBUG_REFBASE + const int attemptCount = refs_->GetAttemptAcquire(); + if (attemptCount < 0) { + UTILS_LOGF("Multi-threads trigger illegal decstrong from %{public}d due to AttemptIncStrong in ipc", + attemptCount); + } +#endif refs_->DecStrongRefCount(objectId); refs_->DecWeakRefCount(objectId); } diff --git a/base/test/unittest/common/utils_refbase_test.cpp b/base/test/unittest/common/utils_refbase_test.cpp index 5041e41..e41ebc2 100644 --- a/base/test/unittest/common/utils_refbase_test.cpp +++ b/base/test/unittest/common/utils_refbase_test.cpp @@ -304,6 +304,7 @@ int RegisterEventThread() int handle = 10; for (int i = 0; i < CYCLE_NUM2; i++) { sptr remote = ipc.FindOrNewObject(handle); + remote->CheckIsAttemptAcquireSet(remote); if (remote) { remote->IsProxyObject(); } @@ -348,10 +349,12 @@ HWTEST_F(UtilsRefbaseTest, testRefbaseOperateNull001, TestSize.Level0) remoteObject->IncWeakRef(nullptr); remoteObject->IncStrongRef(nullptr); + remoteObject->CheckIsAttemptAcquireSet(nullptr); remoteObject->DecStrongRef(nullptr); remoteObject->AttemptAcquire(this); remoteObject->IncStrongRef(nullptr); + remoteObject->CheckIsAttemptAcquireSet(nullptr); remoteObject->DecStrongRef(nullptr); remoteObject->DecWeakRef(nullptr); @@ -468,6 +471,7 @@ HWTEST_F(UtilsRefbaseTest, testRefbaseAcquire001, TestSize.Level0) EXPECT_EQ(testobject->GetSptrRefCount(), 1); { EXPECT_TRUE(testobject->IsAttemptAcquireSet()); + testobject->CheckIsAttemptAcquireSet(this); sptr sptrRef = testobject; EXPECT_EQ(sptrRef->GetSptrRefCount(), 1); EXPECT_FALSE(testobject->IsAttemptAcquireSet()); -- Gitee