UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
WordMutex.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"
7#include <atomic>
8
9#define UE_API CORE_API
10
11namespace UE
12{
13
20class FWordMutex final
21{
22public:
23 constexpr FWordMutex() = default;
24
25 FWordMutex(const FWordMutex&) = delete;
26 FWordMutex& operator=(const FWordMutex&) = delete;
27
28 [[nodiscard]] inline bool TryLock()
29 {
30 UPTRINT Expected = 0;
31 return State.compare_exchange_strong(Expected, IsLockedFlag, std::memory_order_acquire, std::memory_order_relaxed);
32 }
33
34 inline void Lock()
35 {
36 UPTRINT Expected = 0;
37 if (LIKELY(State.compare_exchange_weak(Expected, IsLockedFlag, std::memory_order_acquire, std::memory_order_relaxed)))
38 {
39 return;
40 }
41
42 LockSlow();
43 }
44
45 inline void Unlock()
46 {
47 // Unlock immediately to allow other threads to acquire the lock while this thread looks for a thread to wake.
48 UPTRINT CurrentState = State.fetch_sub(IsLockedFlag, std::memory_order_release);
49 checkSlow(CurrentState & IsLockedFlag);
50
51 // An empty queue indicates that there are no threads to wake.
52 const bool bQueueEmpty = !(CurrentState & QueueMask);
53 // A locked queue indicates that another thread is looking for a thread to wake.
54 const bool bQueueLocked = (CurrentState & IsQueueLockedFlag);
55
57 {
58 return;
59 }
60
61 UnlockSlow(CurrentState);
62 }
63
64private:
65 UE_API void LockSlow();
66 UE_API void UnlockSlow(UPTRINT CurrentState);
67
68 static constexpr UPTRINT IsLockedFlag = 1 << 0;
69 static constexpr UPTRINT IsQueueLockedFlag = 1 << 1;
70 static constexpr UPTRINT QueueMask = ~(IsLockedFlag | IsQueueLockedFlag);
71
72 std::atomic<UPTRINT> State = 0;
73};
74
75} // UE
76
77#undef UE_API
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define LIKELY(x)
Definition CityHash.cpp:107
FPlatformTypes::UPTRINT UPTRINT
An unsigned integer the same size as a pointer.
Definition Platform.h:1146
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UE_API
Definition SColorGradingComponentViewer.h:12
Definition WordMutex.h:21
FWordMutex(const FWordMutex &)=delete
bool TryLock()
Definition WordMutex.h:28
FWordMutex & operator=(const FWordMutex &)=delete
void Unlock()
Definition WordMutex.h:45
constexpr FWordMutex()=default
void Lock()
Definition WordMutex.h:34
Definition AdvancedWidgetsModule.cpp:13