-#ifdef HAVE_POSIX_GETTIME
- return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec) +
- ((double) timer->elapse.tv_sec ) +
- ((((double) timer->stop.tv_nsec) -
- ((double) timer->start.tv_nsec) + ((double) timer->elapse.tv_nsec )) / 1e9);
-#elif defined(HAVE_GETTIMEOFDAY)
- return ((double) timer->stop.tv_sec) - ((double) timer->start.tv_sec)
- + ((double) timer->elapse.tv_sec ) +
- ((((double) timer->stop.tv_usec) -
- ((double) timer->start.tv_usec) + ((double) timer->elapse.tv_usec )) / 1000000.0);
+#if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
+ timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
+ timer->elapse.tv_nsec += timer->stop.tv_nsec - timer->start.tv_nsec;
+ clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->start));
+#elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
+ timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
+ timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
+ mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
+ thread_basic_info_data_t thi_data;
+ thread_basic_info_t thi = &thi_data;
+ thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
+ timer->start.tv_usec = thi->system_time.microseconds + thi->user_time.microseconds;
+ timer->start.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
+#elif HAVE_GETTIMEOFDAY
+ timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
+ timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
+ gettimeofday(&(timer->start), NULL);
+#elif defined(_WIN32)
+ timer->elapse.tv_sec += timer->stop.tv_sec - timer->start.tv_sec;
+ timer->elapse.tv_usec += timer->stop.tv_usec - timer->start.tv_usec;
+ HANDLE h = GetCurrentThread();
+ FILETIME creationTime, exitTime, kernelTime, userTime;
+ GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
+ w32_times_to_timeval(&timer->start, &kernelTime, &userTime);
+#else
+# error The thread timers of SimGrid do not seem to work on your platform.
+#endif
+}
+
+void xbt_os_threadtimer_stop(xbt_os_timer_t timer)
+{
+#if HAVE_POSIX_GETTIME && defined (_POSIX_THREAD_CPUTIME)
+ clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(timer->stop));
+#elif HAVE_GETTIMEOFDAY && defined(__MACH__) && defined(__APPLE__)
+ mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
+ thread_basic_info_data_t thi_data;
+ thread_basic_info_t thi = &thi_data;
+ thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
+ timer->stop.tv_usec = thi->system_time.microseconds + thi->user_time.microseconds;
+ timer->stop.tv_sec = thi->system_time.seconds + thi->user_time.seconds;
+#elif HAVE_GETTIMEOFDAY //if nothing else is available, return just time
+ gettimeofday(&(timer->stop), NULL);
+#elif defined(_WIN32)
+ HANDLE h = GetCurrentThread();
+ FILETIME creationTime, exitTime, kernelTime, userTime;
+ GetThreadTimes(h, &creationTime, &exitTime, &kernelTime, &userTime);
+ w32_times_to_timeval(&timer->stop, &kernelTime, &userTime);