Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid segfault or infinite loop when calling this function from somewhere else than...
[simgrid.git] / src / gras / Virtu / rl_time.c
1 /* $Id$ */
2
3 /* time - time related syscal wrappers                                      */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "math.h" /* floor */
11
12 #include "portable.h"
13
14 #include "xbt/sysdep.h"
15 #include "gras/virtu.h"
16
17 XBT_LOG_EXTERNAL_CATEGORY(virtu);
18 XBT_LOG_DEFAULT_CATEGORY(virtu);
19
20 double gras_os_time() {
21 #ifdef HAVE_GETTIMEOFDAY
22   struct timeval tv;
23
24   gettimeofday(&tv, NULL);
25
26   return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
27 #else
28   /* Poor resolution */
29   return (double)(time(NULL)); 
30 #endif /* HAVE_GETTIMEOFDAY? */ 
31         
32 }
33  
34 void gras_os_sleep(double sec) {
35   DEBUG1("Do sleep %d sec", (int)sec);
36   sleep(sec);
37
38 #ifdef HAVE_USLEEP
39   DEBUG1("Do sleep %d usec", (int) ((sec - floor(sec)) * 1000000 ));
40   (void)usleep( (sec - floor(sec)) * 1000000);
41 #else
42   if ( ((int) sec) == 0) {
43      WARN0("This platform does not implement usleep. Cannot sleep less than one second");
44   }
45 #endif /* ! HAVE_USLEEP */
46 }