Skip to content

Commit

Permalink
iox-#1036 Use builder API for 'Mutex'
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Sep 2, 2023
1 parent 503a0da commit 527b9f9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ enum class MutexTryLock
class mutex
{
public:
/// @attention the construction of the mutex can fail. This can lead to a program termination!
/// @todo iox-#1036 remove this, introduced to keep current API temporarily
explicit mutex(const bool f_isRecursive) noexcept;

/// @brief Destroys the mutex. When the mutex is still locked this will fail and the
/// mutex is leaked! If the MutexThreadTerminationBehavior is set to RELEASE_WHEN_LOCKED
/// a locked mutex is unlocked and the handle is cleaned up correctly.
Expand Down
27 changes: 0 additions & 27 deletions iceoryx_hoofs/source/posix_wrapper/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,33 +279,6 @@ expected<void, MutexCreationError> MutexBuilder::create(optional<mutex>& uniniti
return ok();
}

/// @todo iox-#1036 remove this, introduced to keep current API temporarily
mutex::mutex(bool f_isRecursive) noexcept
{
pthread_mutexattr_t attr;
bool isInitialized{true};
isInitialized &= !posixCall(pthread_mutexattr_init)(&attr).returnValueMatchesErrno().evaluate().has_error();
isInitialized &= !posixCall(pthread_mutexattr_setpshared)(&attr, PTHREAD_PROCESS_SHARED)
.returnValueMatchesErrno()
.evaluate()
.has_error();
isInitialized &=
!posixCall(pthread_mutexattr_settype)(&attr, f_isRecursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL)
.returnValueMatchesErrno()
.evaluate()
.has_error();
isInitialized &= !posixCall(pthread_mutexattr_setprotocol)(&attr, PTHREAD_PRIO_NONE)
.returnValueMatchesErrno()
.evaluate()
.has_error();
isInitialized &= !posixCall(pthread_mutex_init)(&m_handle, &attr).returnValueMatchesErrno().evaluate().has_error();
isInitialized &= !posixCall(pthread_mutexattr_destroy)(&attr).returnValueMatchesErrno().evaluate().has_error();

/// NOLINTJUSTIFICATION is fixed in the PR iox-#1443
/// NOLINTNEXTLINE(hicpp-no-array-decay,cppcoreguidelines-pro-bounds-array-to-pointer-decay)
cxx::Ensures(isInitialized && "Unable to create mutex");
}

mutex::~mutex() noexcept
{
if (m_isDestructable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ namespace popo
class ThreadSafePolicy
{
public:
ThreadSafePolicy() noexcept;

// needs to be public since we want to use std::lock_guard
void lock() const noexcept;
void unlock() const noexcept;
bool tryLock() const noexcept;

private:
mutable posix::mutex m_mutex{true}; // recursive lock
mutable optional<posix::mutex> m_mutex;
};

class SingleThreadedPolicy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "iceoryx_posh/internal/runtime/shared_memory_user.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iox/function.hpp"
#include "iox/optional.hpp"

namespace iox
{
Expand Down Expand Up @@ -104,7 +105,7 @@ class PoshRuntimeImpl : public PoshRuntime
expected<popo::ConditionVariableData*, IpcMessageErrorType>
requestConditionVariableFromRoudi(const IpcMessage& sendBuffer) noexcept;

mutable posix::mutex m_appIpcRequestMutex{false};
mutable optional<posix::mutex> m_appIpcRequestMutex;

IpcRuntimeInterface m_ipcChannelInterface;
optional<SharedMemoryUser> m_ShmInterface;
Expand Down
15 changes: 12 additions & 3 deletions iceoryx_posh/source/popo/building_blocks/locking_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ namespace iox
{
namespace popo
{
ThreadSafePolicy::ThreadSafePolicy() noexcept
{
posix::MutexBuilder()
.isInterProcessCapable(true)
.mutexType(posix::MutexType::RECURSIVE)
.create(m_mutex)
.expect("Valid Mutex");
}

void ThreadSafePolicy::lock() const noexcept
{
if (!m_mutex.lock())
if (!m_mutex->lock())
{
IOX_LOG(FATAL)
<< "Locking of an inter-process mutex failed! This indicates that the application holding the lock "
Expand All @@ -36,7 +45,7 @@ void ThreadSafePolicy::lock() const noexcept

void ThreadSafePolicy::unlock() const noexcept
{
if (!m_mutex.unlock())
if (!m_mutex->unlock())
{
IOX_LOG(FATAL)
<< "Unlocking of an inter-process mutex failed! This indicates that the resources were cleaned up "
Expand All @@ -47,7 +56,7 @@ void ThreadSafePolicy::unlock() const noexcept

bool ThreadSafePolicy::tryLock() const noexcept
{
auto tryLockResult = m_mutex.try_lock();
auto tryLockResult = m_mutex->try_lock();
if (tryLockResult.has_error())
{
errorHandler(PoshError::POPO__CHUNK_TRY_LOCK_ERROR, ErrorLevel::FATAL);
Expand Down
7 changes: 6 additions & 1 deletion iceoryx_posh/source/runtime/posh_runtime_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ PoshRuntimeImpl::PoshRuntimeImpl(optional<const RuntimeName_t*> name, const Runt
m_ipcChannelInterface.getSegmentManagerAddressOffset()});
}())
{
posix::MutexBuilder()
.isInterProcessCapable(false)
.mutexType(posix::MutexType::NORMAL)
.create(m_appIpcRequestMutex)
.expect("Valid Mutex");
}

PoshRuntimeImpl::~PoshRuntimeImpl() noexcept
Expand Down Expand Up @@ -648,7 +653,7 @@ popo::ConditionVariableData* PoshRuntimeImpl::getMiddlewareConditionVariable() n
bool PoshRuntimeImpl::sendRequestToRouDi(const IpcMessage& msg, IpcMessage& answer) noexcept
{
// runtime must be thread safe
std::lock_guard<posix::mutex> g(m_appIpcRequestMutex);
std::lock_guard<posix::mutex> g(m_appIpcRequestMutex.value());
return m_ipcChannelInterface.sendRequestToRouDi(msg, answer);
}

Expand Down

0 comments on commit 527b9f9

Please sign in to comment.