Implement SpinLockHolder in terms of std::lock_guard.

PiperOrigin-RevId: 841813256
Change-Id: Id8a446f889d999720c9d9db527a9f6dfb8b2d6a9
This commit is contained in:
Chris Kennelly
2025-12-08 10:08:47 -08:00
committed by Copybara-Service
parent 5614692612
commit f17f9070d2

View File

@@ -31,6 +31,7 @@
#include <atomic>
#include <cstdint>
#include <mutex>
#include <type_traits>
#include "absl/base/attributes.h"
@@ -247,25 +248,18 @@ class ABSL_LOCKABLE ABSL_ATTRIBUTE_WARN_UNUSED SpinLock {
// Corresponding locker object that arranges to acquire a spinlock for
// the duration of a C++ scope.
class ABSL_SCOPED_LOCKABLE [[nodiscard]] SpinLockHolder {
class ABSL_SCOPED_LOCKABLE [[nodiscard]] SpinLockHolder
: public std::lock_guard<SpinLock> {
public:
inline explicit SpinLockHolder(
SpinLock& l ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))
ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
: lock_(l) {
l.lock();
}
: std::lock_guard<SpinLock>(l) {}
ABSL_DEPRECATE_AND_INLINE()
inline explicit SpinLockHolder(SpinLock* l) ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
: SpinLockHolder(*l) {}
inline ~SpinLockHolder() ABSL_UNLOCK_FUNCTION() { lock_.unlock(); }
SpinLockHolder(const SpinLockHolder&) = delete;
SpinLockHolder& operator=(const SpinLockHolder&) = delete;
private:
SpinLock& lock_;
inline ~SpinLockHolder() ABSL_UNLOCK_FUNCTION() = default;
};
// Register a hook for profiling support.