Implement TimeService with std::chrono 07/242607/4
authorVictor Cebollada <v.cebollada@samsung.com>
Fri, 28 Aug 2020 07:17:31 +0000 (08:17 +0100)
committerGyörgy Straub <g.straub@partner.samsung.com>
Tue, 3 Nov 2020 10:56:58 +0000 (10:56 +0000)
* Platform independent.
* Add GetMilliseconds() to be used in the SceneHolder implementation.

Change-Id: I26977721d0f368e46c8519801957b69ad7d67eb0
Signed-off-by: Victor Cebollada <v.cebollada@samsung.com>
dali/integration-api/adaptor-framework/scene-holder-impl.cpp
dali/internal/system/common/time-service.cpp
dali/internal/system/common/time-service.h

index fc29948..073372c 100644 (file)
@@ -35,6 +35,7 @@
 #include <dali/internal/graphics/gles/egl-graphics.h>
 #include <dali/internal/input/common/key-impl.h>
 #include <dali/internal/input/common/physical-keyboard-impl.h>
+#include <dali/internal/system/common/time-service.h>
 
 namespace Dali
 {
@@ -47,43 +48,6 @@ namespace
 #if defined(DEBUG_ENABLED)
 Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SCENE_HOLDER");
 #endif
-
-// Copied from x server
-static uint32_t GetCurrentMilliSeconds(void)
-{
-  struct timeval tv;
-
-  struct timespec  tp;
-  static clockid_t clockid;
-
-  if(!clockid)
-  {
-#ifdef CLOCK_MONOTONIC_COARSE
-    if(clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
-       (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
-    {
-      clockid = CLOCK_MONOTONIC_COARSE;
-    }
-    else
-#endif
-      if(clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
-    {
-      clockid = CLOCK_MONOTONIC;
-    }
-    else
-    {
-      clockid = ~0L;
-    }
-  }
-  if(clockid != ~0L && clock_gettime(clockid, &tp) == 0)
-  {
-    return static_cast<uint32_t>((tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L));
-  }
-
-  gettimeofday(&tv, NULL);
-  return static_cast<uint32_t>((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
-}
-
 } // unnamed namespace
 
 uint32_t SceneHolder::mSceneHolderCounter = 0;
@@ -276,7 +240,7 @@ void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
 {
   if(timeStamp < 1)
   {
-    timeStamp = GetCurrentMilliSeconds();
+    timeStamp = TimeService::GetMilliSeconds();
   }
 
   RecalculateTouchPosition(point);
index aa310b7..c0bc646 100644 (file)
@@ -19,7 +19,8 @@
 #include <dali/internal/system/common/time-service.h>
 
 // EXTERNAL INCLUDES
-#include <ctime>
+#include <chrono>
+#include <thread>
 
 namespace Dali
 {
@@ -33,36 +34,36 @@ namespace Adaptor
 namespace TimeService
 {
 
-namespace
+void GetNanoseconds( uint64_t& timeInNanoseconds )
 {
-const uint64_t NANOSECONDS_PER_SECOND = 1e+9;
+  // Get the time of a monotonic clock since its epoch.
+  auto epoch = std::chrono::steady_clock::now().time_since_epoch();
+
+  auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch);
+
+  timeInNanoseconds = static_cast<uint64_t>(duration.count());
 }
 
-void GetNanoseconds( uint64_t& timeInNanoseconds )
+uint32_t GetMilliSeconds()
 {
-  timespec timeSpec;
-  if( clock_gettime( CLOCK_MONOTONIC, &timeSpec ) == 0 )
-  {
-    // Convert all values to uint64_t to match our return type
-    timeInNanoseconds = ( static_cast< uint64_t >( timeSpec.tv_sec ) * NANOSECONDS_PER_SECOND ) + static_cast< uint64_t >( timeSpec.tv_nsec );
-  }
-  else
-  {
-    timeInNanoseconds = 0;
-  }
+  // Get the time of a monotonic clock since its epoch.
+  auto epoch = std::chrono::steady_clock::now().time_since_epoch();
+
+  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
+
+  return static_cast<uint32_t>(duration.count());
 }
 
+
 void SleepUntil( uint64_t timeInNanoseconds )
 {
-  timespec timeSpec;
-  timeSpec.tv_sec  = timeInNanoseconds / NANOSECONDS_PER_SECOND;
-  timeSpec.tv_nsec = timeInNanoseconds % NANOSECONDS_PER_SECOND;
-
-  // clock_nanosleep returns 0 if it sleeps for the period specified, otherwise it returns an error value
-  // If an error value is returned, just sleep again till the absolute time specified
-  while( clock_nanosleep( CLOCK_MONOTONIC, TIMER_ABSTIME, &timeSpec, NULL ) )
-  {
-  }
+  using Clock = std::chrono::steady_clock;
+  using TimePoint = std::chrono::time_point<Clock>;
+
+  const Clock::duration duration = std::chrono::nanoseconds(timeInNanoseconds);
+  const TimePoint timePoint(duration);
+
+  std::this_thread::sleep_until(timePoint);
 }
 
 } // namespace TimeService
index 094aeba..7f55328 100644 (file)
@@ -33,7 +33,7 @@ namespace TimeService
 {
 
 /**
- * @brief Get the monotonic time since some unspecified starting point (usually the boot time).
+ * @brief Get the monotonic time since the clock's epoch.
  *
  * @param[out]  timeInNanoseconds  The time in nanoseconds since the reference point.
  *
@@ -42,7 +42,16 @@ namespace TimeService
 void GetNanoseconds( uint64_t& timeInNanoseconds );
 
 /**
- * @brief Sleeps until the monotonic time specified since some unspecified starting point (usually the boot time).
+ * @brief Get the monotonic time since the clock's epoch.
+ *
+ * @return The time in milliseconds since the reference point.
+ *
+ * @note The maximum value that can be returned is 0xFFFFFFFF which is 4,294,967,295. Therefore, this can overflow after approximately 49 days.
+ */
+uint32_t GetMilliSeconds();
+
+/**
+ * @brief Sleeps until the monotonic time specified since the clock's epoch.
  *
  * If the time specified has already passed, then it returns immediately.
  *