sal/osl/unx/salinit.cxx |    4 ++
 sal/osl/unx/time.c      |   93 ++++++++++++++++++++++++++++++++++++------------
 sal/osl/w32/salinit.cxx |    4 ++
 sal/osl/w32/time.c      |   12 ++----
 4 files changed, 83 insertions(+), 30 deletions(-)

New commits:
commit d8d55787b81cdc955b73c8befa4ab608f46e32aa
Author: Norbert Thiebaud <nthieb...@gmail.com>
Date:   Sun May 19 18:09:21 2013 -0500

    Thread-safe version of osl_getGlobalTime
    
    Change-Id: Ibb9d23780600437f607d866ff3d396b96879245d
    Reviewed-on: https://gerrit.libreoffice.org/3960
    Tested-by: LibreOffice gerrit bot <ger...@libreoffice.org>
    Reviewed-by: Fridrich Strba <fridr...@documentfoundation.org>
    Tested-by: Fridrich Strba <fridr...@documentfoundation.org>

diff --git a/sal/osl/unx/salinit.cxx b/sal/osl/unx/salinit.cxx
index 327ca0e..c71c6a3 100644
--- a/sal/osl/unx/salinit.cxx
+++ b/sal/osl/unx/salinit.cxx
@@ -38,6 +38,9 @@ extern bool sal_use_syslog;
 
 extern "C" {
 
+//From time.c
+void sal_initGlobalTimer();
+
 void sal_detail_initialize(int argc, char ** argv) {
 #if defined MACOSX
     // On Mac OS X, soffice can restart itself via exec (see restartOnMac in
@@ -64,6 +67,7 @@ void sal_detail_initialize(int argc, char ** argv) {
         close(fd);
     }
 #endif
+    sal_initGlobalTimer();
 #if HAVE_SYSLOG_H
     const char *use_syslog = getenv("SAL_LOG_SYSLOG");
     sal_use_syslog = use_syslog != NULL && !strcmp(use_syslog, "1");
diff --git a/sal/osl/unx/time.c b/sal/osl/unx/time.c
index c99036b..70512f2 100644
--- a/sal/osl/unx/time.c
+++ b/sal/osl/unx/time.c
@@ -23,6 +23,12 @@
 #include <osl/diagnose.h>
 #include <osl/time.h>
 #include <time.h>
+#include <assert.h>
+#include <unistd.h>
+
+#if defined(MACOSX)
+#include <mach/mach_time.h>
+#endif
 
 /* FIXME: detection should be done in configure script */
 #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || \
@@ -33,20 +39,36 @@
 #define HAS_ALTZONE 1
 #endif
 
+#if defined(MACOSX)
+typedef sal_uInt64 osl_time_t;
+static double adjust_time_factor;
+#else
+#if defined(_POSIX_TIMERS)
+#define USE_CLOCK_GETTIME
+typedef struct timespec osl_time_t;
+#else
+typedef struct timeval osl_time_t;
+#endif
+#endif
+static osl_time_t startTime;
+
+
 /*--------------------------------------------------
  * osl_getSystemTime
  *-------------------------------------------------*/
 
 sal_Bool SAL_CALL osl_getSystemTime(TimeValue* tv)
 {
+#if defined(MACOSX)
+    double diff = (double)(mach_absolute_time() - startTime) * 
adjust_time_factor;
+    tv->Seconds = (sal_uInt32)diff;
+    tv->Nanosec = (sal_uInt32)((diff - tv->Seconds) * 1e9);
+#else
     int res;
-#if defined(LINUX)
-    struct timespec tp;
-
+    osl_time_t tp;
+#if defined(USE_CLOCK_GETTIME)
     res = clock_gettime(CLOCK_REALTIME, &tp);
 #else
-    struct timeval tp;
-
     res = gettimeofday(&tp, NULL);
 #endif
 
@@ -56,12 +78,12 @@ sal_Bool SAL_CALL osl_getSystemTime(TimeValue* tv)
     }
 
     tv->Seconds = tp.tv_sec;
-    #if defined(LINUX)
+    #if defined(USE_CLOCK_GETTIME)
     tv->Nanosec = tp.tv_nsec;
     #else
     tv->Nanosec = tp.tv_usec * 1000;
     #endif
-
+#endif
     return sal_True;
 }
 
@@ -253,28 +275,55 @@ sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( 
TimeValue* pLocalTimeVal, Time
     return sal_False;
 }
 
-
-
-static struct timeval startTime;
-static sal_Bool bGlobalTimer = sal_False;
+void sal_initGlobalTimer()
+{
+#if defined(MACOSX)
+  mach_timebase_info_data_t timebase;
+  mach_timebase_info(&timebase);
+  adjust_time_factor = 1e-9 * (double)timebase.numer / 
(double)(timebase.denom);
+  startTime = mach_absolute_time();
+#else /* NDef MACOSX */
+  int res;
+#if defined(USE_CLOCK_GETTIME)
+  res = clock_gettime(CLOCK_REALTIME, &startTime);
+#else /* Ndef USE_CLOCK_GETTIME */
+  res = gettimeofday( &startTime, NULL );
+#endif /* NDef USE_CLOCK_GETTIME */
+  assert(res == 0);
+#endif /* NDef MACOSX */
+}
 
 sal_uInt32 SAL_CALL osl_getGlobalTimer()
 {
-  struct timeval currentTime;
-  sal_uInt32 nSeconds;
+    sal_uInt32 nSeconds;
 
-  // FIXME: not thread safe !!
-  if ( bGlobalTimer == sal_False )
-  {
-      gettimeofday( &startTime, NULL );
-      bGlobalTimer=sal_True;
-  }
+#if defined(MACOSX)
+    startTime = mach_absolute_time();
 
-  gettimeofday( &currentTime, NULL );
+    double diff = (double)(mach_absolute_time() - startTime) * 
adjust_time_factor * 1000;
+    nSeconds = (sal_uInt32)diff;
+#else
+    osl_time_t currentTime;
+    int res;
 
-  nSeconds = (sal_uInt32)( currentTime.tv_sec - startTime.tv_sec );
+#if defined(USE_CLOCK_GETTIME)
+    res = clock_gettime(CLOCK_REALTIME, &startTime);
+#else
+    res = gettimeofday( &startTime, NULL );
+#endif
+    assert(res == 0);
+
+    if (res != 0)
+        return 0;
 
-  return ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - 
startTime.tv_usec) / 1000 );
+    nSeconds = (sal_uInt32)( currentTime.tv_sec - startTime.tv_sec );
+#if defined(USE_CLOCK_GETTIME)
+    nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_nsec - 
startTime.tv_nsec) / 1000000 );
+#else
+    nSeconds = ( nSeconds * 1000 ) + (long) (( currentTime.tv_usec - 
startTime.tv_usec) / 1000 );
+#endif
+#endif
+    return nSeconds;
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/osl/w32/salinit.cxx b/sal/osl/w32/salinit.cxx
index e392f4a..3eb9290 100644
--- a/sal/osl/w32/salinit.cxx
+++ b/sal/osl/w32/salinit.cxx
@@ -31,6 +31,9 @@
 extern "C" {
 #endif
 
+//From time.c
+void sal_initGlobalTimer();
+
 // _set_invalid_parameter_handler appears unavailable with MinGW:
 #if defined _MSC_VER
 namespace {
@@ -52,6 +55,7 @@ extern "C" void invalidParameterHandler(
 
 void sal_detail_initialize(int argc, char ** argv)
 {
+    sal_initGlobalTimer();
     // SetProcessDEPPolicy(PROCESS_DEP_ENABLE);
     // SetDllDirectoryW(L"");
     // SetSearchPathMode(
diff --git a/sal/osl/w32/time.c b/sal/osl/w32/time.c
index 49f33c2..fc8855b 100644
--- a/sal/osl/w32/time.c
+++ b/sal/osl/w32/time.c
@@ -184,21 +184,17 @@ sal_Bool SAL_CALL osl_getSystemTimeFromLocalTime( 
TimeValue* pLocalTimeVal, Time
     return sal_False;
 }
 
-
 static struct _timeb startTime;
-static sal_Bool bGlobalTimer = sal_False;
+void sal_initGlobalTimer()
+{
+    _ftime( &startTime );
+}
 
 sal_uInt32 SAL_CALL osl_getGlobalTimer(void)
 {
   struct _timeb currentTime;
   sal_uInt32 nSeconds;
 
-  if ( bGlobalTimer == sal_False )
-  {
-      _ftime( &startTime );
-      bGlobalTimer=sal_True;
-  }
-
   _ftime( &currentTime );
 
   nSeconds = (sal_uInt32)( currentTime.time - startTime.time );
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to