Chris@16
|
1 // boost thread_clock.cpp -----------------------------------------------------------//
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Beman Dawes 1994, 2006, 2008
|
Chris@16
|
4 // Copyright Vicente J. Botet Escriba 2009-2011
|
Chris@16
|
5
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
7 // See http://www.boost.org/LICENSE_1_0.txt
|
Chris@16
|
8
|
Chris@16
|
9 // See http://www.boost.org/libs/chrono for documentation.
|
Chris@16
|
10
|
Chris@16
|
11 //--------------------------------------------------------------------------------------//
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/chrono/config.hpp>
|
Chris@16
|
14 #include <boost/chrono/thread_clock.hpp>
|
Chris@16
|
15 #include <cassert>
|
Chris@16
|
16
|
Chris@16
|
17 #if !defined(__VXWORKS__)
|
Chris@16
|
18 # include <sys/times.h>
|
Chris@16
|
19 #endif
|
Chris@16
|
20 # include <pthread.h>
|
Chris@16
|
21 # include <unistd.h>
|
Chris@16
|
22
|
Chris@16
|
23 namespace boost { namespace chrono {
|
Chris@16
|
24
|
Chris@16
|
25 thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT
|
Chris@16
|
26 {
|
Chris@16
|
27 struct timespec ts;
|
Chris@16
|
28 #if defined CLOCK_THREAD_CPUTIME_ID
|
Chris@16
|
29 // get the timespec associated to the thread clock
|
Chris@16
|
30 if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
|
Chris@16
|
31 #else
|
Chris@16
|
32 // get the current thread
|
Chris@16
|
33 pthread_t pth=pthread_self();
|
Chris@16
|
34 // get the clock_id associated to the current thread
|
Chris@16
|
35 clockid_t clock_id;
|
Chris@16
|
36 pthread_getcpuclockid(pth, &clock_id);
|
Chris@16
|
37 // get the timespec associated to the thread clock
|
Chris@16
|
38 if ( ::clock_gettime( clock_id, &ts ) )
|
Chris@16
|
39 #endif
|
Chris@16
|
40 {
|
Chris@16
|
41 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
|
Chris@16
|
42 }
|
Chris@16
|
43
|
Chris@16
|
44 // transform to nanoseconds
|
Chris@16
|
45 return time_point(duration(
|
Chris@16
|
46 static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
|
Chris@16
|
47
|
Chris@16
|
48 }
|
Chris@16
|
49
|
Chris@16
|
50 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
|
Chris@16
|
51 thread_clock::time_point thread_clock::now( system::error_code & ec )
|
Chris@16
|
52 {
|
Chris@16
|
53 struct timespec ts;
|
Chris@16
|
54 #if defined CLOCK_THREAD_CPUTIME_ID
|
Chris@16
|
55 // get the timespec associated to the thread clock
|
Chris@16
|
56 if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
|
Chris@16
|
57 #else
|
Chris@16
|
58 // get the current thread
|
Chris@16
|
59 pthread_t pth=pthread_self();
|
Chris@16
|
60 // get the clock_id associated to the current thread
|
Chris@16
|
61 clockid_t clock_id;
|
Chris@16
|
62 pthread_getcpuclockid(pth, &clock_id);
|
Chris@16
|
63 // get the timespec associated to the thread clock
|
Chris@16
|
64 if ( ::clock_gettime( clock_id, &ts ) )
|
Chris@16
|
65 #endif
|
Chris@16
|
66 {
|
Chris@16
|
67 if (BOOST_CHRONO_IS_THROWS(ec))
|
Chris@16
|
68 {
|
Chris@16
|
69 boost::throw_exception(
|
Chris@16
|
70 system::system_error(
|
Chris@16
|
71 errno,
|
Chris@16
|
72 BOOST_CHRONO_SYSTEM_CATEGORY,
|
Chris@16
|
73 "chrono::thread_clock" ));
|
Chris@16
|
74 }
|
Chris@16
|
75 else
|
Chris@16
|
76 {
|
Chris@16
|
77 ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
|
Chris@16
|
78 return time_point();
|
Chris@16
|
79 }
|
Chris@16
|
80 }
|
Chris@16
|
81 if (!BOOST_CHRONO_IS_THROWS(ec))
|
Chris@16
|
82 {
|
Chris@16
|
83 ec.clear();
|
Chris@16
|
84 }
|
Chris@16
|
85 // transform to nanoseconds
|
Chris@16
|
86 return time_point(duration(
|
Chris@16
|
87 static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
|
Chris@16
|
88
|
Chris@16
|
89 }
|
Chris@16
|
90 #endif
|
Chris@16
|
91 } }
|