Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
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"
7#include "Misc/AssertionMacros.h"
8#include "Windows/WindowsHWrapper.h"
9#include "Windows/AllowWindowsPlatformTypes.h"
10
11class FWindowsSemaphore
12{
13public:
14 UE_NONCOPYABLE(FWindowsSemaphore);
15
16 FWindowsSemaphore(int32 InitialCount, int32 MaxCount)
17 : Semaphore(CreateSemaphore(nullptr, InitialCount, MaxCount, nullptr))
18 {
19 checkfSlow(Semaphore, TEXT("CreateSemaphore failed: %u"), GetLastError());
20 }
21
22 ~FWindowsSemaphore()
23 {
24 CloseHandle(Semaphore);
25 }
26
27 void Acquire()
28 {
29 DWORD Res = WaitForSingleObject(Semaphore, INFINITE);
30 checkfSlow(Res == WAIT_OBJECT_0, TEXT("Acquiring semaphore failed: %d (%u)"), Res, GetLastError());
31 }
32
33 bool TryAcquire(FTimespan Timeout = FTimespan::Zero())
34 {
35 DWORD Res = WaitForSingleObject(Semaphore, (DWORD)Timeout.GetTotalMilliseconds());
36 checkfSlow(Res == WAIT_OBJECT_0 || Res == WAIT_TIMEOUT, TEXT("Acquiring semaphore failed: %d (%u)"), Res, GetLastError());
37 return Res == WAIT_OBJECT_0;
38 }
39
40 void Release(int32 Count = 1)
41 {
42 checkfSlow(Count > 0, TEXT("Releasing semaphore with Count = %d, that should be greater than 0"), Count);
43 bool bRes = ReleaseSemaphore(Semaphore, Count, nullptr);
44 checkfSlow(bRes, TEXT("Releasing semaphore for %d failed: %u"), Count, GetLastError());
45 }
46
47private:
48 HANDLE Semaphore;
49};
50
51using FSemaphore = FWindowsSemaphore;
52
53#include "Windows/HideWindowsPlatformTypes.h"
#define checkfSlow(expr, format,...)
#define UE_NONCOPYABLE(TypeName)