1 // boost thread_clock.cpp -----------------------------------------------------------//
3 // Copyright Beman Dawes 1994, 2006, 2008
4 // Copyright Vicente J. Botet Escriba 2009-2011
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
9 // See http://www.boost.org/libs/chrono for documentation.
11 //--------------------------------------------------------------------------------------//
13 #include <boost/chrono/config.hpp>
14 #include <boost/chrono/thread_clock.hpp>
16 #include <boost/assert.hpp>
18 #if !defined(__VXWORKS__)
19 # include <sys/times.h>
24 namespace boost { namespace chrono {
26 thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT
29 #if defined CLOCK_THREAD_CPUTIME_ID
30 // get the timespec associated to the thread clock
31 if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
33 // get the current thread
34 pthread_t pth=pthread_self();
35 // get the clock_id associated to the current thread
37 pthread_getcpuclockid(pth, &clock_id);
38 // get the timespec associated to the thread clock
39 if ( ::clock_gettime( clock_id, &ts ) )
42 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
45 // transform to nanoseconds
46 return time_point(duration(
47 static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
51 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
52 thread_clock::time_point thread_clock::now( system::error_code & ec )
55 #if defined CLOCK_THREAD_CPUTIME_ID
56 // get the timespec associated to the thread clock
57 if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
59 // get the current thread
60 pthread_t pth=pthread_self();
61 // get the clock_id associated to the current thread
63 pthread_getcpuclockid(pth, &clock_id);
64 // get the timespec associated to the thread clock
65 if ( ::clock_gettime( clock_id, &ts ) )
68 if (BOOST_CHRONO_IS_THROWS(ec))
70 boost::throw_exception(
73 BOOST_CHRONO_SYSTEM_CATEGORY,
74 "chrono::thread_clock" ));
78 ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
82 if (!BOOST_CHRONO_IS_THROWS(ec))
86 // transform to nanoseconds
87 return time_point(duration(
88 static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));