Skip to content

Commit 2e82cf7

Browse files
committed
Spinlock: Removed superfluous load() in try_lock and cleaned up lock() loop
1 parent 6673570 commit 2e82cf7

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

api/include/opentelemetry/common/spin_lock_mutex.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ class SpinLockMutex
8282
*/
8383
bool try_lock() noexcept
8484
{
85-
return !flag_.load(std::memory_order_relaxed) &&
86-
!flag_.exchange(true, std::memory_order_acquire);
85+
return !flag_.exchange(true, std::memory_order_acquire);
8786
}
8887

8988
/**
@@ -95,13 +94,9 @@ class SpinLockMutex
9594
*/
9695
void lock() noexcept
9796
{
98-
for (;;)
97+
// Try once
98+
while (!try_lock())
9999
{
100-
// Try once
101-
if (!flag_.exchange(true, std::memory_order_acquire))
102-
{
103-
return;
104-
}
105100
// Spin-Fast (goal ~10ns)
106101
for (std::size_t i = 0; i < SPINLOCK_FAST_ITERATIONS; ++i)
107102
{

0 commit comments

Comments
 (0)