UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PThreadSemaphore.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5// HEADER_UNIT_UNSUPPORTED - Unsupported platform
6
7#include "CoreTypes.h"
8#include "Misc/Timespan.h"
10#include <pthread.h>
11#include <errno.h>
12#include <semaphore.h>
13
15{
16public:
18
20 {
21 int Res = sem_init(&Semaphore, /*pshared = */ 0, InitialCount);
22 checkfSlow(Res == 0, TEXT("Failed to create semaphore (%d:%d): %d"), InitialCount, MaxCount, errno);
23 }
24
26 {
27 int Res = sem_destroy(&Semaphore);
28 checkfSlow(Res == 0, TEXT("Error destroying semaphore: %d"), errno);
29 }
30
31 void Acquire()
32 {
33 int Res = sem_wait(&Semaphore);
34 checkfSlow(Res == 0, TEXT("Acquiring semaphore failed: %d"), errno);
35 }
36
38 {
41 checkfSlow(Res == 0, TEXT("clock_gettime failed: %d"), errno);
42
43 ts.tv_sec += (uint64)Timeout.GetTotalSeconds();
44 ts.tv_nsec += Timeout.GetFractionNano();
45 Res = sem_timedwait(&Semaphore, &ts);
46 checkfSlow(Res == 0 || Res == ETIMEDOUT, TEXT("sem_timedwait failed: %d"), errno);
47 return Res == 0;
48 }
49
51 {
52 checkfSlow(Count > 0, TEXT("Releasing semaphore with Count = %d, that should be greater than 0"), Count);
53 for (int i = 0; i != Count; ++i)
54 {
55 int Res = sem_post(&Semaphore);
56 checkfSlow(Res == 0, TEXT("Releasing semaphore failed: %d"), errno);
57 }
58 }
59
60private:
61 sem_t Semaphore;
62};
63
#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
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FPThreadSemaphore FSemaphore
Definition PThreadSemaphore.h:64
Definition PThreadSemaphore.h:15
~FPThreadSemaphore()
Definition PThreadSemaphore.h:25
void Release(int32 Count=1)
Definition PThreadSemaphore.h:50
void Acquire()
Definition PThreadSemaphore.h:31
bool TryAcquire(FTimespan Timeout=FTimespan::Zero())
Definition PThreadSemaphore.h:37
UE_NONCOPYABLE(FPThreadSemaphore)
FPThreadSemaphore(int32 InitialCount, int32 MaxCount)
Definition PThreadSemaphore.h:19
Definition Timespan.h:76
static FTimespan Zero()
Definition Timespan.h:747