Skip to content

Commit 785b11b

Browse files
ckennellycopybara-github
authored andcommitted
Accept references on SpinLockHolder/MutexLock
This aligns these classes more closely with std::scoped_lock. PiperOrigin-RevId: 788920634 Change-Id: If29e8092b782bfbb11a1da31834dc738f1cbcfbc
1 parent fbab4bf commit 785b11b

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

absl/base/internal/spinlock.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,20 @@ class ABSL_LOCKABLE ABSL_ATTRIBUTE_WARN_UNUSED SpinLock {
235235
// the duration of a C++ scope.
236236
class ABSL_SCOPED_LOCKABLE [[nodiscard]] SpinLockHolder {
237237
public:
238-
inline explicit SpinLockHolder(SpinLock* l) ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
238+
inline explicit SpinLockHolder(SpinLock& l) ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
239239
: lock_(l) {
240-
l->lock();
240+
l.lock();
241241
}
242-
inline ~SpinLockHolder() ABSL_UNLOCK_FUNCTION() { lock_->unlock(); }
242+
inline explicit SpinLockHolder(SpinLock* l) ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
243+
: SpinLockHolder(*l) {}
244+
245+
inline ~SpinLockHolder() ABSL_UNLOCK_FUNCTION() { lock_.unlock(); }
243246

244247
SpinLockHolder(const SpinLockHolder&) = delete;
245248
SpinLockHolder& operator=(const SpinLockHolder&) = delete;
246249

247250
private:
248-
SpinLock* lock_;
251+
SpinLock& lock_;
249252
};
250253

251254
// Register a hook for profiling support.

absl/synchronization/mutex.h

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -602,29 +602,31 @@ class ABSL_SCOPED_LOCKABLE MutexLock {
602602
// Calls `mu->Lock()` and returns when that call returns. That is, `*mu` is
603603
// guaranteed to be locked when this object is constructed. Requires that
604604
// `mu` be dereferenceable.
605-
explicit MutexLock(Mutex* absl_nonnull mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
606-
: mu_(mu) {
607-
this->mu_->Lock();
605+
explicit MutexLock(Mutex& mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
606+
this->mu_.Lock();
608607
}
609608

609+
explicit MutexLock(Mutex* absl_nonnull mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
610+
: MutexLock(*mu) {}
611+
610612
// Like above, but calls `mu->LockWhen(cond)` instead. That is, in addition to
611613
// the above, the condition given by `cond` is also guaranteed to hold when
612614
// this object is constructed.
613615
explicit MutexLock(Mutex* absl_nonnull mu, const Condition& cond)
614616
ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
615-
: mu_(mu) {
616-
this->mu_->LockWhen(cond);
617+
: mu_(*mu) {
618+
this->mu_.LockWhen(cond);
617619
}
618620

619621
MutexLock(const MutexLock&) = delete; // NOLINT(runtime/mutex)
620622
MutexLock(MutexLock&&) = delete; // NOLINT(runtime/mutex)
621623
MutexLock& operator=(const MutexLock&) = delete;
622624
MutexLock& operator=(MutexLock&&) = delete;
623625

624-
~MutexLock() ABSL_UNLOCK_FUNCTION() { this->mu_->Unlock(); }
626+
~MutexLock() ABSL_UNLOCK_FUNCTION() { this->mu_.Unlock(); }
625627

626628
private:
627-
Mutex* absl_nonnull const mu_;
629+
Mutex& mu_;
628630
};
629631

630632
// ReaderMutexLock
@@ -633,26 +635,32 @@ class ABSL_SCOPED_LOCKABLE MutexLock {
633635
// releases a shared lock on a `Mutex` via RAII.
634636
class ABSL_SCOPED_LOCKABLE ReaderMutexLock {
635637
public:
638+
explicit ReaderMutexLock(Mutex& mu) ABSL_SHARED_LOCK_FUNCTION(mu) : mu_(mu) {
639+
mu.ReaderLock();
640+
}
641+
636642
explicit ReaderMutexLock(Mutex* absl_nonnull mu) ABSL_SHARED_LOCK_FUNCTION(mu)
643+
: ReaderMutexLock(*mu) {}
644+
645+
explicit ReaderMutexLock(Mutex& mu, const Condition& cond)
646+
ABSL_SHARED_LOCK_FUNCTION(mu)
637647
: mu_(mu) {
638-
mu->ReaderLock();
648+
mu.ReaderLockWhen(cond);
639649
}
640650

641651
explicit ReaderMutexLock(Mutex* absl_nonnull mu, const Condition& cond)
642652
ABSL_SHARED_LOCK_FUNCTION(mu)
643-
: mu_(mu) {
644-
mu->ReaderLockWhen(cond);
645-
}
653+
: ReaderMutexLock(*mu, cond) {}
646654

647655
ReaderMutexLock(const ReaderMutexLock&) = delete;
648656
ReaderMutexLock(ReaderMutexLock&&) = delete;
649657
ReaderMutexLock& operator=(const ReaderMutexLock&) = delete;
650658
ReaderMutexLock& operator=(ReaderMutexLock&&) = delete;
651659

652-
~ReaderMutexLock() ABSL_UNLOCK_FUNCTION() { this->mu_->ReaderUnlock(); }
660+
~ReaderMutexLock() ABSL_UNLOCK_FUNCTION() { this->mu_.ReaderUnlock(); }
653661

654662
private:
655-
Mutex* absl_nonnull const mu_;
663+
Mutex& mu_;
656664
};
657665

658666
// WriterMutexLock
@@ -661,27 +669,34 @@ class ABSL_SCOPED_LOCKABLE ReaderMutexLock {
661669
// releases a write (exclusive) lock on a `Mutex` via RAII.
662670
class ABSL_SCOPED_LOCKABLE WriterMutexLock {
663671
public:
672+
explicit WriterMutexLock(Mutex& mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
673+
: mu_(mu) {
674+
mu.WriterLock();
675+
}
676+
664677
explicit WriterMutexLock(Mutex* absl_nonnull mu)
678+
ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
679+
: WriterMutexLock(*mu) {}
680+
681+
explicit WriterMutexLock(Mutex& mu, const Condition& cond)
665682
ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
666683
: mu_(mu) {
667-
mu->WriterLock();
684+
mu.WriterLockWhen(cond);
668685
}
669686

670687
explicit WriterMutexLock(Mutex* absl_nonnull mu, const Condition& cond)
671688
ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
672-
: mu_(mu) {
673-
mu->WriterLockWhen(cond);
674-
}
689+
: WriterMutexLock(*mu, cond) {}
675690

676691
WriterMutexLock(const WriterMutexLock&) = delete;
677692
WriterMutexLock(WriterMutexLock&&) = delete;
678693
WriterMutexLock& operator=(const WriterMutexLock&) = delete;
679694
WriterMutexLock& operator=(WriterMutexLock&&) = delete;
680695

681-
~WriterMutexLock() ABSL_UNLOCK_FUNCTION() { this->mu_->WriterUnlock(); }
696+
~WriterMutexLock() ABSL_UNLOCK_FUNCTION() { this->mu_.WriterUnlock(); }
682697

683698
private:
684-
Mutex* absl_nonnull const mu_;
699+
Mutex& mu_;
685700
};
686701

687702
// -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)