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 */
20 #include <sys\timeb.h>
24 gettimeofday(struct timeval *tv, struct timezone *tz)
27 #if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
40 #if defined(WIN32_WCE) || (_WIN32_WINNT < 0x0400)
44 tv->tv_usec = tm.millitm * 1000;
46 GetSystemTimeAsFileTime (&ft);
47 tm = (unsigned __int64)ft.dwHighDateTime << 32;
48 tm |= ft.dwLowDateTime;
50 tm -= 11644473600000000ULL;
52 tv->tv_sec = (long) (tm / 1000000L);
53 tv->tv_usec = (long) (tm % 1000000L);
62 double xbt_os_time(void) {
63 #ifdef HAVE_GETTIMEOFDAY
66 gettimeofday(&tv, NULL);
68 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
71 return (double)(time(NULL));
72 #endif /* HAVE_GETTIMEOFDAY? */
75 void xbt_os_sleep(double sec) {
78 (void)usleep( (sec - floor(sec)) * 1000000);
82 Sleep((floor(sec) * 1000) +((sec - floor(sec)) * 1000));
85 #else /* don't have usleep. Use select to sleep less than one second */
86 struct timeval timeout;
89 timeout.tv_sec = (unsigned long)(sec);
90 timeout.tv_usec = (sec - floor(sec)) * 1000000;
92 select(0, NULL, NULL, NULL, &timeout);
98 XBT_PUBLIC(void) xbt_free_f(void* p)
104 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
105 disabled in SDL source code */
108 /* \defgroup XBT_sysdep All system dependency
109 * \brief This section describes many macros/functions that can serve as
114 double xbt_os_time(void) {
115 #ifdef HAVE_GETTIMEOFDAY
118 gettimeofday(&tv, NULL);
120 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
122 /* Poor resolution */
123 /* return (double)(time(NULL));*/
124 //#endif /* HAVE_GETTIMEOFDAY? */
127 /*XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");*/
130 struct s_xbt_os_timer {
131 #ifdef HAVE_GETTIMEOFDAY
132 struct timeval start,stop;
134 unsigned long int start,stop;
138 xbt_os_timer_t xbt_os_timer_new(void) {
139 return xbt_new0(struct s_xbt_os_timer,1);
141 void xbt_os_timer_free(xbt_os_timer_t timer) {
144 void xbt_os_timer_start(xbt_os_timer_t timer) {
145 #ifdef HAVE_GETTIMEOFDAY
146 gettimeofday(&(timer->start), NULL);
148 timer->start = (unsigned long int)(time(NULL));
151 void xbt_os_timer_stop(xbt_os_timer_t timer) {
152 #ifdef HAVE_GETTIMEOFDAY
153 gettimeofday(&(timer->stop), NULL);
155 timer->stop = (unsigned long int)(time(NULL));
158 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
159 #ifdef HAVE_GETTIMEOFDAY
160 return ((double)timer->stop.tv_sec) - ((double)timer->start.tv_sec) +
161 ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
163 return (double)timer->stop - (double)timer->start;