Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added xbt_os_time and xbt_os_sleep. Execute in the real and simulate systems.
[simgrid.git] / src / xbt / xbt_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 "xbt/log.h"
16 #include "gras/virtu.h"
17 #include "xbt/xbt_os_time.h" /* private */
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu);
20 double xbt_os_time(void) {
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 void xbt_os_sleep(double sec) {
33 #ifdef HAVE_USLEEP
34   DEBUG1("Do sleep %f sec", sec);
35   sleep(sec);
36   (void)usleep( (sec - floor(sec)) * 1000000);
37
38 #elif _WIN32
39      DEBUG1("Do sleep %f sec", sec);
40
41      Sleep((floor(sec) * 1000) +((sec - floor(sec)) * 1000));
42
43         
44 #else /* don't have usleep. Use select to sleep less than one second */
45   struct timeval timeout;
46
47   DEBUG1("Do sleep %f sec", sec);
48   
49   timeout.tv_sec =  (unsigned long)(sec);
50   timeout.tv_usec = (sec - floor(sec)) * 1000000;
51               
52   select(0, NULL, NULL, NULL, &timeout);
53 #endif
54 }