UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
WindowsSemaphore.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 "Misc/Timespan.h"
9
11{
12public:
14
16 : Semaphore(CreateSemaphore(nullptr, InitialCount, MaxCount, nullptr))
17 {
18 checkfSlow(Semaphore, TEXT("CreateSemaphore failed: %u"), GetLastError());
19 }
20
22 {
23 CloseHandle(Semaphore);
24 }
25
26 void Acquire()
27 {
28 DWORD Res = WaitForSingleObject(Semaphore, INFINITE);
29 checkfSlow(Res == WAIT_OBJECT_0, TEXT("Acquiring semaphore failed: %d (%u)"), Res, GetLastError());
30 }
31
33 {
34 DWORD Res = WaitForSingleObject(Semaphore, (DWORD)Timeout.GetTotalMilliseconds());
35 checkfSlow(Res == WAIT_OBJECT_0 || Res == WAIT_TIMEOUT, TEXT("Acquiring semaphore failed: %d (%u)"), Res, GetLastError());
36 return Res == WAIT_OBJECT_0;
37 }
38
40 {
41 checkfSlow(Count > 0, TEXT("Releasing semaphore with Count = %d, that should be greater than 0"), Count);
42 bool bRes = ReleaseSemaphore(Semaphore, Count, nullptr);
43 checkfSlow(bRes, TEXT("Releasing semaphore for %d failed: %u"), Count, GetLastError());
44 }
45
46private:
47 HANDLE Semaphore;
48};
49
51
#define checkfSlow(expr, format,...)
Definition AssertionMacros.h:333
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition PThreadSemaphore.h:15
Definition WindowsSemaphore.h:11
~FWindowsSemaphore()
Definition WindowsSemaphore.h:21
UE_NONCOPYABLE(FWindowsSemaphore)
FWindowsSemaphore(int32 InitialCount, int32 MaxCount)
Definition WindowsSemaphore.h:15
void Acquire()
Definition WindowsSemaphore.h:26
void Release(int32 Count=1)
Definition WindowsSemaphore.h:39
bool TryAcquire(FTimespan Timeout=FTimespan::Zero())
Definition WindowsSemaphore.h:32
Definition Timespan.h:76
static FTimespan Zero()
Definition Timespan.h:747