Initial commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#include "Core/Base/global_include.h"
|
||||
#include <chrono>
|
||||
namespace Psc {
|
||||
void Empty_Lock::lock() {}
|
||||
void Empty_Lock::unlock() {}
|
||||
bool Empty_Lock::tryLock() {
|
||||
return true;
|
||||
}
|
||||
bool Empty_Lock::tryLock(int durationMillis) {
|
||||
return true;
|
||||
}
|
||||
void Spin_Lock::lock() {
|
||||
while (flag.test_and_set(std::memory_order_acquire)) {}
|
||||
}
|
||||
bool Spin_Lock::tryLock() {
|
||||
return !flag.test_and_set(std::memory_order_acquire);
|
||||
}
|
||||
bool Spin_Lock::tryLock(int durationMillis) {
|
||||
if (durationMillis == 0) {
|
||||
return tryLock();
|
||||
}
|
||||
auto duration = std::chrono::milliseconds(durationMillis);
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
while (std::chrono::steady_clock::now() - start < duration) {
|
||||
if (tryLock()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void Spin_Lock::unlock() {
|
||||
flag.clear(std::memory_order_release);
|
||||
}
|
||||
|
||||
} // namespace Psc
|
||||
Reference in New Issue
Block a user