poco/Foundation/include/Poco/Mutex_STD.h
Alexander B c7ac8574f8
Complimentary to #3918 (std::*mutex wrapper) (#3954)
* Complimentary to #3918
I think that we can use Poco::Mutex and Poco::FastMutex as wrappers for std::recursive_mutex and std::mutex instead of replacing

For using std::*mutexes switch on cmake-option POCO_ENABLE_STD_MUTEX

* add define POCO_ENABLE_STD_MUTEX to the Config.h
remove empty if-else from CMakeLists.txt
2023-05-05 09:49:06 -05:00

136 lines
2.0 KiB
C++

//
// Mutex_STD.h
//
// Library: Foundation
// Package: Threading
// Module: Mutex
//
// Definition of the MutexImpl and FastMutexImpl classes based on Standard library mutex and recursive mutes.
//
// Copyright (c) 2004-2023, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Foundation_Mutex_STD_INCLUDED
#define Foundation_Mutex_STD_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include <mutex>
namespace Poco {
class Foundation_API MutexImpl
{
protected:
MutexImpl();
~MutexImpl();
void lockImpl();
bool tryLockImpl();
bool tryLockImpl(long milliseconds);
void unlockImpl();
private:
std::recursive_mutex _mutex;
};
class Foundation_API FastMutexImpl
{
protected:
FastMutexImpl();
~FastMutexImpl();
void lockImpl();
bool tryLockImpl();
bool tryLockImpl(long milliseconds);
void unlockImpl();
private:
std::mutex _mutex;
};
//
// inlines
//
inline void MutexImpl::lockImpl()
{
try
{
_mutex.lock();
}
catch (std::exception &ex) {
throw SystemException("cannot lock mutex", ex.what());
}
}
inline bool MutexImpl::tryLockImpl()
{
try
{
return _mutex.try_lock();
}
catch (std::exception &ex)
{
throw SystemException("cannot lock mutex", ex.what());
}
}
inline void MutexImpl::unlockImpl()
{
try
{
_mutex.unlock();
}
catch (std::exception &ex) {
throw SystemException("cannot unlock mutex");
}
}
inline void FastMutexImpl::lockImpl()
{
try
{
_mutex.lock();
}
catch (std::exception &ex) {
throw SystemException("cannot lock mutex", ex.what());
}
}
inline bool FastMutexImpl::tryLockImpl()
{
try
{
return _mutex.try_lock();
}
catch (std::exception &ex)
{
throw SystemException("cannot lock mutex", ex.what());
}
}
inline void FastMutexImpl::unlockImpl()
{
try
{
_mutex.unlock();
}
catch (std::exception &ex) {
throw SystemException("cannot unlock mutex");
}
}
} // namespace Poco
#endif //Foundation_Mutex_STD_INCLUDED