From 3b4be872df34c000c6590b4d227e214feaf61a9a Mon Sep 17 00:00:00 2001 From: liyiming13 Date: Thu, 28 Mar 2024 17:28:27 +0800 Subject: [PATCH] fixed 1cb64d2 from https://gitee.com/chenchong_666/commonlibrary_c_utils/pulls/381 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 a49bfcc..a35b026 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 3b71cf7..1a1f26c 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); @@ -498,6 +503,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