UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
CriticalSectionQueryable.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "HAL/PlatformTLS.h"
7#include "Misc/ScopeLock.h"
8
9#include <atomic>
10
13{
14public:
19
21 : OwnerThreadId(0)
22 {
23 }
24
25 void Lock()
26 {
27 Inner.Lock();
28 SetOwner();
29 }
30
31 bool TryLock()
32 {
33 if (Inner.TryLock())
34 {
35 SetOwner();
36 return true;
37 }
38 return false;
39 }
40
41 void Unlock()
42 {
44 ClearOwner();
45 Inner.Unlock();
46 }
47
49 {
50 return OwnerThreadId.load(std::memory_order_relaxed) == FPlatformTLS::GetCurrentThreadId();
51 }
52
53private:
54 void SetOwner()
55 {
56 OwnerThreadId.store(FPlatformTLS::GetCurrentThreadId(), std::memory_order_relaxed);
57 }
58
59 void ClearOwner()
60 {
61 OwnerThreadId.store(0, std::memory_order_relaxed);
62 }
63
64 FCriticalSection Inner;
65 std::atomic<uint32> OwnerThreadId;
66};
67
72{
73public:
79
81 : SynchObject(InSynchObject)
82 {
83 check(SynchObject);
84 SynchObject->Lock();
85 }
86
89 {
90 Unlock();
91 }
92
93 void Unlock()
94 {
95 if (SynchObject)
96 {
97 SynchObject->Unlock();
98 SynchObject = nullptr;
99 }
100 }
101
102private:
103
104 // Holds the synchronization object to aggregate and scope manage.
105 FCriticalSectionQueryable* SynchObject;
106};
107
116{
117public:
124 {
126 return TryTakeOwnershipWithinLock();
127 }
129 {
130 return TryTakeOwnershipWithinLock();
131 }
133 {
134 return TryTakeOwnershipWithinLock();
135 }
136
143 {
145 ReleaseOwnershipCheckedWithinLock();
146 }
148 {
149 ReleaseOwnershipCheckedWithinLock();
150 }
152 {
153 ReleaseOwnershipCheckedWithinLock();
154 }
155
157 {
158 return bHasOwner;
159 }
161 {
162 return bHasOwner;
163 }
164
166 {
167 return OwnerThreadId.load(std::memory_order_relaxed) == FPlatformTLS::GetCurrentThreadId();
168 }
169
170private:
171 bool TryTakeOwnershipWithinLock()
172 {
173 if (!bHasOwner)
174 {
175 bHasOwner = true;
176 OwnerThreadId.store(FPlatformTLS::GetCurrentThreadId(), std::memory_order_relaxed);
177 return true;
178 }
179 return false;
180 }
181 void ReleaseOwnershipCheckedWithinLock()
182 {
184 bHasOwner = false;
185 OwnerThreadId.store(0, std::memory_order_relaxed);
186 }
187
188private:
190 std::atomic<uint32> OwnerThreadId{ 0 };
191 bool bHasOwner = false;
192};
#define check(expr)
Definition AssertionMacros.h:314
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
Definition CriticalSectionQueryable.h:13
void Unlock()
Definition CriticalSectionQueryable.h:41
FCriticalSectionQueryable(FCriticalSectionQueryable &&)=delete
FCriticalSectionQueryable(const FCriticalSectionQueryable &)=delete
void Lock()
Definition CriticalSectionQueryable.h:25
FCriticalSectionQueryable()
Definition CriticalSectionQueryable.h:20
FCriticalSectionQueryable & operator=(FCriticalSectionQueryable &&)=delete
bool IsLockedOnCurrentThread() const
Definition CriticalSectionQueryable.h:48
bool TryLock()
Definition CriticalSectionQueryable.h:31
FCriticalSectionQueryable & operator=(const FCriticalSectionQueryable &)=delete
Definition CriticalSectionQueryable.h:72
FScopeLockQueryable & operator=(FScopeLockQueryable &&InScopeLock)=delete
FScopeLockQueryable(const FScopeLockQueryable &InScopeLock)=delete
FScopeLockQueryable()=delete
void Unlock()
Definition CriticalSectionQueryable.h:93
~FScopeLockQueryable()
Definition CriticalSectionQueryable.h:88
FScopeLockQueryable(FCriticalSectionQueryable *InSynchObject)
Definition CriticalSectionQueryable.h:80
FScopeLockQueryable(FScopeLockQueryable &&InScopeLock)=delete
FScopeLockQueryable & operator=(FScopeLockQueryable &InScopeLock)=delete
Definition ScopeLock.h:141
Definition CriticalSectionQueryable.h:116
void ReleaseOwnershipChecked(FScopeLock &AlreadyEnteredAegisScopeLock)
Definition CriticalSectionQueryable.h:151
bool IsOwned(FScopeLock &AlreadyEnteredAegisScopeLock) const
Definition CriticalSectionQueryable.h:160
bool TryTakeOwnership(FCriticalSection &NotYetEnteredAegisLock)
Definition CriticalSectionQueryable.h:123
bool TryTakeOwnership(FScopeLock &AlreadyEnteredAegisScopeLock)
Definition CriticalSectionQueryable.h:132
bool TryTakeOwnership(FCriticalSectionQueryable &NotYetEnteredAegisLock)
Definition CriticalSectionQueryable.h:118
bool TryTakeOwnership(FScopeLockQueryable &AlreadyEnteredAegisScopeLock)
Definition CriticalSectionQueryable.h:128
void ReleaseOwnershipChecked(FCriticalSection &NotYetEnteredAegisLock)
Definition CriticalSectionQueryable.h:142
bool IsOwned(FScopeLockQueryable &AlreadyEnteredAegisScopeLock) const
Definition CriticalSectionQueryable.h:156
bool IsOwnedByCurrentThread() const
Definition CriticalSectionQueryable.h:165
void ReleaseOwnershipChecked(FScopeLockQueryable &AlreadyEnteredAegisScopeLock)
Definition CriticalSectionQueryable.h:147
void ReleaseOwnershipChecked(FCriticalSectionQueryable &NotYetEnteredAegisLock)
Definition CriticalSectionQueryable.h:137
static uint32 GetCurrentThreadId(void)
Definition AndroidPlatformTLS.h:20