[PATCH 26/26] score: Add _Timecounter_Set_NTP_update_second()

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Nov 8 06:27:54 UTC 2021


Allow the installation of an NTP update second handler which may be used by an
NTP service.

Update #2348.
---
 cpukit/include/rtems/score/timecounter.h | 24 ++++++++++++++++++-
 cpukit/score/src/kern_tc.c               | 30 ++++++++++++++++++++----
 2 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/cpukit/include/rtems/score/timecounter.h b/cpukit/include/rtems/score/timecounter.h
index 8185140f9b..39f0dc353e 100644
--- a/cpukit/include/rtems/score/timecounter.h
+++ b/cpukit/include/rtems/score/timecounter.h
@@ -8,7 +8,7 @@
  */
 
 /*
- * Copyright (c) 2015 embedded brains GmbH.  All rights reserved.
+ * Copyright (c) 2015, 2021 embedded brains GmbH.  All rights reserved.
  *
  *  embedded brains GmbH
  *  Dornierstr. 4
@@ -248,6 +248,28 @@ extern volatile int32_t _Timecounter_Time_uptime;
  */
 extern struct timecounter *_Timecounter;
 
+/**
+ * @brief Handler doing the NTP update second processing shall have this type.
+ *
+ * @param[in, out] adjustment is the NTP time adjustment.
+ *
+ * @param[in, out] newsec is the number of seconds since Unix epoch.
+ */
+typedef	void ( *Timecounter_NTP_update_second )(
+  int64_t *adjustment,
+  time_t  *newsec
+);
+
+/**
+ * @brief Sets the NTP update second handler.
+ *
+ * @param handler is the new NTP update second handler used to carry out the
+ *   NTP update second processing.
+ */
+void _Timecounter_Set_NTP_update_second(
+  Timecounter_NTP_update_second handler
+);
+
 /** @} */
 
 #ifdef __cplusplus
diff --git a/cpukit/score/src/kern_tc.c b/cpukit/score/src/kern_tc.c
index 42318d1c80..e999172cad 100644
--- a/cpukit/score/src/kern_tc.c
+++ b/cpukit/score/src/kern_tc.c
@@ -5,9 +5,10 @@
  *
  * @brief This source file contains the definition of
  *  ::_Timecounter, ::_Timecounter_Time_second, and ::_Timecounter_Time_uptime
- *  and the implementation of _Timecounter_Binuptime(),
- *  _Timecounter_Nanouptime(), _Timecounter_Microuptime(),
- *  _Timecounter_Bintime(), _Timecounter_Nanotime(), _Timecounter_Microtime(),
+ *  and the implementation of _Timecounter_Set_NTP_update_second(),
+ *  _Timecounter_Binuptime(), _Timecounter_Nanouptime(),
+ *  _Timecounter_Microuptime(), _Timecounter_Bintime(),
+ *  _Timecounter_Nanotime(), _Timecounter_Microtime(),
  *  _Timecounter_Getbinuptime(), _Timecounter_Getnanouptime(),
  *  _Timecounter_Getmicrouptime(), _Timecounter_Getbintime(),
  *  _Timecounter_Getnanotime(), _Timecounter_Getmicrotime(),
@@ -97,8 +98,6 @@ ISR_LOCK_DEFINE(, _Timecounter_Lock, "Timecounter")
 #define hz rtems_clock_get_ticks_per_second()
 #define printf(...)
 #define log(...)
-/* FIXME: https://devel.rtems.org/ticket/2348 */
-#define ntp_update_second(a, b) do { (void) a; (void) b; } while (0)
 
 static inline void
 atomic_thread_fence_acq(void)
@@ -134,6 +133,17 @@ atomic_load_ptr(void *ptr)
 
 	return ((void *)_Atomic_Load_uintptr(ptr, ATOMIC_ORDER_RELAXED));
 }
+
+static Timecounter_NTP_update_second _Timecounter_NTP_update_second;
+
+void
+_Timecounter_Set_NTP_update_second(Timecounter_NTP_update_second handler)
+{
+
+	_Timecounter_NTP_update_second = handler;
+}
+
+#define	ntp_update_second(a, b) (*ntp_update_second_handler)(a, b)
 #endif /* __rtems__ */
 
 /*
@@ -1622,6 +1632,9 @@ _Timecounter_Windup(struct bintime *new_boottimebin,
 #endif
 	int i;
 	time_t t;
+#ifdef __rtems__
+	Timecounter_NTP_update_second ntp_update_second_handler;
+#endif
 
 	/*
 	 * Make the next timehands a copy of the current one, but do
@@ -1697,6 +1710,10 @@ _Timecounter_Windup(struct bintime *new_boottimebin,
 	 */
 	bt = th->th_offset;
 	bintime_add(&bt, &th->th_boottime);
+#ifdef __rtems__
+	ntp_update_second_handler = _Timecounter_NTP_update_second;
+	if (ntp_update_second_handler != NULL) {
+#endif /* __rtems__ */
 	i = bt.sec - tho->th_microtime.tv_sec;
 	if (i > 0) {
 		if (i > LARGE_STEP)
@@ -1712,6 +1729,9 @@ _Timecounter_Windup(struct bintime *new_boottimebin,
 
 		recalculate_scaling_factor_and_large_delta(th);
 	}
+#ifdef __rtems__
+	}
+#endif /* __rtems__ */
 
 	/* Update the UTC timestamps used by the get*() functions. */
 	th->th_bintime = bt;
-- 
2.31.1



More information about the devel mailing list