Chris@16: // boost thread_clock.cpp -----------------------------------------------------------// Chris@16: Chris@16: // Copyright Beman Dawes 1994, 2006, 2008 Chris@16: // Copyright Vicente J. Botet Escriba 2009-2011 Chris@16: Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // See http://www.boost.org/LICENSE_1_0.txt Chris@16: Chris@16: // See http://www.boost.org/libs/chrono for documentation. Chris@16: Chris@16: //--------------------------------------------------------------------------------------// Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if !defined(__VXWORKS__) Chris@16: # include Chris@16: #endif Chris@16: # include Chris@16: # include Chris@16: Chris@16: namespace boost { namespace chrono { Chris@16: Chris@16: thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT Chris@16: { Chris@16: struct timespec ts; Chris@16: #if defined CLOCK_THREAD_CPUTIME_ID Chris@16: // get the timespec associated to the thread clock Chris@16: if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) ) Chris@16: #else Chris@16: // get the current thread Chris@16: pthread_t pth=pthread_self(); Chris@16: // get the clock_id associated to the current thread Chris@16: clockid_t clock_id; Chris@16: pthread_getcpuclockid(pth, &clock_id); Chris@16: // get the timespec associated to the thread clock Chris@16: if ( ::clock_gettime( clock_id, &ts ) ) Chris@16: #endif Chris@16: { Chris@16: BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); Chris@16: } Chris@16: Chris@16: // transform to nanoseconds Chris@16: return time_point(duration( Chris@16: static_cast( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); Chris@16: Chris@16: } Chris@16: Chris@16: #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING Chris@16: thread_clock::time_point thread_clock::now( system::error_code & ec ) Chris@16: { Chris@16: struct timespec ts; Chris@16: #if defined CLOCK_THREAD_CPUTIME_ID Chris@16: // get the timespec associated to the thread clock Chris@16: if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) ) Chris@16: #else Chris@16: // get the current thread Chris@16: pthread_t pth=pthread_self(); Chris@16: // get the clock_id associated to the current thread Chris@16: clockid_t clock_id; Chris@16: pthread_getcpuclockid(pth, &clock_id); Chris@16: // get the timespec associated to the thread clock Chris@16: if ( ::clock_gettime( clock_id, &ts ) ) Chris@16: #endif Chris@16: { Chris@16: if (BOOST_CHRONO_IS_THROWS(ec)) Chris@16: { Chris@16: boost::throw_exception( Chris@16: system::system_error( Chris@16: errno, Chris@16: BOOST_CHRONO_SYSTEM_CATEGORY, Chris@16: "chrono::thread_clock" )); Chris@16: } Chris@16: else Chris@16: { Chris@16: ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); Chris@16: return time_point(); Chris@16: } Chris@16: } Chris@16: if (!BOOST_CHRONO_IS_THROWS(ec)) Chris@16: { Chris@16: ec.clear(); Chris@16: } Chris@16: // transform to nanoseconds Chris@16: return time_point(duration( Chris@16: static_cast( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); Chris@16: Chris@16: } Chris@16: #endif Chris@16: } }