21 std::timed_mutex mutex;
24 Mutex::Mutex() : _name(
""), _hHandle(nullptr), _pImpl(new Impl()) {}
26 Mutex::Mutex(
bool bInitiallyOwned) : _name(
""), _hHandle(nullptr), _pImpl(new Impl()) {
28 if (bInitiallyOwned) {
41 static HANDLE
OpenOrCreateWin32Mutex(
const std::wstring& wsName,
bool bInitiallyOwned,
bool openAlways,
bool& bCreatedNew) {
43 HANDLE hHandle = ::CreateMutexW(NULL, bInitiallyOwned ? TRUE : FALSE, wsName.c_str());
44 if (hHandle != NULL) {
45 bCreatedNew = (::GetLastError() != ERROR_ALREADY_EXISTS);
48 if (::GetLastError() == ERROR_ACCESS_DENIED) {
57 hHandle = ::OpenMutexW(SYNCHRONIZE, FALSE, wsName.c_str());
58 if (hHandle == NULL) {
59 if (::GetLastError() == ERROR_ACCESS_DENIED) {
69 : _name(sName), _hHandle(nullptr), _pImpl(new Impl()) {
72 if (!_name.IsEmpty()) {
73 std::wstring wsName = Utils::StringConvert::Utf8ToWChar(_name.GetRawString());
74 _hHandle = OpenOrCreateWin32Mutex(wsName, bInitiallyOwned, openAlways, bCreatedNew);
77 if (bInitiallyOwned) {
83 if (bInitiallyOwned) {
92 if (_hHandle !=
nullptr) {
93 ::CloseHandle((HANDLE)_hHandle);
97 if (_pImpl !=
nullptr) {
115 if (sName.
IsEmpty())
return false;
120 HANDLE h = ::OpenMutexW(SYNCHRONIZE, FALSE, wsName.c_str());
121 if (h == NULL)
return false;
126 pResult = std::move(spM);
136 if (_hHandle !=
nullptr) {
137 DWORD dwWaitResult = ::WaitForSingleObject((HANDLE)_hHandle, INFINITE);
138 return (dwWaitResult == WAIT_OBJECT_0 || dwWaitResult == WAIT_ABANDONED);
141 if (_pImpl) _pImpl->mutex.lock();
148 if (_hHandle !=
nullptr) {
149 DWORD dwWaitResult = ::WaitForSingleObject((HANDLE)_hHandle, (DWORD)millisecondsTimeout);
150 if (dwWaitResult == WAIT_TIMEOUT) {
153 return (dwWaitResult == WAIT_OBJECT_0 || dwWaitResult == WAIT_ABANDONED);
156 if (!_pImpl || !_pImpl->mutex.try_lock_for(std::chrono::milliseconds(millisecondsTimeout))) {
165 if (_hHandle !=
nullptr) {
166 ::ReleaseMutex((HANDLE)_hHandle);
170 if (_pImpl) _pImpl->mutex.unlock();
Represents a Unicode character code point mirroring .NET System.Char.
A synchronization primitive that can also be used for interprocess synchronization.
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.
friend class SmartPointer
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
const char * GetRawString() const
static SmartPointer< Mutex > OpenExisting(const String &sName)
Opens an existing named system mutex.
Mutex()
Initializes a new instance of the Mutex class with default properties.
int Release(int releaseCount=1) override
Releases the Mutex once.
~Mutex() override
Destructor releasing encapsulated mutex resources.
static bool TryOpenExisting(const String &sName, SmartPointer< Mutex > &pResult)
Opens an existing named system mutex, and returns a value that indicates whether the operation succee...
bool WaitOne() override
Blocks the current thread until the current WaitHandle receives a signal.
Exception thrown when an attempt to open a system synchronization handle that does not exist fails.
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 OpenOrCreateWin32Mutex(const std::wstring &wsName, bool bInitiallyOwned, bool openAlways, bool &bCreatedNew)
static bool s_mutexDummyCreatedNew