Chris@16: #ifndef BOOST_THREAD_PTHREAD_TIMESPEC_HPP Chris@16: #define BOOST_THREAD_PTHREAD_TIMESPEC_HPP Chris@16: // (C) Copyright 2007-8 Anthony Williams Chris@16: // (C) Copyright 2012 Vicente J. Botet Escriba Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #if defined BOOST_THREAD_USES_DATETIME Chris@16: #include Chris@16: #endif Chris@16: #include Chris@16: #ifndef _WIN32 Chris@16: #include Chris@16: #endif Chris@16: #ifdef BOOST_THREAD_USES_CHRONO Chris@16: #include Chris@16: #endif Chris@16: Chris@16: #if defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) Chris@16: # define BOOST_THREAD_TIMESPEC_MAC_API Chris@16: #include //for gettimeofday and timeval Chris@16: #else Chris@16: #include // for clock_gettime Chris@16: #endif Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace detail Chris@16: { Chris@16: #if defined BOOST_THREAD_USES_DATETIME Chris@16: inline struct timespec to_timespec(boost::system_time const& abs_time) Chris@16: { Chris@16: struct timespec timeout = { 0,0}; Chris@16: boost::posix_time::time_duration const time_since_epoch=abs_time-boost::posix_time::from_time_t(0); Chris@16: Chris@16: timeout.tv_sec=time_since_epoch.total_seconds(); Chris@16: timeout.tv_nsec=(long)(time_since_epoch.fractional_seconds()*(1000000000l/time_since_epoch.ticks_per_second())); Chris@16: return timeout; Chris@16: } Chris@16: #endif Chris@16: #if defined BOOST_THREAD_USES_CHRONO Chris@16: inline timespec to_timespec(chrono::nanoseconds const& ns) Chris@16: { Chris@16: struct timespec ts; Chris@16: ts.tv_sec = static_cast(chrono::duration_cast(ns).count()); Chris@16: ts.tv_nsec = static_cast((ns - chrono::duration_cast(ns)).count()); Chris@16: return ts; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: inline timespec to_timespec(boost::intmax_t const& ns) Chris@16: { Chris@16: boost::intmax_t s = ns / 1000000000l; Chris@16: struct timespec ts; Chris@16: ts.tv_sec = static_cast (s); Chris@16: ts.tv_nsec = static_cast (ns - s * 1000000000l); Chris@16: return ts; Chris@16: } Chris@16: inline boost::intmax_t to_nanoseconds_int_max(timespec const& ts) Chris@16: { Chris@16: return static_cast(ts.tv_sec) * 1000000000l + ts.tv_nsec; Chris@16: } Chris@16: inline bool timespec_ge_zero(timespec const& ts) Chris@16: { Chris@16: return (ts.tv_sec >= 0) || (ts.tv_nsec >= 0); Chris@16: } Chris@16: inline timespec timespec_now() Chris@16: { Chris@16: timespec ts; Chris@16: Chris@16: #if defined(BOOST_THREAD_TIMESPEC_MAC_API) Chris@16: timeval tv; Chris@16: ::gettimeofday(&tv, 0); Chris@16: ts.tv_sec = tv.tv_sec; Chris@16: ts.tv_nsec = tv.tv_usec * 1000; Chris@16: #else Chris@16: if ( ::clock_gettime( CLOCK_REALTIME, &ts ) ) Chris@16: { Chris@16: BOOST_ASSERT(0 && "Boost::Thread - Internal Error"); Chris@16: } Chris@16: #endif Chris@16: return ts; Chris@16: } Chris@16: inline timespec timespec_zero() Chris@16: { Chris@16: timespec ts; Chris@16: ts.tv_sec = 0; Chris@16: ts.tv_nsec = 0; Chris@16: return ts; Chris@16: } Chris@16: inline timespec timespec_plus(timespec const& lhs, timespec const& rhs) Chris@16: { Chris@16: return to_timespec(to_nanoseconds_int_max(lhs) + to_nanoseconds_int_max(rhs)); Chris@16: } Chris@16: inline timespec timespec_minus(timespec const& lhs, timespec const& rhs) Chris@16: { Chris@16: return to_timespec(to_nanoseconds_int_max(lhs) - to_nanoseconds_int_max(rhs)); Chris@16: } Chris@16: inline bool timespec_gt(timespec const& lhs, timespec const& rhs) Chris@16: { Chris@16: return to_nanoseconds_int_max(lhs) > to_nanoseconds_int_max(rhs); Chris@16: } Chris@16: inline bool timespec_ge(timespec const& lhs, timespec const& rhs) Chris@16: { Chris@16: return to_nanoseconds_int_max(lhs) >= to_nanoseconds_int_max(rhs); Chris@16: } Chris@16: Chris@16: } Chris@16: } Chris@16: Chris@16: #include Chris@16: Chris@16: #endif