1#include <Foundation/FoundationInternal.h>
2PL_FOUNDATION_INTERNAL_HEADER
4#include <Foundation/Threading/Semaphore.h>
6#include <Foundation/Strings/StringBuilder.h>
14PL_WARNING_DISABLE_CLANG("-Wdeprecated-declarations")
20 if (m_hSemaphore.m_pNamedOrUnnamed !=
nullptr)
22 if (m_hSemaphore.m_pNamed !=
nullptr)
24 sem_close(m_hSemaphore.m_pNamed);
28 sem_destroy(&m_hSemaphore.m_Unnamed);
39 if (sem_init(&m_hSemaphore.m_Unnamed, 0, uiInitialTokenCount) != 0)
44 m_hSemaphore.m_pNamedOrUnnamed = &m_hSemaphore.m_Unnamed;
52 m_hSemaphore.m_pNamed = sem_open(sSharedName.
GetData(tmp), O_CREAT | O_EXCL, S_IRWXU | S_IRWXO | S_IRWXG, uiInitialTokenCount);
54 if (m_hSemaphore.m_pNamed ==
nullptr)
59 m_hSemaphore.m_pNamedOrUnnamed = m_hSemaphore.m_pNamed;
67 PL_ASSERT_DEV(!sSharedName.
IsEmpty(),
"Name of semaphore to open mustn't be empty.");
72 m_hSemaphore.m_pNamed = sem_open(sSharedName.
GetData(tmp), 0);
74 if (m_hSemaphore.m_pNamed ==
nullptr)
79 m_hSemaphore.m_pNamedOrUnnamed = m_hSemaphore.m_pNamed;
86 PL_VERIFY(sem_wait(m_hSemaphore.m_pNamedOrUnnamed) == 0,
"Semaphore token acquisition failed.");
91 PL_VERIFY(sem_post(m_hSemaphore.m_pNamedOrUnnamed) == 0,
"Returning a semaphore token failed, most likely due to a AcquireToken() / ReturnToken() mismatch.");
99 if (sem_trywait(m_hSemaphore.m_pNamedOrUnnamed) == 0)
A semaphore is used to synchronize threads, similar to a mutex (see plMutex).
Definition Semaphore.h:17
void ReturnToken()
Returns a single token. If another thread is currently waiting for a token, this will wake it up.
Definition Semaphore_Posix.h:89
plResult TryAcquireToken(plTime timeout=plTime::MakeZero())
Same as AcquireToken() but returns immediately with PL_FAILURE, if currently not tokens are available...
Definition Semaphore_Posix.h:94
plResult Create(plUInt32 uiInitialTokenCount=0, plStringView sSharedName=plStringView())
Attempts to create a new semaphore with an initial number of available tokens.
Definition Semaphore_Posix.h:33
void AcquireToken()
Waits until a token is available and acquires it.
Definition Semaphore_Posix.h:84
plResult Open(plStringView sSharedName)
Attempts to open an existing named semaphore.
Definition Semaphore_Posix.h:65
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
const char * GetData(plStringBuilder &ref_sTempStorage) const
Returns the data as a zero-terminated string.
Definition StringView.cpp:15
bool IsEmpty() const
Returns whether the string is an empty string.
Definition StringView_inl.h:85
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12