-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add queue spinlock
- Loading branch information
Showing
8 changed files
with
171 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// Created by konstantin on 28.07.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
|
||
namespace NSync { | ||
|
||
class QueueSpinLock final { | ||
public: | ||
class Guard final { | ||
public: | ||
explicit Guard(QueueSpinLock& host) : host(host) { host.Acquire(this); } | ||
~Guard() { | ||
if (is_owner) Release(); | ||
} | ||
|
||
void Release() { | ||
host.Release(this); | ||
is_owner.store(false); | ||
} | ||
|
||
void SetOwner() { is_owner.store(true); } | ||
|
||
void SetNext(Guard* guard) { next.store(guard); } | ||
|
||
bool IsOwner() const { return is_owner.load(); } | ||
|
||
bool HasNext() const { return next.load() != nullptr; } | ||
|
||
void SetNextOwner() { next.load()->SetOwner(); } | ||
|
||
private: | ||
QueueSpinLock& host; | ||
std::atomic<Guard*> next{}; | ||
std::atomic<bool> is_owner{}; | ||
}; | ||
|
||
private: | ||
void Acquire(Guard* guard) { | ||
auto ancestor = tail_.exchange(guard); | ||
if (ancestor == nullptr) { | ||
guard->SetOwner(); | ||
return; | ||
} | ||
|
||
ancestor->SetNext(guard); | ||
while (!guard->IsOwner()) { | ||
} | ||
} | ||
|
||
void Release(Guard* guard) { | ||
if (guard->HasNext()) { | ||
guard->SetNextOwner(); | ||
return; | ||
} | ||
|
||
Guard* old_guard = guard; | ||
while (!tail_.compare_exchange_weak(old_guard, nullptr)) { | ||
if (guard->HasNext()) { | ||
guard->SetNextOwner(); | ||
return; | ||
} | ||
old_guard = guard; | ||
} | ||
} | ||
|
||
std::atomic<Guard*> tail_{}; | ||
}; | ||
|
||
} // namespace NSync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// Created by konstantin on 28.07.24. | ||
// | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include <thread> | ||
|
||
#include <components/sync/queue_spinlock.h> | ||
|
||
TEST(TestQueueSpinlock, LockUnlock) { | ||
NSync::QueueSpinLock spinlock; | ||
|
||
{ | ||
NSync::QueueSpinLock::Guard guard(spinlock); // <-- Acquired | ||
// Critical section | ||
} // <-- Released | ||
} | ||
|
||
TEST(TestQueueSpinlock, SequentialLockUnlock) { | ||
NSync::QueueSpinLock spinlock; | ||
|
||
{ | ||
NSync::QueueSpinLock::Guard guard(spinlock); | ||
// Critical section | ||
} | ||
|
||
{ | ||
NSync::QueueSpinLock::Guard guard(spinlock); | ||
// Critical section | ||
} | ||
} | ||
|
||
TEST(TestQueueSpinlock, ConcurrentIncrements) { | ||
NSync::QueueSpinLock spinlock; | ||
size_t counter = 0; | ||
|
||
const size_t kIncrementsPerThread = 1000; | ||
|
||
auto contender = [&] { | ||
for (size_t i = 0; i < kIncrementsPerThread; ++i) { | ||
NSync::QueueSpinLock::Guard guard(spinlock); | ||
|
||
size_t current = counter; | ||
std::this_thread::yield(); | ||
counter = current + 1; | ||
} | ||
}; | ||
|
||
std::thread t1(contender); | ||
std::thread t2(contender); | ||
t1.join(); | ||
t2.join(); | ||
|
||
std::cout << "Shared counter value: " << counter << std::endl; | ||
|
||
ASSERT_EQ(counter, 2 * kIncrementsPerThread); | ||
} |