Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
PThreadSemaphore.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 <pthread.h>
9#include <errno.h>
10#include <semaphore.h>
11
13{
14public:
16
17 FPThreadSemaphore(int32 InitialCount, int32 MaxCount)
18 {
19 int Res = sem_init(&Semaphore, /*pshared = */ 0, InitialCount);
20 checkfSlow(Res == 0, TEXT("Failed to create semaphore (%d:%d): %d"), InitialCount, MaxCount, errno);
21 }
22
24 {
25 int Res = sem_destroy(&Semaphore);
26 checkfSlow(Res == 0, TEXT("Error destroying semaphore: %d"), errno);
27 }
28
29 void Acquire()
30 {
31 int Res = sem_wait(&Semaphore);
32 checkfSlow(Res == 0, TEXT("Acquiring semaphore failed: %d"), errno);
33 }
34
36 {
37 timespec ts;
38 int Res = clock_gettime(CLOCK_REALTIME, &ts);
39 checkfSlow(Res == 0, TEXT("clock_gettime failed: %d"), errno);
40
41 ts.tv_sec += (uint64)Timeout.GetTotalSeconds();
42 ts.tv_nsec += Timeout.GetFractionNano();
43 Res = sem_timedwait(&Semaphore, &ts);
44 checkfSlow(Res == 0 || Res == ETIMEDOUT, TEXT("sem_timedwait failed: %d"), errno);
45 return Res == 0;
46 }
47
48 void Release(int32 Count = 1)
49 {
50 checkfSlow(Count > 0, TEXT("Releasing semaphore with Count = %d, that should be greater than 0"), Count);
51 for (int i = 0; i != Count; ++i)
52 {
53 int Res = sem_post(&Semaphore);
54 checkfSlow(Res == 0, TEXT("Releasing semaphore failed: %d"), errno);
55 }
56 }
57
58private:
60};
61
#define checkfSlow(expr, format,...)
#define UE_NONCOPYABLE(TypeName)
FPThreadSemaphore FSemaphore
void Release(int32 Count=1)
bool TryAcquire(FTimespan Timeout=FTimespan::Zero())
FPThreadSemaphore(int32 InitialCount, int32 MaxCount)
static FTimespan Zero()
Definition Timespan.h:747