Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1391 Move 'smart_lock' to new location
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Jan 2, 2024
1 parent 23d20e5 commit e003168
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 42 deletions.
2 changes: 0 additions & 2 deletions .clang-tidy-diff-scans.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
./iceoryx_hoofs/memory/**/*
./iceoryx_hoofs/test/moduletests/test_memory_*

./iceoryx_hoofs/include/iceoryx_hoofs/internal/concurrent/*

./iceoryx_hoofs/testing/include/iceoryx_hoofs/testing/mocks/logger_mock.hpp
./iceoryx_hoofs/testing/include/iceoryx_hoofs/testing/fatal_failure*
./iceoryx_hoofs/testing/include/iceoryx_hoofs/testing/testing_logger.hpp
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ The module structure is a logical grouping. It is replicated for `concurrent` an
| class | internal | description |
|:---------------------:|:--------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|`PeriodicTask` | i | Periodically executes a callable specified by the template parameter in a configurable time interval. |
|`smart_lock` | i | Creates arbitrary thread-safe constructs which then can be used like smart pointers. If some STL type should be thread safe use the smart_lock to create the thread safe version in one line. Based on some ideas presented in [Wrapping C++ Member Function Calls](https://stroustrup.com/wrapper.pdf) |
|`smart_lock` | | Creates arbitrary thread-safe constructs which then can be used like smart pointers. If some STL type should be thread safe use the smart_lock to create the thread safe version in one line. Based on some ideas presented in [Wrapping C++ Member Function Calls](https://stroustrup.com/wrapper.pdf) |
|`mutex` | i | Mutex interface, see [ManPage pthread_mutex_lock](https://man7.org/linux/man-pages/man3/pthread_mutex_lock.3p.html). |
|`UnnamedSemaphore` | | Unamed semaphore interface, see [ManPage sem_overview](https://man7.org/linux/man-pages/man7/sem_overview.7.html) |
|`NamedSemaphore` | | Named semaphore interface, see [ManPage sem_overview](https://man7.org/linux/man-pages/man7/sem_overview.7.html) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_CONCURRENT_SMART_LOCK_INL
#define IOX_HOOFS_CONCURRENT_SMART_LOCK_INL

#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
#ifndef IOX_HOOFS_CONCURRENT_SYNC_SMART_LOCK_INL
#define IOX_HOOFS_CONCURRENT_SYNC_SMART_LOCK_INL

#include "iox/smart_lock.hpp"

namespace iox
{
Expand Down Expand Up @@ -98,27 +99,47 @@ inline const typename smart_lock<T, MutexType>::Proxy smart_lock<T, MutexType>::
}

template <typename T, typename MutexType>
inline typename smart_lock<T, MutexType>::Proxy smart_lock<T, MutexType>::getScopeGuard() noexcept
inline typename smart_lock<T, MutexType>::Proxy smart_lock<T, MutexType>::get_scope_guard() noexcept
{
return Proxy(base, lock);
}

template <typename T, typename MutexType>
// const return type improves const correctness, operator-> can be chained with underlying operator->
// NOLINTNEXTLINE(readability-const-return-type)
inline const typename smart_lock<T, MutexType>::Proxy smart_lock<T, MutexType>::getScopeGuard() const noexcept
inline const typename smart_lock<T, MutexType>::Proxy smart_lock<T, MutexType>::get_scope_guard() const noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) const_cast to avoid code duplication
return const_cast<smart_lock<T, MutexType>*>(this)->getScopeGuard();
return const_cast<smart_lock<T, MutexType>*>(this)->get_scope_guard();
}

template <typename T, typename MutexType>
inline T smart_lock<T, MutexType>::getCopy() const noexcept
inline typename smart_lock<T, MutexType>::Proxy smart_lock<T, MutexType>::getScopeGuard() noexcept
{
return get_scope_guard();
}

template <typename T, typename MutexType>
// const return type improves const correctness, operator-> can be chained with underlying operator->
// NOLINTNEXTLINE(readability-const-return-type)
inline const typename smart_lock<T, MutexType>::Proxy smart_lock<T, MutexType>::getScopeGuard() const noexcept
{
return get_scope_guard();
}

template <typename T, typename MutexType>
inline T smart_lock<T, MutexType>::get_copy() const noexcept
{
std::lock_guard<MutexType> guard(lock);
return base;
}

template <typename T, typename MutexType>
inline T smart_lock<T, MutexType>::getCopy() const noexcept
{
return get_copy();
}

// PROXY OBJECT

template <typename T, typename MutexType>
Expand Down Expand Up @@ -163,4 +184,4 @@ inline const T& smart_lock<T, MutexType>::Proxy::operator*() const noexcept
} // namespace concurrent
} // namespace iox

#endif // IOX_HOOFS_CONCURRENT_SMART_LOCK_INL
#endif // IOX_HOOFS_CONCURRENT_SYNC_SMART_LOCK_INL
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_CONCURRENT_SMART_LOCK_HPP
#define IOX_HOOFS_CONCURRENT_SMART_LOCK_HPP

#ifndef IOX_HOOFS_CONCURRENT_SYNC_SMART_LOCK_HPP
#define IOX_HOOFS_CONCURRENT_SYNC_SMART_LOCK_HPP

#include "iox/detail/deprecation_marker.hpp"
#include <mutex>

namespace iox
Expand Down Expand Up @@ -47,7 +49,7 @@ constexpr ForwardArgsToCTor_t ForwardArgsToCTor{};
/// size_t vectorSize = threadSafeVector->size();
///
/// {
/// auto guardedVector = threadSafeVector.getScopeGuard();
/// auto guardedVector = threadSafeVector.get_scope_guard();
/// auto iter = std::find(guardVector->begin(), guardVector->end(), 456);
/// if (iter != guardVector->end()) guardVector->erase(iter);
/// }
Expand Down Expand Up @@ -134,12 +136,15 @@ class smart_lock
/// // since it would lead to a deadlock.
/// // You access the underlying object by using the vectorGuard object!
/// {
/// auto vectorGuard = threadSafeVector.getScopeGuard();
/// auto vectorGuard = threadSafeVector.get_scope_guard();
/// auto iter = std::find(vectorGuard->begin(), vectorGuard->end(),
/// 123);
/// if ( iter != vectorGuard->end() )
/// vectorGuard->erase(iter);
/// }
Proxy get_scope_guard() noexcept;

IOX_DEPRECATED_SINCE(3, "Please use 'get_scope_guard' instead.")
Proxy getScopeGuard() noexcept;

/// @brief If you need to lock your object over multiple method calls you
Expand All @@ -157,15 +162,21 @@ class smart_lock
/// // since it would lead to a deadlock.
/// // You access the underlying object by using the vectorGuard object!
/// {
/// auto vectorGuard = threadSafeVector.getScopeGuard();
/// auto vectorGuard = threadSafeVector.get_scope_guard();
/// auto iter = std::find(vectorGuard->begin(), vectorGuard->end(),
/// 123);
/// if ( iter != vectorGuard->end() )
/// vectorGuard->erase(iter);
/// }
const Proxy get_scope_guard() const noexcept;

IOX_DEPRECATED_SINCE(3, "Please use 'get_scope_guard' instead.")
const Proxy getScopeGuard() const noexcept;

/// @brief Returns a copy of the underlying object
T get_copy() const noexcept;

IOX_DEPRECATED_SINCE(3, "Please use 'get_copy' instead.")
T getCopy() const noexcept;

private:
Expand All @@ -175,6 +186,6 @@ class smart_lock
} // namespace concurrent
} // namespace iox

#include "iceoryx_hoofs/internal/concurrent/smart_lock.inl"
#include "iox/detail/smart_lock.inl"

#endif // IOX_HOOFS_CONCURRENT_SMART_LOCK_HPP
#endif // IOX_HOOFS_CONCURRENT_SYNC_SMART_LOCK_HPP
18 changes: 9 additions & 9 deletions iceoryx_hoofs/test/moduletests/test_concurrent_smart_lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
#include "iceoryx_hoofs/testing/watch_dog.hpp"
#include "iox/optional.hpp"
#include "iox/smart_lock.hpp"
#include "iox/vector.hpp"
#include "test.hpp"

Expand Down Expand Up @@ -246,7 +246,7 @@ TEST_F(smart_lock_test, AccessThroughConstScopeGuardWorks)
::testing::Test::RecordProperty("TEST_ID", "f52e9b24-0819-487b-947a-5f18d24b55e6");
constexpr int32_t CTOR_VALUE = 6212818;
const SutType_t constSut(ForwardArgsToCTor, CTOR_VALUE);
const auto guard = constSut.getScopeGuard();
const auto guard = constSut.get_scope_guard();

EXPECT_THAT(guard->getA(), Eq(CTOR_VALUE));
}
Expand All @@ -256,7 +256,7 @@ TEST_F(smart_lock_test, AccessThroughScopeGuardWorks)
::testing::Test::RecordProperty("TEST_ID", "9d91a04f-d84e-4fa3-9ee0-95ebc4bfbed1");
constexpr int32_t CTOR_VALUE = 62818;
SutType_t Sut(ForwardArgsToCTor, CTOR_VALUE);
auto guard = Sut.getScopeGuard();
auto guard = Sut.get_scope_guard();

EXPECT_THAT(guard->getA(), Eq(CTOR_VALUE));
}
Expand All @@ -266,7 +266,7 @@ TEST_F(smart_lock_test, AccessViaConstDereferenceOperatorWorks)
::testing::Test::RecordProperty("TEST_ID", "43db4ae1-86ad-4d36-93c4-c38f1faf68b5");
constexpr int32_t CTOR_VALUE = 8182126;
const SutType_t constSut(ForwardArgsToCTor, CTOR_VALUE);
const auto guard = constSut.getScopeGuard();
const auto guard = constSut.get_scope_guard();

EXPECT_THAT((*guard).getA(), Eq(CTOR_VALUE));
}
Expand All @@ -276,7 +276,7 @@ TEST_F(smart_lock_test, AccessViaDereferenceOperatorWorks)
::testing::Test::RecordProperty("TEST_ID", "45cccede-a609-493d-a285-20c1dfb77714");
constexpr int32_t CTOR_VALUE = 81826;
SutType_t Sut(ForwardArgsToCTor, CTOR_VALUE);
auto guard = Sut.getScopeGuard();
auto guard = Sut.get_scope_guard();

EXPECT_THAT((*guard).getA(), Eq(CTOR_VALUE));
}
Expand All @@ -287,7 +287,7 @@ TEST_F(smart_lock_test, AcquiringCopyWorks)
constexpr int32_t CTOR_VALUE = 628189;
m_sut.emplace(ForwardArgsToCTor, CTOR_VALUE);

EXPECT_THAT(m_sut->getCopy().getA(), Eq(CTOR_VALUE));
EXPECT_THAT(m_sut->get_copy().getA(), Eq(CTOR_VALUE));
}

//////////////////////////////////////
Expand Down Expand Up @@ -348,7 +348,7 @@ TEST_F(smart_lock_test, ThreadSafeAccessThroughScopedGuard)
{
::testing::Test::RecordProperty("TEST_ID", "2ffb9ba4-4df3-4851-8976-20f15a3bfa4b");
threadSafeOperationTest(this, [=] {
auto guard = (*m_sut).getScopeGuard();
auto guard = (*m_sut).get_scope_guard();
guard->incrementA();
});
EXPECT_THAT((*m_sut)->getA(), Eq(NUMBER_OF_RUNS_PER_THREAD * NUMBER_OF_THREADS));
Expand All @@ -358,8 +358,8 @@ TEST_F(smart_lock_test, ThreadSafeAccessThroughConstScopedGuard)
{
::testing::Test::RecordProperty("TEST_ID", "dc2efd41-4c0a-4ac0-93ed-f8e9195e0dfd");
threadSafeOperationTest(this, [=] {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) used to explitly test const getScopeGuard()
auto guard = const_cast<const SutType_t&>(*m_sut).getScopeGuard();
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) used to explitly test const get_scope_guard()
auto guard = const_cast<const SutType_t&>(*m_sut).get_scope_guard();
guard->incrementA();
});
EXPECT_THAT((*m_sut)->getA(), Eq(NUMBER_OF_RUNS_PER_THREAD * NUMBER_OF_THREADS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#ifndef IOX_HOOFS_TESTING_TESTING_LOGGER_HPP
#define IOX_HOOFS_TESTING_TESTING_LOGGER_HPP

#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
#include "iox/log/logger.hpp"
#include "iox/smart_lock.hpp"

#include "test.hpp"

Expand Down
4 changes: 2 additions & 2 deletions iceoryx_hoofs/testing/testing_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void TestingLogger::clearLogBuffer() noexcept

void TestingLogger::printLogBuffer() noexcept
{
auto loggerData = m_loggerData.getScopeGuard();
auto loggerData = m_loggerData.get_scope_guard();
if (loggerData->buffer.empty())
{
return;
Expand Down Expand Up @@ -101,7 +101,7 @@ void TestingLogger::checkLogMessageIfLogLevelIsSupported(

void TestingLogger::flush() noexcept
{
auto loggerData = m_loggerData.getScopeGuard();
auto loggerData = m_loggerData.get_scope_guard();
const auto logBuffer = Base::getLogBuffer();
loggerData->buffer.emplace_back(logBuffer.buffer, logBuffer.writeIndex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#ifndef IOX_POSH_GW_GATEWAY_GENERIC_HPP
#define IOX_POSH_GW_GATEWAY_GENERIC_HPP

#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
#include "iceoryx_posh/capro/service_description.hpp"
#include "iceoryx_posh/gateway/gateway_base.hpp"
#include "iceoryx_posh/gateway/gateway_config.hpp"
Expand All @@ -28,6 +27,7 @@
#include "iox/expected.hpp"
#include "iox/function_ref.hpp"
#include "iox/optional.hpp"
#include "iox/smart_lock.hpp"
#include "iox/string.hpp"
#include "iox/vector.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ template <typename channel_t, typename gateway_t>
inline optional<channel_t>
GatewayGeneric<channel_t, gateway_t>::findChannel(const iox::capro::ServiceDescription& service) const noexcept
{
auto guardedVector = this->m_channels.getScopeGuard();
auto guardedVector = this->m_channels.get_scope_guard();
auto channel = std::find_if(guardedVector->begin(), guardedVector->end(), [&service](const channel_t& channel) {
return channel.getServiceDescription() == service;
});
Expand All @@ -135,7 +135,7 @@ GatewayGeneric<channel_t, gateway_t>::findChannel(const iox::capro::ServiceDescr
template <typename channel_t, typename gateway_t>
inline void GatewayGeneric<channel_t, gateway_t>::forEachChannel(const function_ref<void(channel_t&)> f) const noexcept
{
auto guardedVector = m_channels.getScopeGuard();
auto guardedVector = m_channels.get_scope_guard();
for (auto channel = guardedVector->begin(); channel != guardedVector->end(); ++channel)
{
f(*channel);
Expand All @@ -146,7 +146,7 @@ template <typename channel_t, typename gateway_t>
inline expected<void, GatewayError>
GatewayGeneric<channel_t, gateway_t>::discardChannel(const capro::ServiceDescription& service) noexcept
{
auto guardedVector = this->m_channels.getScopeGuard();
auto guardedVector = this->m_channels.get_scope_guard();
auto channel = std::find_if(guardedVector->begin(), guardedVector->end(), [&service](const channel_t& channel) {
return channel.getServiceDescription() == service;
});
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef IOX_POSH_ROUDI_ROUDI_MULTI_PROCESS_HPP
#define IOX_POSH_ROUDI_ROUDI_MULTI_PROCESS_HPP

#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
#include "iceoryx_platform/file.hpp"
#include "iceoryx_posh/iceoryx_posh_types.hpp"
#include "iceoryx_posh/internal/roudi/introspection/mempool_introspection.hpp"
Expand All @@ -30,6 +29,7 @@
#include "iox/posix_user.hpp"
#include "iox/relative_pointer.hpp"
#include "iox/scope_guard.hpp"
#include "iox/smart_lock.hpp"

#include <cstdint>
#include <thread>
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_posh/include/iceoryx_posh/popo/listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef IOX_POSH_POPO_LISTENER_HPP
#define IOX_POSH_POPO_LISTENER_HPP

#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
#include "iceoryx_posh/internal/popo/building_blocks/condition_listener.hpp"
#include "iceoryx_posh/popo/enum_trigger_type.hpp"
#include "iceoryx_posh/popo/notification_attorney.hpp"
Expand All @@ -27,6 +26,7 @@
#include "iox/detail/mpmc_loffli.hpp"
#include "iox/expected.hpp"
#include "iox/function.hpp"
#include "iox/smart_lock.hpp"

#include <thread>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
#include "iceoryx_hoofs/testing/timing_test.hpp"
#include "iceoryx_hoofs/testing/watch_dog.hpp"
#include "iceoryx_posh/iceoryx_posh_types.hpp"
Expand All @@ -26,6 +25,7 @@
#include "iceoryx_posh/internal/popo/ports/subscriber_port_user.hpp"
#include "iceoryx_posh/mepoo/mepoo_config.hpp"
#include "iox/scope_guard.hpp"
#include "iox/smart_lock.hpp"
#include "test.hpp"

#include <chrono>
Expand Down Expand Up @@ -159,7 +159,7 @@ class PortUser_IntegrationTest : public Test
// Add delay to allow other thread accessing the shared resource
std::this_thread::sleep_for(std::chrono::microseconds(100));
{
auto guardedVector = concurrentCaproMessageVector.getScopeGuard();
auto guardedVector = concurrentCaproMessageVector.get_scope_guard();
if (guardedVector->size() != 0U)
{
caproMessage = guardedVector->back();
Expand Down
Loading

0 comments on commit e003168

Please sign in to comment.