Skip to content

Commit 3e0d9ff

Browse files
ckennellycopybara-github
authored andcommitted
Inline SpinLock Lock->lock, Unlock->unlock internal to Abseil.
PiperOrigin-RevId: 788531193 Change-Id: Icca9ff096c2ec2fc0662f1f94ecfb232a8492974
1 parent 1f28b48 commit 3e0d9ff

9 files changed

Lines changed: 39 additions & 62 deletions

File tree

absl/base/internal/low_level_alloc.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ class ABSL_SCOPED_LOCKABLE ArenaLock {
318318
mask_valid_ = pthread_sigmask(SIG_BLOCK, &all, &mask_) == 0;
319319
}
320320
#endif
321-
arena_->mu.Lock();
321+
arena_->mu.lock();
322322
}
323323
~ArenaLock() { ABSL_RAW_CHECK(left_, "haven't left Arena region"); }
324324
void Leave() ABSL_UNLOCK_FUNCTION() {
325-
arena_->mu.Unlock();
325+
arena_->mu.unlock();
326326
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
327327
if (mask_valid_) {
328328
const int err = pthread_sigmask(SIG_SETMASK, &mask_, nullptr);
@@ -573,7 +573,7 @@ static void *DoAllocWithArena(size_t request, LowLevelAlloc::Arena *arena) {
573573
}
574574
// we unlock before mmap() both because mmap() may call a callback hook,
575575
// and because it may be slow.
576-
arena->mu.Unlock();
576+
arena->mu.unlock();
577577
// mmap generous 64K chunks to decrease
578578
// the chances/impact of fragmentation:
579579
size_t new_pages_size = RoundUp(req_rnd, arena->pagesize * 16);
@@ -612,7 +612,7 @@ static void *DoAllocWithArena(size_t request, LowLevelAlloc::Arena *arena) {
612612
#endif
613613
#endif // __linux__
614614
#endif // _WIN32
615-
arena->mu.Lock();
615+
arena->mu.lock();
616616
s = reinterpret_cast<AllocList *>(new_pages);
617617
s->header.size = new_pages_size;
618618
// Pretend the block is allocated; call AddToFreelist() to free it.

absl/base/internal/spinlock_benchmark.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void BM_TryLock(benchmark::State& state) {
3535
static absl::NoDestructor<absl::base_internal::SpinLock> spinlock(
3636
scheduling_mode);
3737
for (auto _ : state) {
38-
if (spinlock->TryLock()) spinlock->Unlock();
38+
if (spinlock->try_lock()) spinlock->unlock();
3939
}
4040
}
4141

absl/base/spinlock_test_common.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,13 @@ static_assert(std::is_trivially_destructible<SpinLock>(), "");
132132

133133
TEST(SpinLock, StackNonCooperativeDisablesScheduling) {
134134
SpinLock spinlock(base_internal::SCHEDULE_KERNEL_ONLY);
135-
spinlock.Lock();
135+
SpinLockHolder l(&spinlock);
136136
EXPECT_FALSE(base_internal::SchedulingGuard::ReschedulingIsAllowed());
137-
spinlock.Unlock();
138137
}
139138

140139
TEST(SpinLock, StaticNonCooperativeDisablesScheduling) {
141-
static_noncooperative_spinlock.Lock();
140+
SpinLockHolder l(&static_noncooperative_spinlock);
142141
EXPECT_FALSE(base_internal::SchedulingGuard::ReschedulingIsAllowed());
143-
static_noncooperative_spinlock.Unlock();
144142
}
145143

146144
TEST(SpinLock, WaitCyclesEncoding) {

absl/debugging/symbolize_elf.inc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ struct FileMappingHint {
167167
// We are using SpinLock and not a Mutex here, because we may be called
168168
// from inside Mutex::Lock itself, and it prohibits recursive calls.
169169
// This happens in e.g. base/stacktrace_syscall_unittest.
170-
// Moreover, we are using only TryLock(), if the decorator list
170+
// Moreover, we are using only try_lock(), if the decorator list
171171
// is being modified (is busy), we skip all decorators, and possibly
172172
// loose some info. Sorry, that's the best we could do.
173173
ABSL_CONST_INIT absl::base_internal::SpinLock g_decorators_mu(
@@ -1549,7 +1549,7 @@ const char *Symbolizer::GetUncachedSymbol(const void *pc) {
15491549
#endif
15501550
}
15511551

1552-
if (g_decorators_mu.TryLock()) {
1552+
if (g_decorators_mu.try_lock()) {
15531553
if (g_num_decorators > 0) {
15541554
SymbolDecoratorArgs decorator_args = {
15551555
pc, relocation, fd, symbol_buf_, sizeof(symbol_buf_),
@@ -1559,7 +1559,7 @@ const char *Symbolizer::GetUncachedSymbol(const void *pc) {
15591559
g_decorators[i].fn(&decorator_args);
15601560
}
15611561
}
1562-
g_decorators_mu.Unlock();
1562+
g_decorators_mu.unlock();
15631563
}
15641564
if (symbol_buf_[0] == '\0') {
15651565
return nullptr;
@@ -1605,17 +1605,17 @@ const char *Symbolizer::GetSymbol(const void *pc) {
16051605
}
16061606

16071607
bool RemoveAllSymbolDecorators(void) {
1608-
if (!g_decorators_mu.TryLock()) {
1608+
if (!g_decorators_mu.try_lock()) {
16091609
// Someone else is using decorators. Get out.
16101610
return false;
16111611
}
16121612
g_num_decorators = 0;
1613-
g_decorators_mu.Unlock();
1613+
g_decorators_mu.unlock();
16141614
return true;
16151615
}
16161616

16171617
bool RemoveSymbolDecorator(int ticket) {
1618-
if (!g_decorators_mu.TryLock()) {
1618+
if (!g_decorators_mu.try_lock()) {
16191619
// Someone else is using decorators. Get out.
16201620
return false;
16211621
}
@@ -1629,14 +1629,14 @@ bool RemoveSymbolDecorator(int ticket) {
16291629
break;
16301630
}
16311631
}
1632-
g_decorators_mu.Unlock();
1632+
g_decorators_mu.unlock();
16331633
return true; // Decorator is known to be removed.
16341634
}
16351635

16361636
int InstallSymbolDecorator(SymbolDecorator decorator, void *arg) {
16371637
static int ticket = 0;
16381638

1639-
if (!g_decorators_mu.TryLock()) {
1639+
if (!g_decorators_mu.try_lock()) {
16401640
// Someone else is using decorators. Get out.
16411641
return -2;
16421642
}
@@ -1647,7 +1647,7 @@ int InstallSymbolDecorator(SymbolDecorator decorator, void *arg) {
16471647
g_decorators[g_num_decorators] = {decorator, arg, ticket++};
16481648
++g_num_decorators;
16491649
}
1650-
g_decorators_mu.Unlock();
1650+
g_decorators_mu.unlock();
16511651
return ret;
16521652
}
16531653

@@ -1658,7 +1658,7 @@ bool RegisterFileMappingHint(const void *start, const void *end,
16581658

16591659
base_internal::InitSigSafeArena();
16601660

1661-
if (!g_file_mapping_mu.TryLock()) {
1661+
if (!g_file_mapping_mu.try_lock()) {
16621662
return false;
16631663
}
16641664

@@ -1681,13 +1681,13 @@ bool RegisterFileMappingHint(const void *start, const void *end,
16811681
hint.filename = dst;
16821682
}
16831683

1684-
g_file_mapping_mu.Unlock();
1684+
g_file_mapping_mu.unlock();
16851685
return ret;
16861686
}
16871687

16881688
bool GetFileMappingHint(const void **start, const void **end, uint64_t *offset,
16891689
const char **filename) {
1690-
if (!g_file_mapping_mu.TryLock()) {
1690+
if (!g_file_mapping_mu.try_lock()) {
16911691
return false;
16921692
}
16931693
bool found = false;
@@ -1710,7 +1710,7 @@ bool GetFileMappingHint(const void **start, const void **end, uint64_t *offset,
17101710
break;
17111711
}
17121712
}
1713-
g_file_mapping_mu.Unlock();
1713+
g_file_mapping_mu.unlock();
17141714
return found;
17151715
}
17161716

absl/log/internal/vlog_config.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ void UpdateVLogSites() ABSL_UNLOCK_FUNCTION(mutex)
267267
// have to wait on all updates in order to acquire `mutex` and initialize
268268
// themselves.
269269
absl::MutexLock ul(GetUpdateSitesMutex());
270-
mutex.Unlock();
270+
mutex.unlock();
271271
VLogSite* n = site_list_head.load(std::memory_order_seq_cst);
272272
// Because sites are added to the list in the order they are executed, there
273273
// tend to be clusters of entries with the same file.
@@ -299,7 +299,7 @@ void UpdateVModule(absl::string_view vmodule)
299299
if (!absl::SimpleAtoi(glob_level.substr(eq + 1), &level)) continue;
300300
glob_levels.emplace_back(glob, level);
301301
}
302-
mutex.Lock(); // Unlocked by UpdateVLogSites().
302+
mutex.lock(); // unlocked by UpdateVLogSites().
303303
get_vmodule_info().clear();
304304
for (const auto& it : glob_levels) {
305305
const absl::string_view glob = it.first;
@@ -311,10 +311,10 @@ void UpdateVModule(absl::string_view vmodule)
311311

312312
int UpdateGlobalVLogLevel(int v)
313313
ABSL_LOCKS_EXCLUDED(mutex, GetUpdateSitesMutex()) {
314-
mutex.Lock(); // Unlocked by UpdateVLogSites().
314+
mutex.lock(); // unlocked by UpdateVLogSites().
315315
const int old_global_v = global_v;
316316
if (v == global_v) {
317-
mutex.Unlock();
317+
mutex.unlock();
318318
return old_global_v;
319319
}
320320
global_v = v;
@@ -324,7 +324,7 @@ int UpdateGlobalVLogLevel(int v)
324324

325325
int PrependVModule(absl::string_view module_pattern, int log_level)
326326
ABSL_LOCKS_EXCLUDED(mutex, GetUpdateSitesMutex()) {
327-
mutex.Lock(); // Unlocked by UpdateVLogSites().
327+
mutex.lock(); // unlocked by UpdateVLogSites().
328328
int old_v = PrependVModuleLocked(module_pattern, log_level);
329329
UpdateVLogSites();
330330
return old_v;

absl/synchronization/internal/graphcycles.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ ABSL_CONST_INIT static absl::base_internal::SpinLock arena_mu(
5858
ABSL_CONST_INIT static base_internal::LowLevelAlloc::Arena* arena;
5959

6060
static void InitArenaIfNecessary() {
61-
arena_mu.Lock();
61+
base_internal::SpinLockHolder l(&arena_mu);
6262
if (arena == nullptr) {
6363
arena = base_internal::LowLevelAlloc::NewArena(0);
6464
}
65-
arena_mu.Unlock();
6665
}
6766

6867
// Number of inlined elements in Vec. Hash table implementation

absl/synchronization/mutex.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ static SynchEvent* EnsureSynchEvent(std::atomic<intptr_t>* addr,
330330
const char* name, intptr_t bits,
331331
intptr_t lockbit) {
332332
uint32_t h = reinterpret_cast<uintptr_t>(addr) % kNSynchEvent;
333-
synch_event_mu.Lock();
333+
synch_event_mu.lock();
334334
// When a Mutex/CondVar is destroyed, we don't remove the associated
335335
// SynchEvent to keep destructors empty in release builds for performance
336336
// reasons. If the current call is the first to set bits (kMuEvent/kCVEvent),
@@ -392,16 +392,16 @@ static SynchEvent* EnsureSynchEvent(std::atomic<intptr_t>* addr,
392392
} else {
393393
e->refcount++; // for return value
394394
}
395-
synch_event_mu.Unlock();
395+
synch_event_mu.unlock();
396396
return e;
397397
}
398398

399399
// Decrement the reference count of *e, or do nothing if e==null.
400400
static void UnrefSynchEvent(SynchEvent* e) {
401401
if (e != nullptr) {
402-
synch_event_mu.Lock();
402+
synch_event_mu.lock();
403403
bool del = (--(e->refcount) == 0);
404-
synch_event_mu.Unlock();
404+
synch_event_mu.unlock();
405405
if (del) {
406406
base_internal::LowLevelAlloc::Free(e);
407407
}
@@ -414,15 +414,15 @@ static void UnrefSynchEvent(SynchEvent* e) {
414414
static SynchEvent* GetSynchEvent(const void* addr) {
415415
uint32_t h = reinterpret_cast<uintptr_t>(addr) % kNSynchEvent;
416416
SynchEvent* e;
417-
synch_event_mu.Lock();
417+
synch_event_mu.lock();
418418
for (e = synch_event[h];
419419
e != nullptr && e->masked_addr != base_internal::HidePtr(addr);
420420
e = e->next) {
421421
}
422422
if (e != nullptr) {
423423
e->refcount++;
424424
}
425-
synch_event_mu.Unlock();
425+
synch_event_mu.unlock();
426426
return e;
427427
}
428428

@@ -1223,9 +1223,9 @@ static GraphId GetGraphIdLocked(Mutex* mu)
12231223
}
12241224

12251225
static GraphId GetGraphId(Mutex* mu) ABSL_LOCKS_EXCLUDED(deadlock_graph_mu) {
1226-
deadlock_graph_mu.Lock();
1226+
deadlock_graph_mu.lock();
12271227
GraphId id = GetGraphIdLocked(mu);
1228-
deadlock_graph_mu.Unlock();
1228+
deadlock_graph_mu.unlock();
12291229
return id;
12301230
}
12311231

@@ -1456,7 +1456,7 @@ static GraphId DeadlockCheck(Mutex* mu) {
14561456
}
14571457
if (synch_deadlock_detection.load(std::memory_order_acquire) ==
14581458
OnDeadlockCycle::kAbort) {
1459-
deadlock_graph_mu.Unlock(); // avoid deadlock in fatal sighandler
1459+
deadlock_graph_mu.unlock(); // avoid deadlock in fatal sighandler
14601460
ABSL_RAW_LOG(FATAL, "dying due to potential deadlock");
14611461
return mu_id;
14621462
}
@@ -1481,11 +1481,11 @@ static inline GraphId DebugOnlyDeadlockCheck(Mutex* mu) {
14811481
void Mutex::ForgetDeadlockInfo() {
14821482
if (kDebugMode && synch_deadlock_detection.load(std::memory_order_acquire) !=
14831483
OnDeadlockCycle::kIgnore) {
1484-
deadlock_graph_mu.Lock();
1484+
deadlock_graph_mu.lock();
14851485
if (deadlock_graph != nullptr) {
14861486
deadlock_graph->RemoveNode(this);
14871487
}
1488-
deadlock_graph_mu.Unlock();
1488+
deadlock_graph_mu.unlock();
14891489
}
14901490
}
14911491

absl/synchronization/mutex_benchmark.cc

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,6 @@ static void DelayNs(int64_t ns, int* data) {
7272
}
7373
}
7474

75-
template <typename MutexType>
76-
class RaiiLocker {
77-
public:
78-
explicit RaiiLocker(MutexType* mu) : mu_(mu) { mu_->Lock(); }
79-
~RaiiLocker() { mu_->Unlock(); }
80-
private:
81-
MutexType* mu_;
82-
};
83-
84-
template <>
85-
class RaiiLocker<std::mutex> {
86-
public:
87-
explicit RaiiLocker(std::mutex* mu) : mu_(mu) { mu_->lock(); }
88-
~RaiiLocker() { mu_->unlock(); }
89-
private:
90-
std::mutex* mu_;
91-
};
92-
9375
// RAII object to change the Mutex priority of the running thread.
9476
class ScopedThreadMutexPriority {
9577
public:
@@ -226,7 +208,7 @@ void BM_Contended(benchmark::State& state) {
226208
// to keep ratio between local work and critical section approximately
227209
// equal regardless of number of threads.
228210
DelayNs(100 * state.threads(), &local);
229-
RaiiLocker<MutexType> locker(&shared->mu);
211+
std::scoped_lock locker(shared->mu);
230212
DelayNs(state.range(0), &shared->data);
231213
}
232214
}

absl/time/clock.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ static int64_t GetCurrentTimeNanosSlowPath()
415415
ABSL_LOCKS_EXCLUDED(time_state.lock) {
416416
// Serialize access to slow-path. Fast-path readers are not blocked yet, and
417417
// code below must not modify last_sample until the seqlock is acquired.
418-
time_state.lock.Lock();
418+
base_internal::SpinLockHolder l(&time_state.lock);
419419

420420
// Sample the kernel time base. This is the definition of
421421
// "now" if we take the slow path.
@@ -446,8 +446,6 @@ static int64_t GetCurrentTimeNanosSlowPath()
446446
UpdateLastSample(now_cycles, now_ns, delta_cycles, &sample);
447447
}
448448

449-
time_state.lock.Unlock();
450-
451449
return static_cast<int64_t>(estimated_base_ns);
452450
}
453451

0 commit comments

Comments
 (0)