Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
SpinLock.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "HAL/PlatformProcess.h"
7#include <atomic>
8
9namespace UE
10{
11 // A mutex that doesn't put the thread into a WAIT state but instead repeatedly tries to aquire the lock.
12 // WARNING: Should be used only for very short locks
13 // Use with `TScopeLock`
15 {
16 public:
18
19 FSpinLock() = default;
20
21 void Lock()
22 {
23 while (true)
24 {
25 if (!bFlag.exchange(true, std::memory_order_acquire))
26 {
27 break;
28 }
29
30 while (bFlag.load(std::memory_order_relaxed))
31 {
32 FPlatformProcess::Yield();
33 }
34 }
35 }
36
37 bool TryLock()
38 {
39 return !bFlag.exchange(true, std::memory_order_acquire);
40 }
41
42 void Unlock()
43 {
44 bFlag.store(false, std::memory_order_release);
45 }
46
47 private:
48 std::atomic<bool> bFlag{ false };
49 };
50}
#define UE_NONCOPYABLE(TypeName)
void Unlock()
Definition SpinLock.h:42
void Lock()
Definition SpinLock.h:21
bool TryLock()
Definition SpinLock.h:37
FSpinLock()=default
std::atomic< bool > bFlag
Definition SpinLock.h:48
Definition Vector.h:40
Definition json.hpp:4518