From 9dfdce5b3c3898de8ed20177d6c9252e1d2bc634 Mon Sep 17 00:00:00 2001 From: Sean Keely Date: Wed, 13 Sep 2017 18:13:51 -0500 Subject: [PATCH] Improve branch elimination in ScopedAcquire. GCC can't reasonably be told that the lock ptr isn't null. Adding a private bool allows the branch to be eliminated, along with the bool. Change-Id: I0605d69474d6a6e6951be93c0af1d8caf3f77124 --- runtime/hsa-runtime/core/util/locks.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/runtime/hsa-runtime/core/util/locks.h b/runtime/hsa-runtime/core/util/locks.h index c9ff9ee7b2..4b13c1e94b 100644 --- a/runtime/hsa-runtime/core/util/locks.h +++ b/runtime/hsa-runtime/core/util/locks.h @@ -56,20 +56,22 @@ class ScopedAcquire { public: /// @brief: When constructing, acquire the lock. /// @param: lock(Input), pointer to an existing lock. - explicit ScopedAcquire(LockType* lock) : lock_(lock) { lock_->Acquire(); } + explicit ScopedAcquire(LockType* lock) : lock_(lock), doRelease(true) { lock_->Acquire(); } /// @brief: when destructing, release the lock. ~ScopedAcquire() { - if (lock_ != nullptr) lock_->Release(); + if (doRelease) lock_->Release(); } + /// @brief: Release the lock early. Avoid using when possible. void Release() { lock_->Release(); - lock_ = nullptr; + doRelease = false; } private: LockType* lock_; + bool doRelease; /// @brief: Disable copiable and assignable ability. DISALLOW_COPY_AND_ASSIGN(ScopedAcquire); };