Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1391 Move 'mutex' to new location
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Nov 29, 2023
1 parent ee16f33 commit 2699a3b
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 74 deletions.
6 changes: 3 additions & 3 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,9 @@
myMutex.lock();

// after
iox::optional<mutex> myMutex;
iox::posix::MutexBuilder()
.mutexType(iox::posix::MutexType::RECURSIVE)
iox::optional<iox::mutex> myMutex;
iox::MutexBuilder()
.mutexType(iox::MutexType::RECURSIVE)
.create(myMutex);
myMutex->lock();
```
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ iox_add_library(
source/cxx/requires.cpp
source/error_handling/error_handler.cpp
source/error_handling/error_handling.cpp
source/posix_wrapper/mutex.cpp
source/posix_wrapper/scheduler.cpp
source/posix_wrapper/shared_memory_object.cpp
source/posix_wrapper/shared_memory_object/memory_map.cpp
Expand All @@ -122,6 +121,7 @@ iox_add_library(
posix/filesystem/source/file.cpp
posix/filesystem/source/file_lock.cpp
posix/filesystem/source/posix_acl.cpp
posix/sync/source/mutex.cpp
posix/sync/source/named_semaphore.cpp
posix/sync/source/semaphore_interface.cpp
posix/sync/source/thread.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_POSIX_WRAPPER_MUTEX_HPP
#define IOX_HOOFS_POSIX_WRAPPER_MUTEX_HPP

#ifndef IOX_HOOFS_POSIX_SYNC_MUTEX_HPP
#define IOX_HOOFS_POSIX_SYNC_MUTEX_HPP

#include "iceoryx_platform/pthread.hpp"
#include "iox/builder.hpp"
Expand All @@ -24,8 +25,6 @@

namespace iox
{
namespace posix
{
enum class MutexCreationError
{
MUTEX_ALREADY_INITIALIZED,
Expand Down Expand Up @@ -71,23 +70,23 @@ enum class MutexTryLock
/// @brief Wrapper for a inter-process pthread based mutex which does not use
/// exceptions!
/// @code
/// #include "iceoryx_hoofs/internal/posix_wrapper/mutex.hpp"
/// #include "iox/mutex.hpp"
///
/// int main() {
/// optional<iox::posix::Mutex> myMutex;
/// iox::posix::MutexBuilder().isInterProcessCapable(true)
/// .mutexType(MutexType::RECURSIVE)
/// .priorityInheritance(MutexPriorityInheritance::NONE)
/// .threadTerminationBehavior(MutexThreadTerminationBehavior::RELEASE_WHEN_LOCKED)
/// .create(myMutex)
/// .expect("Failed to create mutex!");
/// optional<iox::Mutex> myMutex;
/// iox::MutexBuilder().isInterProcessCapable(true)
/// .mutexType(MutexType::RECURSIVE)
/// .priorityInheritance(MutexPriorityInheritance::NONE)
/// .threadTerminationBehavior(MutexThreadTerminationBehavior::RELEASE_WHEN_LOCKED)
/// .create(myMutex)
/// .expect("Failed to create mutex!");
///
/// myMutex->lock().expect("Mutex lock failed. Maybe the system is corrupted.");
/// // ... do stuff
/// myMutex->unlock().expect("Mutex unlock failed. Maybe the system is corrupted.");
///
/// {
/// std::lock_guard<posix::mutex> lock(*myMutex);
/// std::lock_guard<mutex> lock(*myMutex);
/// // ...
/// }
///
Expand Down Expand Up @@ -220,7 +219,6 @@ class MutexBuilder
/// @return On failure MutexError which explains the error
expected<void, MutexCreationError> create(optional<mutex>& uninitializedMutex) noexcept;
};
} // namespace posix
} // namespace iox

#endif // IOX_HOOFS_POSIX_WRAPPER_MUTEX_HPP
#endif // IOX_HOOFS_POSIX_SYNC_MUTEX_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/internal/posix_wrapper/mutex.hpp"
#include "iox/mutex.hpp"
#include "iceoryx_hoofs/posix_wrapper/scheduler.hpp"
#include "iox/logging.hpp"
#include "iox/posix_call.hpp"
Expand All @@ -24,8 +24,6 @@

namespace iox
{
namespace posix
{
/// @brief Internal struct used during mutex construction to handle all the mutex attribute settings
struct MutexAttributes
{
Expand Down Expand Up @@ -157,8 +155,8 @@ struct MutexAttributes
return err(MutexCreationError::PRIORITIES_UNSUPPORTED_BY_PLATFORM);
case EINVAL:
{
auto minimumPriority = getSchedulerPriorityMinimum(Scheduler::FIFO);
auto maximumPriority = getSchedulerPriorityMaximum(Scheduler::FIFO);
auto minimumPriority = posix::getSchedulerPriorityMinimum(posix::Scheduler::FIFO);
auto maximumPriority = posix::getSchedulerPriorityMaximum(posix::Scheduler::FIFO);

IOX_LOG(ERROR,
"The priority ceiling \"" << priorityCeiling << "\" is not in the valid priority range [ "
Expand Down Expand Up @@ -411,5 +409,4 @@ expected<MutexTryLock, MutexTryLockError> mutex::try_lock() noexcept

return (result->errnum == EBUSY) ? ok(MutexTryLock::FAILED_TO_ACQUIRE_LOCK) : ok(MutexTryLock::LOCK_SUCCEEDED);
}
} // namespace posix
} // namespace iox
67 changes: 31 additions & 36 deletions iceoryx_hoofs/test/moduletests/test_posix_mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/internal/posix_wrapper/mutex.hpp"
#include "iceoryx_hoofs/testing/test.hpp"
#include "iceoryx_hoofs/testing/watch_dog.hpp"
#include "iox/deadline_timer.hpp"
#include "iox/mutex.hpp"

#include <atomic>
#include <thread>

namespace
{
using namespace ::testing;
using namespace iox;
using namespace iox::units::duration_literals;

class Mutex_test : public Test
Expand All @@ -35,10 +36,8 @@ class Mutex_test : public Test
{
deadlockWatchdog.watchAndActOnFailure([] { std::terminate(); });

ASSERT_FALSE(
iox::posix::MutexBuilder().mutexType(iox::posix::MutexType::RECURSIVE).create(sutRecursive).has_error());
ASSERT_FALSE(
iox::posix::MutexBuilder().mutexType(iox::posix::MutexType::NORMAL).create(sutNonRecursive).has_error());
ASSERT_FALSE(MutexBuilder().mutexType(MutexType::RECURSIVE).create(sutRecursive).has_error());
ASSERT_FALSE(MutexBuilder().mutexType(MutexType::NORMAL).create(sutNonRecursive).has_error());
}

void TearDown() override
Expand All @@ -65,8 +64,8 @@ class Mutex_test : public Test
}

std::atomic_bool doWaitForThread{true};
iox::optional<iox::posix::mutex> sutNonRecursive;
iox::optional<iox::posix::mutex> sutRecursive;
iox::optional<mutex> sutNonRecursive;
iox::optional<mutex> sutRecursive;
iox::units::Duration watchdogTimeout = 5_s;
Watchdog deadlockWatchdog{watchdogTimeout};
};
Expand All @@ -76,7 +75,7 @@ TEST_F(Mutex_test, TryLockAndUnlockWithNonRecursiveMutexWorks)
::testing::Test::RecordProperty("TEST_ID", "4ed2c3f1-6c91-465e-a702-9ea25b5434bb");
auto tryLockResult = sutNonRecursive->try_lock();
ASSERT_FALSE(tryLockResult.has_error());
EXPECT_THAT(*tryLockResult, Eq(iox::posix::MutexTryLock::LOCK_SUCCEEDED));
EXPECT_THAT(*tryLockResult, Eq(MutexTryLock::LOCK_SUCCEEDED));
EXPECT_FALSE(sutNonRecursive->unlock().has_error());
}

Expand All @@ -87,7 +86,7 @@ TEST_F(Mutex_test, TryLockWithNonRecursiveMutexReturnsFailsWhenLocked)
EXPECT_FALSE(sutNonRecursive->lock().has_error());
auto tryLockResult = sutNonRecursive->try_lock();
ASSERT_FALSE(tryLockResult.has_error());
EXPECT_THAT(*tryLockResult, Eq(iox::posix::MutexTryLock::FAILED_TO_ACQUIRE_LOCK));
EXPECT_THAT(*tryLockResult, Eq(MutexTryLock::FAILED_TO_ACQUIRE_LOCK));
EXPECT_FALSE(sutNonRecursive->unlock().has_error());
}
#endif
Expand All @@ -108,9 +107,9 @@ TEST_F(Mutex_test, RepeatedLockAndUnlockWithNonRecursiveMutexWorks)
EXPECT_FALSE(sutNonRecursive->unlock().has_error());
}

void tryLockReturnsFalseWhenMutexLockedInOtherThread(iox::posix::mutex& mutex)
void tryLockReturnsFalseWhenMutexLockedInOtherThread(mutex& mutex)
{
std::atomic<iox::posix::MutexTryLock> tryLockState = {iox::posix::MutexTryLock::LOCK_SUCCEEDED};
std::atomic<MutexTryLock> tryLockState = {MutexTryLock::LOCK_SUCCEEDED};
ASSERT_FALSE(mutex.lock().has_error());
std::thread lockThread([&] {
auto tryLockResult = mutex.try_lock();
Expand All @@ -119,7 +118,7 @@ void tryLockReturnsFalseWhenMutexLockedInOtherThread(iox::posix::mutex& mutex)
});

lockThread.join();
EXPECT_THAT(tryLockState.load(), Eq(iox::posix::MutexTryLock::FAILED_TO_ACQUIRE_LOCK));
EXPECT_THAT(tryLockState.load(), Eq(MutexTryLock::FAILED_TO_ACQUIRE_LOCK));

ASSERT_FALSE(mutex.unlock().has_error());
}
Expand All @@ -136,7 +135,7 @@ TEST_F(Mutex_test, TryLockReturnsFalseWhenMutexLockedInOtherThreadRecursiveMutex
tryLockReturnsFalseWhenMutexLockedInOtherThread(*sutRecursive);
}

void lockedMutexBlocks(Mutex_test* test, iox::posix::mutex& mutex)
void lockedMutexBlocks(Mutex_test* test, mutex& mutex)
{
const std::chrono::milliseconds WAIT_IN_MS(100);

Expand Down Expand Up @@ -175,13 +174,12 @@ TEST_F(Mutex_test, LockedMutexBlocksRecursiveMutex)
TEST_F(Mutex_test, MutexWithDeadlockDetectionsFailsOnDeadlock)
{
::testing::Test::RecordProperty("TEST_ID", "feb07935-674d-4ebc-abaa-66664751719a");
iox::optional<iox::posix::mutex> sut;
ASSERT_FALSE(
iox::posix::MutexBuilder().mutexType(iox::posix::MutexType::WITH_DEADLOCK_DETECTION).create(sut).has_error());
iox::optional<mutex> sut;
ASSERT_FALSE(MutexBuilder().mutexType(MutexType::WITH_DEADLOCK_DETECTION).create(sut).has_error());
EXPECT_FALSE(sut->lock().has_error());
auto result = sut->lock();
ASSERT_TRUE(result.has_error());
EXPECT_THAT(result.error(), Eq(iox::posix::MutexLockError::DEADLOCK_CONDITION));
EXPECT_THAT(result.error(), Eq(MutexLockError::DEADLOCK_CONDITION));

EXPECT_FALSE(sut->unlock().has_error());
}
Expand All @@ -190,29 +188,27 @@ TEST_F(Mutex_test, MutexWithDeadlockDetectionsFailsOnDeadlock)
TEST_F(Mutex_test, MutexWithDeadlockDetectionsFailsWhenSameThreadTriesToUnlockItTwice)
{
::testing::Test::RecordProperty("TEST_ID", "062e411e-a5d3-4759-9faf-db6f4129d395");
iox::optional<iox::posix::mutex> sut;
ASSERT_FALSE(
iox::posix::MutexBuilder().mutexType(iox::posix::MutexType::WITH_DEADLOCK_DETECTION).create(sut).has_error());
iox::optional<mutex> sut;
ASSERT_FALSE(MutexBuilder().mutexType(MutexType::WITH_DEADLOCK_DETECTION).create(sut).has_error());
EXPECT_FALSE(sut->lock().has_error());
EXPECT_FALSE(sut->unlock().has_error());

auto result = sut->unlock();
ASSERT_TRUE(result.has_error());
EXPECT_THAT(result.error(), Eq(iox::posix::MutexUnlockError::NOT_OWNED_BY_THREAD));
EXPECT_THAT(result.error(), Eq(MutexUnlockError::NOT_OWNED_BY_THREAD));
}

TEST_F(Mutex_test, MutexWithDeadlockDetectionsFailsWhenAnotherThreadTriesToUnlock)
{
::testing::Test::RecordProperty("TEST_ID", "4dcea981-2259-48c6-bf27-7839ad9013b4");
iox::optional<iox::posix::mutex> sut;
ASSERT_FALSE(
iox::posix::MutexBuilder().mutexType(iox::posix::MutexType::WITH_DEADLOCK_DETECTION).create(sut).has_error());
iox::optional<mutex> sut;
ASSERT_FALSE(MutexBuilder().mutexType(MutexType::WITH_DEADLOCK_DETECTION).create(sut).has_error());
EXPECT_FALSE(sut->lock().has_error());

std::thread t([&] {
auto result = sut->unlock();
ASSERT_TRUE(result.has_error());
EXPECT_THAT(result.error(), Eq(iox::posix::MutexUnlockError::NOT_OWNED_BY_THREAD));
EXPECT_THAT(result.error(), Eq(MutexUnlockError::NOT_OWNED_BY_THREAD));
});
t.join();
EXPECT_FALSE(sut->unlock().has_error());
Expand All @@ -226,9 +222,9 @@ TEST_F(Mutex_test,
#if defined(QNX) || defined(__QNX) || defined(__QNX__) || defined(QNX__)
GTEST_SKIP() << "iox-#1683 QNX supports robust mutex not like the posix standard describes them.";
#endif
iox::optional<iox::posix::mutex> sut;
ASSERT_FALSE(iox::posix::MutexBuilder()
.threadTerminationBehavior(iox::posix::MutexThreadTerminationBehavior::RELEASE_WHEN_LOCKED)
iox::optional<mutex> sut;
ASSERT_FALSE(MutexBuilder()
.threadTerminationBehavior(MutexThreadTerminationBehavior::RELEASE_WHEN_LOCKED)
.create(sut)
.has_error());

Expand All @@ -237,8 +233,7 @@ TEST_F(Mutex_test,

auto result = sut->try_lock();
ASSERT_TRUE(result.has_error());
EXPECT_THAT(result.error(),
iox::posix::MutexTryLockError::LOCK_ACQUIRED_BUT_HAS_INCONSISTENT_STATE_SINCE_OWNER_DIED);
EXPECT_THAT(result.error(), MutexTryLockError::LOCK_ACQUIRED_BUT_HAS_INCONSISTENT_STATE_SINCE_OWNER_DIED);
sut->make_consistent();
EXPECT_FALSE(sut->unlock().has_error());
}
Expand All @@ -250,9 +245,9 @@ TEST_F(Mutex_test, MutexWithStallWhenLockedBehaviorDoesntUnlockMutexWhenThreadTe
#if defined(QNX) || defined(__QNX) || defined(__QNX__) || defined(QNX__)
GTEST_SKIP() << "iox-#1683 QNX supports robust mutex not like the posix standard describes them.";
#endif
iox::optional<iox::posix::mutex> sut;
ASSERT_FALSE(iox::posix::MutexBuilder()
.threadTerminationBehavior(iox::posix::MutexThreadTerminationBehavior::STALL_WHEN_LOCKED)
iox::optional<mutex> sut;
ASSERT_FALSE(MutexBuilder()
.threadTerminationBehavior(MutexThreadTerminationBehavior::STALL_WHEN_LOCKED)
.create(sut)
.has_error());

Expand All @@ -261,17 +256,17 @@ TEST_F(Mutex_test, MutexWithStallWhenLockedBehaviorDoesntUnlockMutexWhenThreadTe

auto result = sut->try_lock();
ASSERT_FALSE(result.has_error());
EXPECT_THAT(*result, iox::posix::MutexTryLock::FAILED_TO_ACQUIRE_LOCK);
EXPECT_THAT(*result, MutexTryLock::FAILED_TO_ACQUIRE_LOCK);
}
#endif
#endif

TEST_F(Mutex_test, InitializingMutexTwiceResultsInError)
{
::testing::Test::RecordProperty("TEST_ID", "2f26c05f-08e5-481f-8a6e-2ceca3067cf0");
auto result = iox::posix::MutexBuilder().create(sutRecursive);
auto result = MutexBuilder().create(sutRecursive);

ASSERT_THAT(result.has_error(), Eq(true));
EXPECT_THAT(result.error(), Eq(iox::posix::MutexCreationError::MUTEX_ALREADY_INITIALIZED));
EXPECT_THAT(result.error(), Eq(MutexCreationError::MUTEX_ALREADY_INITIALIZED));
}
} // namespace
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
#ifndef IOX_POSH_POPO_BUILDING_BLOCKS_CHUNK_DISTRIBUTOR_DATA_HPP
#define IOX_POSH_POPO_BUILDING_BLOCKS_CHUNK_DISTRIBUTOR_DATA_HPP

#include "iceoryx_hoofs/internal/posix_wrapper/mutex.hpp"
#include "iceoryx_posh/error_handling/error_handling.hpp"
#include "iceoryx_posh/internal/mepoo/shm_safe_unmanaged_chunk.hpp"
#include "iceoryx_posh/internal/popo/building_blocks/chunk_queue_pusher.hpp"
#include "iceoryx_posh/popo/port_queue_policies.hpp"
#include "iox/algorithm.hpp"
#include "iox/logging.hpp"
#include "iox/mutex.hpp"
#include "iox/relative_pointer.hpp"
#include "iox/vector.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#ifndef IOX_POSH_POPO_BUILDING_BLOCKS_LOCKING_POLICY_HPP
#define IOX_POSH_POPO_BUILDING_BLOCKS_LOCKING_POLICY_HPP

#include "iceoryx_hoofs/internal/posix_wrapper/mutex.hpp"
#include "iox/mutex.hpp"

namespace iox
{
Expand All @@ -33,7 +33,7 @@ class ThreadSafePolicy
bool tryLock() const noexcept;

private:
mutable optional<posix::mutex> m_mutex;
mutable optional<mutex> m_mutex;
};

class SingleThreadedPolicy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#define IOX_POSH_RUNTIME_POSH_RUNTIME_IMPL_HPP

#include "iceoryx_hoofs/internal/concurrent/periodic_task.hpp"
#include "iceoryx_hoofs/internal/posix_wrapper/mutex.hpp"
#include "iceoryx_posh/internal/runtime/heartbeat.hpp"
#include "iceoryx_posh/internal/runtime/shared_memory_user.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iox/function.hpp"
#include "iox/mutex.hpp"
#include "iox/optional.hpp"

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

mutable optional<posix::mutex> m_appIpcRequestMutex;
mutable optional<mutex> m_appIpcRequestMutex;

IpcRuntimeInterface m_ipcChannelInterface;
optional<SharedMemoryUser> m_ShmInterface;
Expand Down
Loading

0 comments on commit 2699a3b

Please sign in to comment.