Skip to content

Commit

Permalink
iox-eclipse-iceoryx#2301 Squash me
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Sep 19, 2024
1 parent ca63dd8 commit 6929dd8
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 61 deletions.
2 changes: 1 addition & 1 deletion iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ iox_add_library(
posix/sync/source/named_semaphore.cpp
posix/sync/source/signal_handler.cpp
posix/sync/source/signal_watcher.cpp
posix/sync/source/semaphore_interface.cpp
posix/sync/source/semaphore_helper.cpp
posix/sync/source/thread.cpp
posix/sync/source/unnamed_semaphore.cpp
posix/time/source/adaptive_wait.cpp
Expand Down
18 changes: 10 additions & 8 deletions iceoryx_hoofs/concurrent/sync/include/iox/spin_semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
#include "iox/atomic.hpp"
#include "iox/deadline_timer.hpp"
#include "iox/detail/adaptive_wait.hpp"
#include "iox/detail/semaphore_interface.hpp"
#include "iox/detail/semaphore_helper.hpp"
#include "iox/optional.hpp"
#include "iox/semaphore_interface.hpp"
#include "iox/spin_lock.hpp"

namespace iox
Expand All @@ -43,18 +44,19 @@ class SpinSemaphore : public detail::SemaphoreInterface<SpinSemaphore>

~SpinSemaphore() noexcept;

expected<void, SemaphoreError> post() noexcept;
private:
friend class optional<SpinSemaphore>;
friend class detail::SemaphoreInterface<SpinSemaphore>;

expected<void, SemaphoreError> wait() noexcept;
explicit SpinSemaphore(int32_t initial_value) noexcept;

expected<bool, SemaphoreError> tryWait() noexcept;
expected<void, SemaphoreError> post_impl() noexcept;

expected<SemaphoreWaitState, SemaphoreError> timedWait(const units::Duration& timeout) noexcept;
expected<void, SemaphoreError> wait_impl() noexcept;

private:
friend class optional<SpinSemaphore>;
expected<bool, SemaphoreError> try_wait_impl() noexcept;

explicit SpinSemaphore(int32_t initial_value) noexcept;
expected<SemaphoreWaitState, SemaphoreError> timed_wait_impl(const units::Duration& timeout) noexcept;

private:
concurrent::Atomic<int32_t> m_count{0};
Expand Down
8 changes: 4 additions & 4 deletions iceoryx_hoofs/concurrent/sync/source/spin_semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ SpinSemaphore::~SpinSemaphore() noexcept
m_to_be_destroyed = true;
}

expected<void, SemaphoreError> SpinSemaphore::post() noexcept
expected<void, SemaphoreError> SpinSemaphore::post_impl() noexcept
{
std::lock_guard<concurrent::SpinLock> lock(*m_spinlock);
++m_count;
return ok();
}

expected<void, SemaphoreError> SpinSemaphore::wait() noexcept
expected<void, SemaphoreError> SpinSemaphore::wait_impl() noexcept
{
detail::adaptive_wait spinner;
spinner.wait_loop([this] { return !this->tryWait(); });
return ok();
}

expected<bool, SemaphoreError> SpinSemaphore::tryWait() noexcept
expected<bool, SemaphoreError> SpinSemaphore::try_wait_impl() noexcept
{
std::lock_guard<concurrent::SpinLock> lock(*m_spinlock);
if (m_to_be_destroyed.load(std::memory_order_relaxed))
Expand All @@ -72,7 +72,7 @@ expected<bool, SemaphoreError> SpinSemaphore::tryWait() noexcept
return ok(false);
}

expected<SemaphoreWaitState, SemaphoreError> SpinSemaphore::timedWait(const units::Duration& timeout) noexcept
expected<SemaphoreWaitState, SemaphoreError> SpinSemaphore::timed_wait_impl(const units::Duration& timeout) noexcept
{
iox::deadline_timer deadline_timer(timeout);
detail::adaptive_wait spinner;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2024 by ekxide IO GmbH. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,8 +15,8 @@
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_POSIX_SYNC_SEMAPHORE_INTERFACE_HPP
#define IOX_HOOFS_POSIX_SYNC_SEMAPHORE_INTERFACE_HPP
#ifndef IOX_HOOFS_DESIGN_SEMAPHORE_INTERFACE_HPP
#define IOX_HOOFS_DESIGN_SEMAPHORE_INTERFACE_HPP

#include "iceoryx_platform/semaphore.hpp"
#include "iox/duration.hpp"
Expand Down Expand Up @@ -59,32 +60,41 @@ class SemaphoreInterface
/// @brief Increments the semaphore by one
/// @return Fails when the value of the semaphore overflows or when the
/// semaphore was removed from outside the process
expected<void, SemaphoreError> post() noexcept;
expected<void, SemaphoreError> post() noexcept
{
return static_cast<SemaphoreChild*>(this)->post_impl();
}

/// @brief Decrements the semaphore by one. When the semaphore value is zero
/// it blocks until the semaphore value is greater zero
/// @return Fails when semaphore was removed from outside the process
expected<void, SemaphoreError> wait() noexcept;
expected<void, SemaphoreError> wait() noexcept
{
return static_cast<SemaphoreChild*>(this)->wait_impl();
}

/// @brief Tries to decrement the semaphore by one. When the semaphore value is zero
/// it returns false otherwise it returns true and decrement the value by one.
/// @return Fails when semaphore was removed from outside the process
expected<bool, SemaphoreError> tryWait() noexcept;
expected<bool, SemaphoreError> tryWait() noexcept
{
return static_cast<SemaphoreChild*>(this)->try_wait_impl();
}

/// @brief Tries to decrement the semaphore by one. When the semaphore value is zero
/// it waits until the timeout has passed.
/// @return If during the timeout time the semaphore value increases to non zero
/// it returns SemaphoreWaitState::NO_TIMEOUT and decreases the semaphore by one
/// otherwise returns SemaphoreWaitState::TIMEOUT
expected<SemaphoreWaitState, SemaphoreError> timedWait(const units::Duration& timeout) noexcept;
expected<SemaphoreWaitState, SemaphoreError> timedWait(const units::Duration& timeout) noexcept
{
return static_cast<SemaphoreChild*>(this)->timed_wait_impl(timeout);
}

protected:
SemaphoreInterface() noexcept = default;

private:
iox_sem_t* getHandle() noexcept;
};
} // namespace detail
} // namespace iox

#endif // IOX_HOOFS_POSIX_SYNC_SEMAPHORE_INTERFACE_HPP
#endif // IOX_HOOFS_DESIGN_SEMAPHORE_INTERFACE_HPP
54 changes: 54 additions & 0 deletions iceoryx_hoofs/posix/sync/include/iox/detail/semaphore_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2022 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_POSIX_SYNC_SEMAPHORE_HELPER_HPP
#define IOX_HOOFS_POSIX_SYNC_SEMAPHORE_HELPER_HPP

#include "iceoryx_platform/semaphore.hpp"
#include "iox/duration.hpp"
#include "iox/expected.hpp"
#include "iox/semaphore_interface.hpp"

namespace iox
{
namespace detail
{
/// @brief Increments the semaphore by one
/// @return Fails when the value of the semaphore overflows or when the
/// semaphore was removed from outside the process
expected<void, SemaphoreError> sem_post(iox_sem_t* handle) noexcept;

/// @brief Decrements the semaphore by one. When the semaphore value is zero
/// it blocks until the semaphore value is greater zero
/// @return Fails when semaphore was removed from outside the process
expected<void, SemaphoreError> sem_wait(iox_sem_t* handle) noexcept;

/// @brief Tries to decrement the semaphore by one. When the semaphore value is zero
/// it returns false otherwise it returns true and decrement the value by one.
/// @return Fails when semaphore was removed from outside the process
expected<bool, SemaphoreError> sem_try_wait(iox_sem_t* handle) noexcept;

/// @brief Tries to decrement the semaphore by one. When the semaphore value is zero
/// it waits until the timeout has passed.
/// @return If during the timeout time the semaphore value increases to non zero
/// it returns SemaphoreWaitState::NO_TIMEOUT and decreases the semaphore by one
/// otherwise returns SemaphoreWaitState::TIMEOUT
expected<SemaphoreWaitState, SemaphoreError> sem_timed_wait(iox_sem_t* handle, const units::Duration& timeout) noexcept;

} // namespace detail
} // namespace iox

#endif // IOX_HOOFS_POSIX_SYNC_SEMAPHORE_HELPER_HPP
9 changes: 7 additions & 2 deletions iceoryx_hoofs/posix/sync/include/iox/named_semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

#include "iceoryx_platform/platform_settings.hpp"
#include "iox/builder.hpp"
#include "iox/detail/semaphore_interface.hpp"
#include "iox/detail/semaphore_helper.hpp"
#include "iox/expected.hpp"
#include "iox/filesystem.hpp"
#include "iox/optional.hpp"
#include "iox/semaphore_interface.hpp"
#include "iox/string.hpp"

namespace iox
Expand Down Expand Up @@ -50,7 +51,11 @@ class NamedSemaphore final : public detail::SemaphoreInterface<NamedSemaphore>
friend class detail::SemaphoreInterface<NamedSemaphore>;

NamedSemaphore(iox_sem_t* handle, const Name_t& name, const bool hasOwnership) noexcept;
iox_sem_t* getHandle() noexcept;

expected<void, SemaphoreError> post_impl() noexcept;
expected<void, SemaphoreError> wait_impl() noexcept;
expected<bool, SemaphoreError> try_wait_impl() noexcept;
expected<SemaphoreWaitState, SemaphoreError> timed_wait_impl(const units::Duration& timeout) noexcept;

iox_sem_t* m_handle = nullptr;
Name_t m_name;
Expand Down
9 changes: 7 additions & 2 deletions iceoryx_hoofs/posix/sync/include/iox/unnamed_semaphore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
#define IOX_HOOFS_POSIX_SYNC_UNNAMED_SEMAPHORE_HPP

#include "iox/builder.hpp"
#include "iox/detail/semaphore_interface.hpp"
#include "iox/detail/semaphore_helper.hpp"
#include "iox/expected.hpp"
#include "iox/optional.hpp"
#include "iox/semaphore_interface.hpp"

namespace iox
{
Expand Down Expand Up @@ -48,7 +49,11 @@ class UnnamedSemaphore final : public detail::SemaphoreInterface<UnnamedSemaphor
friend class detail::SemaphoreInterface<UnnamedSemaphore>;

UnnamedSemaphore() noexcept = default;
iox_sem_t* getHandle() noexcept;

expected<void, SemaphoreError> post_impl() noexcept;
expected<void, SemaphoreError> wait_impl() noexcept;
expected<bool, SemaphoreError> try_wait_impl() noexcept;
expected<SemaphoreWaitState, SemaphoreError> timed_wait_impl(const units::Duration& timeout) noexcept;

iox_sem_t m_handle;
bool m_destroyHandle = true;
Expand Down
16 changes: 14 additions & 2 deletions iceoryx_hoofs/posix/sync/source/named_semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,20 @@ NamedSemaphore::~NamedSemaphore() noexcept
}
}

iox_sem_t* NamedSemaphore::getHandle() noexcept
expected<void, SemaphoreError> NamedSemaphore::post_impl() noexcept
{
return m_handle;
return detail::sem_post(m_handle);
}
expected<void, SemaphoreError> NamedSemaphore::wait_impl() noexcept
{
return detail::sem_wait(m_handle);
}
expected<bool, SemaphoreError> NamedSemaphore::try_wait_impl() noexcept
{
return detail::sem_try_wait(m_handle);
}
expected<SemaphoreWaitState, SemaphoreError> NamedSemaphore::timed_wait_impl(const units::Duration& timeout) noexcept
{
return detail::sem_timed_wait(m_handle, timeout);
}
} // namespace iox
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iox/detail/semaphore_interface.hpp"
#include "iox/detail/semaphore_helper.hpp"
#include "iox/logging.hpp"
#include "iox/named_semaphore.hpp"
#include "iox/posix_call.hpp"
#include "iox/unnamed_semaphore.hpp"

namespace iox
{
namespace detail
{
SemaphoreError errnoToEnum(const int32_t errnum) noexcept
SemaphoreError sem_errno_to_enum(const int32_t errnum) noexcept
{
switch (errnum)
{
Expand All @@ -44,70 +42,57 @@ SemaphoreError errnoToEnum(const int32_t errnum) noexcept
return SemaphoreError::UNDEFINED;
}

template <typename SemaphoreChild>
iox_sem_t* SemaphoreInterface<SemaphoreChild>::getHandle() noexcept
expected<void, SemaphoreError> sem_post(iox_sem_t* handle) noexcept
{
return static_cast<SemaphoreChild*>(this)->getHandle();
}

template <typename SemaphoreChild>
expected<void, SemaphoreError> SemaphoreInterface<SemaphoreChild>::post() noexcept
{
auto result = IOX_POSIX_CALL(iox_sem_post)(getHandle()).failureReturnValue(-1).evaluate();
auto result = IOX_POSIX_CALL(iox_sem_post)(handle).failureReturnValue(-1).evaluate();

if (result.has_error())
{
return err(errnoToEnum(result.error().errnum));
return err(sem_errno_to_enum(result.error().errnum));
}

return ok<void>();
}

template <typename SemaphoreChild>
expected<SemaphoreWaitState, SemaphoreError>
SemaphoreInterface<SemaphoreChild>::timedWait(const units::Duration& timeout) noexcept
expected<SemaphoreWaitState, SemaphoreError> sem_timed_wait(iox_sem_t* handle, const units::Duration& timeout) noexcept
{
const timespec timeoutAsTimespec = timeout.timespec(units::TimeSpecReference::Epoch);
auto result = IOX_POSIX_CALL(iox_sem_timedwait)(getHandle(), &timeoutAsTimespec)
auto result = IOX_POSIX_CALL(iox_sem_timedwait)(handle, &timeoutAsTimespec)
.failureReturnValue(-1)
.ignoreErrnos(ETIMEDOUT)
.evaluate();

if (result.has_error())
{
return err(errnoToEnum(result.error().errnum));
return err(sem_errno_to_enum(result.error().errnum));
}

return ok((result.value().errnum == ETIMEDOUT) ? SemaphoreWaitState::TIMEOUT : SemaphoreWaitState::NO_TIMEOUT);
}

template <typename SemaphoreChild>
expected<bool, SemaphoreError> SemaphoreInterface<SemaphoreChild>::tryWait() noexcept
expected<bool, SemaphoreError> sem_try_wait(iox_sem_t* handle) noexcept
{
auto result = IOX_POSIX_CALL(iox_sem_trywait)(getHandle()).failureReturnValue(-1).ignoreErrnos(EAGAIN).evaluate();
auto result = IOX_POSIX_CALL(iox_sem_trywait)(handle).failureReturnValue(-1).ignoreErrnos(EAGAIN).evaluate();

if (result.has_error())
{
return err(errnoToEnum(result.error().errnum));
return err(sem_errno_to_enum(result.error().errnum));
}

return ok(result.value().errnum != EAGAIN);
}

template <typename SemaphoreChild>
expected<void, SemaphoreError> SemaphoreInterface<SemaphoreChild>::wait() noexcept
expected<void, SemaphoreError> sem_wait(iox_sem_t* handle) noexcept
{
auto result = IOX_POSIX_CALL(iox_sem_wait)(getHandle()).failureReturnValue(-1).evaluate();
auto result = IOX_POSIX_CALL(iox_sem_wait)(handle).failureReturnValue(-1).evaluate();

if (result.has_error())
{
return err(errnoToEnum(result.error().errnum));
return err(sem_errno_to_enum(result.error().errnum));
}

return ok<void>();
}

template class SemaphoreInterface<UnnamedSemaphore>;
template class SemaphoreInterface<NamedSemaphore>;
} // namespace detail
} // namespace iox
Loading

0 comments on commit 6929dd8

Please sign in to comment.