Skip to content

Commit f17f907

Browse files
ckennellycopybara-github
authored andcommitted
Implement SpinLockHolder in terms of std::lock_guard.
PiperOrigin-RevId: 841813256 Change-Id: Id8a446f889d999720c9d9db527a9f6dfb8b2d6a9
1 parent 5614692 commit f17f907

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

absl/base/internal/spinlock.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <atomic>
3333
#include <cstdint>
34+
#include <mutex>
3435
#include <type_traits>
3536

3637
#include "absl/base/attributes.h"
@@ -247,25 +248,18 @@ class ABSL_LOCKABLE ABSL_ATTRIBUTE_WARN_UNUSED SpinLock {
247248

248249
// Corresponding locker object that arranges to acquire a spinlock for
249250
// the duration of a C++ scope.
250-
class ABSL_SCOPED_LOCKABLE [[nodiscard]] SpinLockHolder {
251+
class ABSL_SCOPED_LOCKABLE [[nodiscard]] SpinLockHolder
252+
: public std::lock_guard<SpinLock> {
251253
public:
252254
inline explicit SpinLockHolder(
253255
SpinLock& l ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))
254256
ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
255-
: lock_(l) {
256-
l.lock();
257-
}
257+
: std::lock_guard<SpinLock>(l) {}
258258
ABSL_DEPRECATE_AND_INLINE()
259259
inline explicit SpinLockHolder(SpinLock* l) ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
260260
: SpinLockHolder(*l) {}
261261

262-
inline ~SpinLockHolder() ABSL_UNLOCK_FUNCTION() { lock_.unlock(); }
263-
264-
SpinLockHolder(const SpinLockHolder&) = delete;
265-
SpinLockHolder& operator=(const SpinLockHolder&) = delete;
266-
267-
private:
268-
SpinLock& lock_;
262+
inline ~SpinLockHolder() ABSL_UNLOCK_FUNCTION() = default;
269263
};
270264

271265
// Register a hook for profiling support.

0 commit comments

Comments
 (0)