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