Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gnulinux: use CLOCK_MONOTONIC for periodic tasks (fix #105) #138

Merged
merged 1 commit into from
May 17, 2018

Conversation

meyerj
Copy link
Member

@meyerj meyerj commented Feb 9, 2016

This PR applies the patch proposed by @rbrtslmn in #105.

It switches to use a monotonic clock source for periodic waiting in the gnulinux target, but it is also somehow incomplete because it does not fix the same issue for other broken targets (like MacOS), for waiting on condition variables and for RTT Timers.

Signed-off-by: Johannes Meyer <johannes@intermodalics.eu>
@meyerj
Copy link
Member Author

meyerj commented Jun 6, 2016

Merged to toolchain-2.9 branch in f6017b0.

@meyerj
Copy link
Member Author

meyerj commented Oct 23, 2017

Note that this patch is not effective anymore in combination with #91.

meyerj added a commit to meyerj/rtt that referenced this pull request Mar 1, 2018
The absolute time returned by rtos_get_time_ns() or RTT::os::TimeService::getNSecs() was generated from
a wall clock (time of day), which is not monotonic and can jump in certain cases, e.g. due to an NTP update.
This time is used in several internal and public API functions to implement timers or to wait on a
condition variable, mutex or semaphore with a timeout. In all these cases non-steady system time can have
undesirable side effects, which are not expected in a Real-Time Toolkit.

There have been several attempts to fix or at least mitigate that problem:
- http://bugs.orocos.org/show_bug.cgi?id=735
- orocos-toolchain#129
- orocos-toolchain#138

This patch introduces some potentially breaking changes:
- Use clock_gettime(CLOCK_MONOTONIC, ...) to implement rtos_get_time_ns() for the gnulinux target.
  This change has a major impact on all usages of that method, rtos_get_ticks() or the higher-level
  methods RTT::os::TimeService::getNSecs() and RTT::os::TimeService::getTicks(), whose return values
  cannot be interpreted as wall time anymore.
- Consequently remove the rtos_get_time_monotonic-ns() function introduced in
  orocos-toolchain#138.
- Drop RTT::os::Semaphore::waitUntil() and the underlying OS abstraction functions.
  The only usage within the RTT core was in RTT::os::Timer, which can easily be rewritten using
  condition variables. POSIX semaphores do not support waiting with an absolute timeout from the
  monotonic clock.
- Added new OS abstraction functions for waiting on a mutex lock with a relative timeout. POSIX does
  not provide a pthread_mutexattr_setclock() method similar to pthread_condattr_setclock(). For that
  reason rtos_mutex_lock_until() and rtos_mutex_trylock_for() are currently falling back to
  CLOCK_REALTIME.
meyerj added a commit to meyerj/rtt that referenced this pull request Mar 1, 2018
The absolute time returned by rtos_get_time_ns() or RTT::os::TimeService::getNSecs() was generated from
a wall clock (time of day), which is not monotonic and can jump in certain cases, e.g. due to an NTP update.
This time is used in several internal and public API functions to implement timers or to wait on a
condition variable, mutex or semaphore with a timeout. In all these cases non-steady system time can have
undesirable side effects, which are not expected in a Real-Time Toolkit.

There have been several attempts to fix or at least mitigate that problem:
- http://bugs.orocos.org/show_bug.cgi?id=735
- orocos-toolchain#129
- orocos-toolchain#138

This patch introduces some potentially breaking changes:
- Use clock_gettime(CLOCK_MONOTONIC, ...) to implement rtos_get_time_ns() for the gnulinux target.
  This change has a major impact on all usages of that method, rtos_get_ticks() or the higher-level
  methods RTT::os::TimeService::getNSecs() and RTT::os::TimeService::getTicks(), whose return values
  cannot be interpreted as wall time anymore.
- Consequently remove the rtos_get_time_monotonic-ns() function introduced in
  orocos-toolchain#138.
- Drop RTT::os::Semaphore::waitUntil() and the underlying OS abstraction functions.
  The only usage within the RTT core was in RTT::os::Timer, which can easily be rewritten using
  condition variables. POSIX semaphores do not support waiting with an absolute timeout from the
  monotonic clock.
- Added new OS abstraction functions for waiting on a mutex lock with a relative timeout. POSIX does
  not provide a pthread_mutexattr_setclock() method similar to pthread_condattr_setclock(). For that
  reason rtos_mutex_lock_until() and rtos_mutex_trylock_for() are currently falling back to
  CLOCK_REALTIME.

Signed-off-by: Johannes Meyer <johannes@intermodalics.eu>
meyerj added a commit to Intermodalics/rtt that referenced this pull request Mar 1, 2018
The absolute time returned by rtos_get_time_ns() or RTT::os::TimeService::getNSecs() was generated from
a wall clock (time of day), which is not monotonic and can jump in certain cases, e.g. due to an NTP update.
This time source is used in several internal and public API functions to implement timers or to wait on a
condition variable, mutex or semaphore with a timeout. In all these cases non-steady system time can have
undesirable side effects, which are not expected in a Real-Time Toolkit.

There have been several attempts to fix or at least mitigate that problem:
- http://bugs.orocos.org/show_bug.cgi?id=735
- orocos-toolchain#129
- orocos-toolchain#138

This patch introduces some potentially breaking semantic changes, with only minimal changes to the
public API:

- Use clock_gettime(CLOCK_MONOTONIC, ...) to implement rtos_get_time_ns() for the gnulinux target.
  This change has a major impact on all usages of that method, rtos_get_ticks() or the higher-level
  methods RTT::os::TimeService::getNSecs() and RTT::os::TimeService::getTicks(), whose return values
  cannot be interpreted as wall time anymore.
- Consequently remove the rtos_get_time_monotonic_ns() function introduced in
  orocos-toolchain#138.
- Drop RTT::os::Semaphore::waitUntil() and the underlying OS abstraction functions.
  The only usage within the RTT core was in RTT::os::Timer, which can be rewritten using a
  condition variable. POSIX semaphores do not support waiting with an absolute timeout from the
  monotonic clock.
- Add new OS abstraction functions for waiting on a mutex lock with a relative timeout and use them
  to implement RTT::os::Mutex::timedlock(). POSIX does not provide a pthread_mutexattr_setclock()
  method similar to pthread_condattr_setclock(). For that reason rtos_mutex_lock_until() and
  rtos_mutex_trylock_for() have to fall back to CLOCK_REALTIME and may still be affected by time
  adjustments while waiting.

Signed-off-by: Johannes Meyer <johannes@intermodalics.eu>
meyerj added a commit to Intermodalics/rtt that referenced this pull request Mar 1, 2018
The absolute time returned by rtos_get_time_ns() or RTT::os::TimeService::getNSecs() was generated from
a wall clock (time of day), which is not monotonic and can jump in certain cases, e.g. due to an NTP update.
This time source is used in several internal and public API functions to implement timers or to wait on a
condition variable, mutex or semaphore with a timeout. In all these cases non-steady system time can have
undesirable side effects, which are not expected in a Real-Time Toolkit.

There have been several attempts to fix or at least mitigate that problem:
- http://bugs.orocos.org/show_bug.cgi?id=735
- orocos-toolchain#129
- orocos-toolchain#138

This patch introduces some potentially breaking semantic changes, with only minimal changes to the
public API:

- Use clock_gettime(CLOCK_MONOTONIC, ...) to implement rtos_get_time_ns() for the gnulinux target.
  This change has a major impact on all usages of that method, rtos_get_ticks() or the higher-level
  methods RTT::os::TimeService::getNSecs() and RTT::os::TimeService::getTicks(), whose return values
  cannot be interpreted as wall time anymore.
- Consequently remove the rtos_get_time_monotonic_ns() function introduced in
  orocos-toolchain#138.
- Drop RTT::os::Semaphore::waitUntil() and the underlying OS abstraction functions.
  The only usage within the RTT core was in RTT::os::Timer, which can be rewritten using a
  condition variable. POSIX semaphores do not support waiting with an absolute timeout from the
  monotonic clock.
- Add new OS abstraction functions for waiting on a mutex lock with a relative timeout and use them
  to implement RTT::os::Mutex::timedlock(). POSIX does not provide a pthread_mutexattr_setclock()
  method similar to pthread_condattr_setclock(). For that reason rtos_mutex_lock_until() and
  rtos_mutex_trylock_for() have to fall back to CLOCK_REALTIME and may still be affected by time
  adjustments while waiting.

Signed-off-by: Johannes Meyer <johannes@intermodalics.eu>
@meyerj meyerj merged commit 702f9f8 into master May 17, 2018
@meyerj meyerj deleted the fix/wait-period-clock-monotonic branch April 16, 2019 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants