-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* 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
- Loading branch information
Showing
6 changed files
with
272 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// | ||
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// | ||
// Mutex_STD.cpp | ||
// | ||
// Library: Foundation | ||
// Package: Threading | ||
// Module: Mutex | ||
// | ||
// Copyright (c) 2004-2023, Applied Informatics Software Engineering GmbH. | ||
// and Contributors. | ||
// | ||
// SPDX-License-Identifier: BSL-1.0 | ||
// | ||
|
||
|
||
#include "Poco/Mutex_STD.h" | ||
#include "Poco/Timestamp.h" | ||
#if !defined(POCO_NO_SYS_SELECT_H) | ||
#include <sys/select.h> | ||
#endif | ||
#include <unistd.h> | ||
#if defined(POCO_VXWORKS) | ||
#include <timers.h> | ||
#include <cstring> | ||
#else | ||
#include <sys/time.h> | ||
#endif | ||
|
||
|
||
namespace Poco { | ||
|
||
|
||
MutexImpl::MutexImpl() : _mutex() | ||
{ | ||
} | ||
|
||
MutexImpl::~MutexImpl() | ||
{ | ||
} | ||
|
||
|
||
bool MutexImpl::tryLockImpl(long milliseconds) | ||
{ | ||
const int sleepMillis = 5; | ||
Timestamp now; | ||
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000); | ||
do | ||
{ | ||
bool rc = false; | ||
try | ||
{ | ||
rc = _mutex.try_lock(); | ||
if (rc) | ||
return true; | ||
} | ||
catch (std::exception &ex) | ||
{ | ||
throw SystemException("cannot lock mutex", ex.what()); | ||
} | ||
#if defined(POCO_VXWORKS) | ||
struct timespec ts; | ||
ts.tv_sec = 0; | ||
ts.tv_nsec = sleepMillis*1000000; | ||
nanosleep(&ts, NULL); | ||
#else | ||
struct timeval tv; | ||
tv.tv_sec = 0; | ||
tv.tv_usec = sleepMillis * 1000; | ||
select(0, nullptr, nullptr, nullptr, &tv); | ||
#endif | ||
} | ||
while (!now.isElapsed(diff)); | ||
return false; | ||
|
||
} | ||
|
||
|
||
FastMutexImpl::FastMutexImpl(): _mutex() | ||
{ | ||
} | ||
|
||
|
||
FastMutexImpl::~FastMutexImpl() | ||
{ | ||
} | ||
|
||
bool FastMutexImpl::tryLockImpl(long milliseconds) | ||
{ | ||
const int sleepMillis = 5; | ||
Timestamp now; | ||
Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000); | ||
do | ||
{ | ||
bool rc = false; | ||
try | ||
{ | ||
rc = _mutex.try_lock(); | ||
if (rc) | ||
return true; | ||
} | ||
catch (std::exception &ex) | ||
{ | ||
throw SystemException("cannot lock mutex", ex.what()); | ||
} | ||
#if defined(POCO_VXWORKS) | ||
struct timespec ts; | ||
ts.tv_sec = 0; | ||
ts.tv_nsec = sleepMillis*1000000; | ||
nanosleep(&ts, NULL); | ||
#else | ||
struct timeval tv; | ||
tv.tv_sec = 0; | ||
tv.tv_usec = sleepMillis * 1000; | ||
select(0, NULL, NULL, NULL, &tv); | ||
#endif | ||
} | ||
while (!now.isElapsed(diff)); | ||
return false; | ||
|
||
} | ||
} // namespace Poco |