3 /* sysdep.c -- all system dependency */
4 /* no system header should be loaded out of this file so that we have only */
5 /* one file to check when porting to another OS */
7 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "xbt/sysdep.h"
13 #include "xbt/xbt_os_time.h" /* this module */
16 #include <math.h> /* floor */
18 double xbt_os_time(void) {
19 #ifdef HAVE_GETTIMEOFDAY
22 gettimeofday(&tv, NULL);
24 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
27 return (double)(time(NULL));
28 #endif /* HAVE_GETTIMEOFDAY? */
31 void xbt_os_sleep(double sec) {
34 (void)usleep( (sec - floor(sec)) * 1000000);
38 Sleep((floor(sec) * 1000) +((sec - floor(sec)) * 1000));
41 #else /* don't have usleep. Use select to sleep less than one second */
42 struct timeval timeout;
45 timeout.tv_sec = (unsigned long)(sec);
46 timeout.tv_usec = (sec - floor(sec)) * 1000000;
48 select(0, NULL, NULL, NULL, &timeout);
54 XBT_PUBLIC(void) xbt_free_f(void* p)
60 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
61 disabled in SDL source code */
64 /* \defgroup XBT_sysdep All system dependency
65 * \brief This section describes many macros/functions that can serve as
70 double xbt_os_time(void) {
71 #ifdef HAVE_GETTIMEOFDAY
74 gettimeofday(&tv, NULL);
76 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
79 /* return (double)(time(NULL));*/
80 //#endif /* HAVE_GETTIMEOFDAY? */
83 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");*/
86 struct s_xbt_os_timer {
87 #ifdef HAVE_GETTIMEOFDAY
88 struct timeval start,stop;
90 unsigned long int start,stop;
94 xbt_os_timer_t xbt_os_timer_new(void) {
95 return xbt_new0(struct s_xbt_os_timer,1);
97 void xbt_os_timer_free(xbt_os_timer_t timer) {
100 void xbt_os_timer_start(xbt_os_timer_t timer) {
101 #ifdef HAVE_GETTIMEOFDAY
102 gettimeofday(&(timer->start), NULL);
104 timer->start = (unsigned long int)(time(NULL));
107 void xbt_os_timer_stop(xbt_os_timer_t timer) {
108 #ifdef HAVE_GETTIMEOFDAY
109 gettimeofday(&(timer->stop), NULL);
111 timer->stop = (unsigned long int)(time(NULL));
114 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
115 #ifdef HAVE_GETTIMEOFDAY
116 return ((double)timer->stop.tv_sec) - ((double)timer->start.tv_sec) +
117 ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
119 return (double)timer->stop - (double)timer->start;