Added a millisecond timer to the threading library.
authorJim Carroll <thecarrolls@jiminger.com>
Tue, 19 Jul 2011 00:03:32 +0000 (20:03 -0400)
committerJim Carroll <thecarrolls@jiminger.com>
Wed, 20 Jul 2011 04:58:28 +0000 (00:58 -0400)
Since we are removing boost threads shortly, and since the boost::date_time has issues with thread safety on osx, we moved the code in CTimeUtils::GetTimeMS() into XbmcThread::currentClockMillis(). It was also adjusted so that the windows call to timeGetTime handles the wrapping correctly (which it didn't appear to do before)."

xbmc/threads/Condition.h
xbmc/threads/Makefile
xbmc/threads/Time.cpp [new file with mode: 0644]
xbmc/threads/Time.h [new file with mode: 0644]
xbmc/utils/TimeUtils.cpp

index bc8ad59..b66669f 100644 (file)
@@ -23,9 +23,7 @@
 
 #include "threads/platform/Condition.h"
 
-#include <boost/date_time/microsec_time_clock.hpp>
-#include <boost/date_time/posix_time/posix_time_types.hpp>
-
+#include "threads/Time.h"
 #include <stdio.h>
 
 namespace XbmcThreads
@@ -46,12 +44,10 @@ namespace XbmcThreads
     ConditionVariable& cond;
     P predicate;
 
-    typedef boost::posix_time::ptime system_time;
-
-    inline static unsigned long timeLeft(const system_time& endtime)
+    inline static unsigned long timeLeft(unsigned int endtime)
     {
-      long diff = (long)(endtime - boost::date_time::microsec_clock<system_time>::universal_time()).total_milliseconds();
-      return diff < 0 ? 0 : (unsigned long)diff;
+      unsigned int cur = currentClockMillis();
+      return endtime <= cur ? 0 : (endtime - cur);
     }
 
   public:
@@ -63,7 +59,7 @@ namespace XbmcThreads
       bool ret = true;
       if (!predicate)
       {
-        system_time const endtime = boost::date_time::microsec_clock<system_time>::universal_time() + boost::posix_time::milliseconds(milliseconds);
+        unsigned int endtime = currentClockMillis() + milliseconds;
         bool notdone = true;
         while (notdone && ret == true)
         {
index 725b6ba..50d357b 100644 (file)
@@ -2,6 +2,7 @@ SRCS=Atomics.cpp \
      Event.cpp \
      LockFree.cpp \
      Thread.cpp \
+     Time.cpp \
      platform/Implementation.cpp
 
 LIB=threads.a
diff --git a/xbmc/threads/Time.cpp b/xbmc/threads/Time.cpp
new file mode 100644 (file)
index 0000000..54d2d9f
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ *      Copyright (C) 2005-2011 Team XBMC
+ *      http://www.xbmc.org
+ *
+ *  This Program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This Program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with XBMC; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#include <stdint.h>
+
+#ifdef __APPLE__
+#if defined(__ppc__) || defined(__arm__)
+#include <mach/mach_time.h>
+#include <CoreVideo/CVHostTime.h>
+#else
+#include <time.h>
+#include "posix-realtime-stub.h"
+#endif
+#elif defined(_LINUX)
+#include <time.h>
+#elif defined(_WIN32)
+#include <windows.h>
+#endif
+
+namespace XbmcThreads
+{
+  unsigned int currentClockMillis()
+  {
+    uint64_t now_time;
+    static uint64_t start_time = 0;
+    static bool start_time_set = false;
+#ifdef _LINUX
+#if defined(__APPLE__) && (defined(__ppc__) || defined(__arm__))
+    now_time = CVGetCurrentHostTime() *  1000 / CVGetHostClockFrequency();
+#else
+    struct timespec ts = {};
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    now_time = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
+#endif
+#else
+    now_time = (uint64_t)timeGetTime();
+#endif
+    if (!start_time_set)
+    {
+      start_time = now_time;
+      start_time_set = true;
+    }
+    return (now_time - start_time);
+  }
+}
diff --git a/xbmc/threads/Time.h b/xbmc/threads/Time.h
new file mode 100644 (file)
index 0000000..8802c86
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ *      Copyright (C) 2005-2011 Team XBMC
+ *      http://www.xbmc.org
+ *
+ *  This Program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This Program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with XBMC; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#pragma once
+
+namespace XbmcThreads
+{
+  /**
+   * This function returns the system clock's number of milliseconds but with
+   *  an arbitrary reference point. It handles the wrapping of any underlying
+   *  system clock by setting a starting point at the first call. It should
+   *  only be used for measuring time durations.
+   *
+   * Of course, on windows it just calls timeGetTime, so you're on your own.
+   */
+  unsigned int currentClockMillis();
+}
index 5f721dc..fcf066a 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "TimeUtils.h"
 #include "XBDateTime.h"
+#include "threads/Time.h"
 
 #ifdef __APPLE__
 #if defined(__ppc__) || defined(__arm__)
@@ -79,22 +80,7 @@ unsigned int CTimeUtils::GetFrameTime()
 
 unsigned int CTimeUtils::GetTimeMS()
 {
-#ifdef _LINUX
-  uint64_t now_time;
-  static  uint64_t start_time = 0;
-#if defined(__APPLE__) && (defined(__ppc__) || defined(__arm__))
-    now_time = CVGetCurrentHostTime() *  1000 / CVGetHostClockFrequency();
-#else
-    struct timespec ts = {};
-    clock_gettime(CLOCK_MONOTONIC, &ts);
-    now_time = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
-#endif
-    if (start_time == 0)
-      start_time = now_time;
-    return (now_time - start_time);
-#else
-  return timeGetTime();
-#endif
+  return XbmcThreads::currentClockMillis();
 }
 
 CDateTime CTimeUtils::GetLocalTime(time_t time)