Moved silc_usleep from inline function to source file.
[runtime.git] / lib / silcutil / silctimer.c
index 903712748bae4627d3023cb9c204edfce8e2dde8..ce1c2bf11ad75832788783ec2e2b38d0e917c386 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2007 Pekka Riikonen
+  Copyright (C) 2007 - 2008 Pekka Riikonen
 
   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
@@ -17,7 +17,7 @@
 
 */
 
-#include "silc.h"
+#include "silcruntime.h"
 
 /* Start timer */
 
@@ -25,12 +25,13 @@ void silc_timer_start(SilcTimer timer)
 {
   struct timeval curtime;
 
-  memset(timer, 0, sizeof(timer));
-
   silc_gettimeofday(&curtime);
   timer->start_sec = curtime.tv_sec;
   timer->start_usec = curtime.tv_usec;
-
+  timer->timer_sec = 0;
+  timer->timer_usec = 0;
+  timer->sync_diff = 0;
+  timer->sync_tdiff = 0;
   timer->running = TRUE;
 }
 
@@ -48,6 +49,7 @@ void silc_timer_stop(SilcTimer timer)
   }
   timer->timer_sec = curtime.tv_sec - timer->start_sec;
   timer->timer_usec = curtime.tv_usec - timer->start_usec;
+  timer->timer_usec -= timer->sync_diff;
 
   timer->running = FALSE;
 }
@@ -90,6 +92,7 @@ void silc_timer_value(SilcTimer timer,
     }
     timer->timer_sec = curtime.tv_sec - timer->start_sec;
     timer->timer_usec = curtime.tv_usec - timer->start_usec;
+    timer->timer_usec -= timer->sync_diff;
   }
 
   if (elapsed_time_seconds)
@@ -125,3 +128,30 @@ SilcBool silc_timer_is_running(SilcTimer timer)
 {
   return timer->running;
 }
+
+/* Delay execution */
+
+void silc_usleep(unsigned long microseconds)
+{
+#ifdef SILC_UNIX
+#ifdef HAVE_NANOSLEEP
+  struct timespec tv;
+  tv.tv_sec = 0;
+  tv.tv_nsec = microseconds * 1000;
+#endif /* HAVE_NANOSLEEP */
+#endif /* SILC_UNIX */
+
+#ifdef SILC_UNIX
+#ifdef HAVE_NANOSLEEP
+  nanosleep(&tv, NULL);
+#else
+  usleep(microseconds);
+#endif /* HAVE_NANOSLEEP */
+#endif /* SILC_UNIX */
+#ifdef SILC_WIN32
+  Sleep(microseconds / 1000);
+#endif /* SILC_WIN32 */
+#ifdef SILC_SYMBIAN
+  silc_symbian_usleep(microseconds);
+#endif /* SILC_SYMBIAN */
+}