]>
Commit | Line | Data |
---|---|---|
1 | // boost thread_clock.cpp -----------------------------------------------------------// | |
2 | ||
3 | // Copyright Beman Dawes 1994, 2006, 2008 | |
4 | // Copyright Vicente J. Botet Escriba 2009-2011 | |
5 | // Copyright Christopher Brown 2013 | |
6 | ||
7 | // Distributed under the Boost Software License, Version 1.0. | |
8 | // See http://www.boost.org/LICENSE_1_0.txt | |
9 | ||
10 | // See http://www.boost.org/libs/chrono for documentation. | |
11 | ||
12 | //--------------------------------------------------------------------------------------// | |
13 | ||
14 | #include <boost/chrono/config.hpp> | |
15 | #include <boost/chrono/thread_clock.hpp> | |
16 | #include <cassert> | |
17 | #include <boost/assert.hpp> | |
18 | ||
19 | # include <pthread.h> | |
20 | # include <mach/thread_act.h> | |
21 | ||
22 | namespace boost { namespace chrono { | |
23 | ||
24 | thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT | |
25 | { | |
26 | // get the thread port (borrowing pthread's reference) | |
27 | mach_port_t port = pthread_mach_thread_np(pthread_self()); | |
28 | ||
29 | // get the thread info | |
30 | thread_basic_info_data_t info; | |
31 | mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; | |
32 | if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS ) | |
33 | { | |
34 | BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); | |
35 | return time_point(); | |
36 | } | |
37 | ||
38 | // convert to nanoseconds | |
39 | duration user = duration( | |
40 | static_cast<thread_clock::rep>( info.user_time.seconds ) * 1000000000 | |
41 | + static_cast<thread_clock::rep>(info.user_time.microseconds ) * 1000); | |
42 | ||
43 | duration system = duration( | |
44 | static_cast<thread_clock::rep>( info.system_time.seconds ) * 1000000000 | |
45 | + static_cast<thread_clock::rep>( info.system_time.microseconds ) * 1000); | |
46 | ||
47 | return time_point( user + system ); | |
48 | } | |
49 | ||
50 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
51 | thread_clock::time_point thread_clock::now( system::error_code & ec ) | |
52 | { | |
53 | // get the thread port (borrowing pthread's reference) | |
54 | mach_port_t port = pthread_mach_thread_np(pthread_self()); | |
55 | ||
56 | // get the thread info | |
57 | thread_basic_info_data_t info; | |
58 | mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; | |
59 | if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS ) | |
60 | { | |
61 | if (BOOST_CHRONO_IS_THROWS(ec)) | |
62 | { | |
63 | boost::throw_exception( | |
64 | system::system_error( | |
65 | EINVAL, | |
66 | BOOST_CHRONO_SYSTEM_CATEGORY, | |
67 | "chrono::thread_clock" )); | |
68 | } | |
69 | else | |
70 | { | |
71 | ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); | |
72 | return time_point(); | |
73 | } | |
74 | } | |
75 | if (!BOOST_CHRONO_IS_THROWS(ec)) | |
76 | { | |
77 | ec.clear(); | |
78 | } | |
79 | ||
80 | // convert to nanoseconds | |
81 | duration user = duration( | |
82 | static_cast<thread_clock::rep>( info.user_time.seconds ) * 1000000000 | |
83 | + static_cast<thread_clock::rep>(info.user_time.microseconds ) * 1000); | |
84 | ||
85 | duration system = duration( | |
86 | static_cast<thread_clock::rep>( info.system_time.seconds ) * 1000000000 | |
87 | + static_cast<thread_clock::rep>( info.system_time.microseconds ) * 1000); | |
88 | ||
89 | return time_point( user + system ); | |
90 | } | |
91 | #endif | |
92 | } } |