[PATCH 08/16] score: _Watchdog_Is_far_future_realtime_timespec()

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Oct 23 13:53:39 UTC 2017


Update #3117.
Update #3182.
---
 cpukit/score/include/rtems/score/watchdogimpl.h | 41 +++++++++++++++++++------
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/cpukit/score/include/rtems/score/watchdogimpl.h b/cpukit/score/include/rtems/score/watchdogimpl.h
index f1693a3a14..d9dfa36b56 100644
--- a/cpukit/score/include/rtems/score/watchdogimpl.h
+++ b/cpukit/score/include/rtems/score/watchdogimpl.h
@@ -284,8 +284,11 @@ RTEMS_INLINE_ROUTINE void _Watchdog_Next_first(
   }
 }
 
+#define WATCHDOG_NANOSECONDS_PER_SECOND 1000000000
+
 /**
- * @brief The bits necessary to store 1000000000 nanoseconds.
+ * @brief The bits necessary to store 1000000000
+ * (= WATCHDOG_NANOSECONDS_PER_SECOND) nanoseconds.
  *
  * The expiration time is an unsigned 64-bit integer.  To store absolute
  * timeouts we use 30 bits (2**30 == 1073741824) for the nanoseconds and 34
@@ -293,6 +296,29 @@ RTEMS_INLINE_ROUTINE void _Watchdog_Next_first(
  */
 #define WATCHDOG_BITS_FOR_1E9_NANOSECONDS 30
 
+/**
+ * @brief The maximum number of seconds representable in the realtime watchdog
+ * format.
+ *
+ * We have 2**34 bits for the seconds part.
+ */
+#define WATCHDOG_REALTIME_MAX_SECONDS 0x3ffffffff
+
+RTEMS_INLINE_ROUTINE bool _Watchdog_Is_valid_timespec(
+  const struct timespec *ts
+)
+{
+  return ts != NULL
+    && (unsigned long) ts->tv_nsec < WATCHDOG_NANOSECONDS_PER_SECOND;
+}
+
+RTEMS_INLINE_ROUTINE bool _Watchdog_Is_far_future_realtime_timespec(
+  const struct timespec *ts
+)
+{
+  return ts->tv_sec > WATCHDOG_REALTIME_MAX_SECONDS;
+}
+
 RTEMS_INLINE_ROUTINE uint64_t _Watchdog_Realtime_from_seconds(
   uint32_t seconds
 )
@@ -308,16 +334,13 @@ RTEMS_INLINE_ROUTINE uint64_t _Watchdog_Realtime_from_timespec(
   const struct timespec *ts
 )
 {
-  /*
-   * The seconds are in time_t which is a signed integer.  Thus this cast is
-   * subject to the year 2038 problem in case time_t is a 32-bit integer.
-   */
-  uint64_t ticks = (uint64_t) ts->tv_sec;
+  uint64_t ticks;
 
-  _Assert( ticks < 0x400000000 );
-  _Assert( ts->tv_nsec >= 0 );
-  _Assert( ts->tv_nsec < 1000000000 );
+  _Assert( _Watchdog_Is_valid_timespec( ts ) );
+  _Assert( ts->tv_sec >= 0 );
+  _Assert( !_Watchdog_Is_far_future_realtime_timespec( ts ) );
 
+  ticks = (uint64_t) ts->tv_sec;
   ticks <<= WATCHDOG_BITS_FOR_1E9_NANOSECONDS;
   ticks |= (uint32_t) ts->tv_nsec;
 
-- 
2.12.3




More information about the devel mailing list