11#include <condition_variable>
21 struct EventWaitHandle::Impl {
23 std::condition_variable cv;
37 static HANDLE
OpenOrCreateWin32Event(
const std::wstring& wsName,
bool manualReset,
bool initialState,
bool openAlways,
bool& bCreatedNew) {
39 HANDLE hHandle = ::CreateEventW(NULL, manualReset ? TRUE : FALSE, initialState ? TRUE : FALSE, wsName.c_str());
40 if (hHandle != NULL) {
41 bCreatedNew = (::GetLastError() != ERROR_ALREADY_EXISTS);
44 if (::GetLastError() == ERROR_ACCESS_DENIED) {
53 hHandle = ::OpenEventW(EVENT_MODIFY_STATE | SYNCHRONIZE, FALSE, wsName.c_str());
54 if (hHandle == NULL) {
55 if (::GetLastError() == ERROR_ACCESS_DENIED) {
69 std::wstring wsName = Utils::StringConvert::Utf8ToWChar(_name.GetRawString());
70 _hHandle = OpenOrCreateWin32Event(wsName, manualReset, initialState, openAlways, bCreatedNew);
105 if (sName.
IsEmpty())
return false;
110 HANDLE h = ::OpenEventW(EVENT_MODIFY_STATE | SYNCHRONIZE, FALSE, wsName.c_str());
111 if (!h)
return false;
114 spEvt->_name = sName;
116 pResult = std::move(spEvt);
127 return (::SetEvent((HANDLE)
_hHandle) != FALSE);
130 if (!
_pImpl)
return false;
131 std::lock_guard<std::mutex> lock(
_pImpl->mutex);
145 return (::ResetEvent((HANDLE)
_hHandle) != FALSE);
148 if (!
_pImpl)
return false;
149 std::lock_guard<std::mutex> lock(
_pImpl->mutex);
154 static bool WaitForEventCv(EventWaitHandle::Impl* pImpl,
bool& bState,
bool bManualReset) {
156 std::unique_lock<std::mutex> lock(pImpl->mutex);
157 pImpl->cv.wait(lock, [&bState]() {
return bState; });
158 if (!bManualReset) bState =
false;
162 static bool WaitForEventCv(EventWaitHandle::Impl* pImpl,
bool& bState,
bool bManualReset,
int msTimeout) {
164 std::unique_lock<std::mutex> lock(pImpl->mutex);
165 bool bRes = pImpl->cv.wait_for(lock, std::chrono::milliseconds(msTimeout), [&bState]() {
return bState; });
167 if (!bManualReset) bState =
false;
177 DWORD dwWaitResult = ::WaitForSingleObject((HANDLE)
_hHandle, INFINITE);
178 return (dwWaitResult == WAIT_OBJECT_0);
181 if (!
_pImpl)
return false;
189 DWORD dwWaitResult = ::WaitForSingleObject((HANDLE)
_hHandle, (DWORD)millisecondsTimeout);
190 if (dwWaitResult == WAIT_TIMEOUT) {
193 return (dwWaitResult == WAIT_OBJECT_0);
196 if (!
_pImpl)
return false;
Represents a Unicode character code point mirroring .NET System.Char.
Represents a thread synchronization event supporting automatic and manual reset modes.
Provides reference-counted and weak pointer memory management primitives ensuring zero raw ownership.
Utility routines for high-performance UTF-8, UTF-16, and wide-character string conversions.
Defines the exception thrown when the time allotted for a process or operation has expired.
The exception that is thrown when the operating system denies access because of an I/O error or a spe...
Exception thrown when an attempt to open a named system wait handle fails.
A unified smart pointer that supports both unique and shared ownership semantics.
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
const char * GetRawString() const
bool Set()
Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
static SmartPointer< EventWaitHandle > OpenExisting(const String &sName)
Opens an existing named system event wait handle.
static bool TryOpenExisting(const String &sName, SmartPointer< EventWaitHandle > &pResult)
Opens an existing named system event wait handle, and returns a value that indicates whether the oper...
bool WaitOne() override
Blocks the current thread until the current WaitHandle receives a signal.
~EventWaitHandle() override
Destructor closing event handle.
bool Reset()
Sets the state of the event to nonsignaled, causing threads to block.
EventWaitHandle(bool initialState, bool manualReset)
Initializes a new instance of the EventWaitHandle class, specifying whether the wait handle is initia...
WaitHandleCannotBeOpenedException(const String &sMessage)
Initializes a new instance of the WaitHandleCannotBeOpenedException class with a specified error mess...
TimeoutException(const String &sMessage)
Initializes a new instance of the TimeoutException class with a specified error message.
UnauthorizedAccessException()
Initializes a new instance of the UnauthorizedAccessException class with a default message.
static std::wstring Utf8ToWChar(const char *pUtf8Str)
Converts a null-terminated UTF-8 char string into a UTF-16 std::wstring.
static HANDLE OpenOrCreateWin32Event(const std::wstring &wsName, bool manualReset, bool initialState, bool openAlways, bool &bCreatedNew)
static bool WaitForEventCv(EventWaitHandle::Impl *pImpl, bool &bState, bool bManualReset)
static bool s_dummyCreatedNew