Skip to content

Commit

Permalink
iox-eclipse-iceoryx#161 Terminate when overtaking periodicity of posi…
Browse files Browse the repository at this point in the history
…x::timer and add its respective test case

Signed-off-by: Hoinkis Simon (CC-AD/ESW1) <simon.hoinkis2@de.bosch.com>
  • Loading branch information
mossmaurice committed Jun 30, 2020
1 parent 8da9279 commit 9a30f9e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions iceoryx_utils/include/iceoryx_utils/posix_wrapper/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ class Timer
/// @note This call is wrapped in a plain C function
void executeCallback() noexcept;


private:
/// @brief Duration after the timer calls the user-defined callback function
units::Duration m_timeToWait;

Expand All @@ -182,6 +180,8 @@ class Timer
/// @brief Bool that signals whether the object is fully initalized
bool m_isInitialized{false};

std::atomic_bool m_isCallbackRunning{false};

/// @todo creation pattern
/// @brief If an error happened during creation the value is stored in here
TimerError m_errorValue{TimerError::NO_ERROR};
Expand Down
18 changes: 16 additions & 2 deletions iceoryx_utils/source/posix_wrapper/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ void Timer::OsTimer::callbackHelper(sigval data)
return;
}

std::lock_guard<std::mutex> lock(callbackHandle.m_accessMutex);
if (!callbackHandle.m_inUse.load(std::memory_order::memory_order_relaxed))
{
return;
Expand All @@ -94,7 +93,18 @@ void Timer::OsTimer::callbackHelper(sigval data)
/// @todo cxx::expect
if (callbackHandle.m_timer != nullptr)
{
callbackHandle.m_timer->executeCallback();
if (callbackHandle.m_timer->m_isCallbackRunning)
{
#if defined(NDEBUG)
std::cerr << "Callable, passed to the POSIX timer, took longer than periodicity" << std::endl;
std::terminate();
#endif
}
else
{
std::lock_guard<std::mutex> lock(callbackHandle.m_accessMutex);
callbackHandle.m_timer->executeCallback();
}
}
}

Expand Down Expand Up @@ -201,6 +211,8 @@ Timer::OsTimer::~OsTimer() noexcept

void Timer::OsTimer::executeCallback() noexcept
{
m_isCallbackRunning.store(true, std::memory_order_relaxed);

if (m_isInitialized && m_callback)
{
m_callback();
Expand All @@ -211,6 +223,8 @@ void Timer::OsTimer::executeCallback() noexcept
// temporary?
errorHandler(Error::kPOSIX_TIMER__FIRED_TIMER_BUT_STATE_IS_INVALID);
}

m_isCallbackRunning.store(false, std::memory_order_relaxed);
}

cxx::expected<TimerError> Timer::OsTimer::start(const RunMode runMode) noexcept
Expand Down
6 changes: 6 additions & 0 deletions iceoryx_utils/test/moduletests/test_posix_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,9 @@ TEST_F(Timer_test, GetOverrunsFailsWithNoCallback)
ASSERT_THAT(call.has_error(), Eq(true));
EXPECT_THAT(call.get_error(), Eq(TimerError::TIMER_NOT_INITIALIZED));
}

TEST_F(Timer_test, CallableOvertakingPeridicityLeadsToTermination)
{
Timer sut(10_ms, [] { std::this_thread::sleep_for(std::chrono::milliseconds(100)); });
EXPECT_DEATH({}, ".*");
}

0 comments on commit 9a30f9e

Please sign in to comment.