1 // boost thread_clock.cpp -----------------------------------------------------------//
3 // Copyright Beman Dawes 1994, 2006, 2008
4 // Copyright Vicente J. Botet Escriba 2009-2011
5 // Copyright Christopher Brown 2013
7 // Distributed under the Boost Software License, Version 1.0.
8 // See http://www.boost.org/LICENSE_1_0.txt
10 // See http://www.boost.org/libs/chrono for documentation.
12 //--------------------------------------------------------------------------------------//
14 #include <boost/chrono/config.hpp>
15 #include <boost/chrono/thread_clock.hpp>
17 #include <boost/assert.hpp>
20 # include <mach/thread_act.h>
22 namespace boost { namespace chrono {
24 thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT
26 // get the thread port (borrowing pthread's reference)
27 mach_port_t port = pthread_mach_thread_np(pthread_self());
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 )
34 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
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);
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);
47 return time_point( user + system );
50 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
51 thread_clock::time_point thread_clock::now( system::error_code & ec )
53 // get the thread port (borrowing pthread's reference)
54 mach_port_t port = pthread_mach_thread_np(pthread_self());
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 )
61 if (BOOST_CHRONO_IS_THROWS(ec))
63 boost::throw_exception(
66 BOOST_CHRONO_SYSTEM_CATEGORY,
67 "chrono::thread_clock" ));
71 ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
75 if (!BOOST_CHRONO_IS_THROWS(ec))
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);
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);
89 return time_point( user + system );