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_portability.h" /* private */
15 #include "xbt/error.h"
19 /* TSC (tick-level) timers are said to be unreliable on SMP hosts and thus
20 disabled in SDL source code */
23 /* \defgroup XBT_sysdep All system dependency
24 * \brief This section describes many macros/functions that can serve as
28 double xbt_os_time(void) {
29 #ifdef HAVE_GETTIMEOFDAY
32 gettimeofday(&tv, NULL);
34 return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
37 return (double)(time(NULL));
38 #endif /* HAVE_GETTIMEOFDAY? */
41 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sysdep, xbt, "System dependency");
44 struct s_xbt_os_timer {
45 #ifdef HAVE_GETTIMEOFDAY
46 struct timeval start,stop;
48 unsigned long int start,stop;
52 xbt_os_timer_t xbt_os_timer_new(void) {
53 return xbt_new0(struct s_xbt_os_timer,1);
55 void xbt_os_timer_free(xbt_os_timer_t timer) {
58 void xbt_os_timer_start(xbt_os_timer_t timer) {
59 #ifdef HAVE_GETTIMEOFDAY
60 gettimeofday(&(timer->start), NULL);
62 timer->start = (unsigned long int)(time(NULL));
65 void xbt_os_timer_stop(xbt_os_timer_t timer) {
66 #ifdef HAVE_GETTIMEOFDAY
67 gettimeofday(&(timer->stop), NULL);
69 timer->stop = (unsigned long int)(time(NULL));
72 double xbt_os_timer_elapsed(xbt_os_timer_t timer) {
73 #ifdef HAVE_GETTIMEOFDAY
74 return ((double)timer->stop.tv_sec) - ((double)timer->start.tv_sec) +
75 ((((double)timer->stop.tv_usec) - ((double)timer->start.tv_usec)) / 1000000.0);
77 return (double)timer->stop - (double)timer->start;