From 1cb64d2a48ab687584a2dda9ee304ffd6257a16b Mon Sep 17 00:00:00 2001 From: liyiming13 Date: Thu, 28 Mar 2024 17:28:27 +0800 Subject: [PATCH] Add refbase attempt Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/I9CFT2 Signed-off-by: liyiming13 --- base/include/refbase.h | 5 +++++ base/src/refbase.cpp | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/base/include/refbase.h b/base/include/refbase.h index 1134be4..9537d9d 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. * diff --git a/base/src/refbase.cpp b/base/src/refbase.cpp index b9db296..ec6d08a 100644 --- a/base/src/refbase.cpp +++ b/base/src/refbase.cpp @@ -302,6 +302,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); @@ -484,6 +489,11 @@ void RefBase::IncStrongRef(const void *objectId) } if (refs_->IsAttemptAcquireSet()) { refs_->ClearAttemptAcquire(); + const int attemptCount = refs_->GetAttemptAcquire(); + if (attemptCount < 0) { + UTILS_LOGF("Multi-threads trigger illegal decstrong from %{public}d due to AttemptIncStrong in ipc", + attemptCount); + } refs_->DecStrongRefCount(objectId); refs_->DecWeakRefCount(objectId); } -- Gitee