| 6 |
| 7 | #include "base/debug/activity_tracker.h" |
| 8 |
| 9 | #include <windows.h> |
| 10 |
| 11 | namespace base { |
| 12 | namespace internal { |
| 13 |
| 14 | LockImpl::LockImpl() : native_handle_(SRWLOCK_INIT) {} |
| 15 |
| 16 | LockImpl::~LockImpl() = default; |
| 17 |
| 18 | bool LockImpl::Try() { |
| 19 | return !!::TryAcquireSRWLockExclusive( |
| 20 | reinterpret_cast<PSRWLOCK>(&native_handle_)); |
| 21 | } |
| 22 |
| 23 | void LockImpl::Lock() { |
| 24 | // The ScopedLockAcquireActivity below is relatively expensive and so its |
| 25 | // actions can become significant due to the very large number of locks |
| 26 | // that tend to be used throughout the build. To avoid this cost in the |
| 27 | // vast majority of the calls, simply "try" the lock first and only do the |
| 28 | // (tracked) blocking call if that fails. Since "try" itself is a system |
| 29 | // call, and thus also somewhat expensive, don't bother with it unless |
| 30 | // tracking is actually enabled. |
| 31 | if (base::debug::GlobalActivityTracker::IsEnabled()) |
| 32 | if (Try()) |
| 33 | return; |
| 34 |
| 35 | base::debug::ScopedLockAcquireActivity lock_activity(this); |
| 36 | ::AcquireSRWLockExclusive(reinterpret_cast<PSRWLOCK>(&native_handle_)); |
| 37 | } |
| 38 |
| 39 | } // namespace internal |
| 40 | } // namespace base |
| 41 |