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
This commit is contained in:
Sean Keely
2017-09-13 18:13:51 -05:00
szülő 4275661682
commit 9dfdce5b3c
@@ -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);
};