Chris@16: // boost thread_clock.cpp -----------------------------------------------------------// Chris@16: Chris@101: // Copyright Beman Dawes 1994, 2006, 2008 Chris@101: // Copyright Vicente J. Botet Escriba 2009-2011 Chris@101: // Copyright Christopher Brown 2013 Chris@16: Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // See http://www.boost.org/LICENSE_1_0.txt Chris@16: Chris@16: // See http://www.boost.org/libs/chrono for documentation. Chris@16: Chris@16: //--------------------------------------------------------------------------------------// Chris@16: Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: Chris@101: # include Chris@101: # include Chris@101: Chris@101: namespace boost { namespace chrono { Chris@101: Chris@101: thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT Chris@101: { Chris@101: // get the thread port (borrowing pthread's reference) Chris@101: mach_port_t port = pthread_mach_thread_np(pthread_self()); Chris@101: Chris@101: // get the thread info Chris@101: thread_basic_info_data_t info; Chris@101: mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; Chris@101: if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS ) Chris@101: { Chris@101: BOOST_ASSERT(0 && "Boost::Chrono - Internal Error"); Chris@101: return time_point(); Chris@101: } Chris@101: Chris@101: // convert to nanoseconds Chris@101: duration user = duration( Chris@101: static_cast( info.user_time.seconds ) * 1000000000 Chris@101: + static_cast(info.user_time.microseconds ) * 1000); Chris@101: Chris@101: duration system = duration( Chris@101: static_cast( info.system_time.seconds ) * 1000000000 Chris@101: + static_cast( info.system_time.microseconds ) * 1000); Chris@101: Chris@101: return time_point( user + system ); Chris@101: } Chris@101: Chris@101: #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING Chris@101: thread_clock::time_point thread_clock::now( system::error_code & ec ) Chris@101: { Chris@101: // get the thread port (borrowing pthread's reference) Chris@101: mach_port_t port = pthread_mach_thread_np(pthread_self()); Chris@101: Chris@101: // get the thread info Chris@101: thread_basic_info_data_t info; Chris@101: mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; Chris@101: if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS ) Chris@101: { Chris@101: if (BOOST_CHRONO_IS_THROWS(ec)) Chris@101: { Chris@101: boost::throw_exception( Chris@101: system::system_error( Chris@101: EINVAL, Chris@101: BOOST_CHRONO_SYSTEM_CATEGORY, Chris@101: "chrono::thread_clock" )); Chris@101: } Chris@101: else Chris@101: { Chris@101: ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); Chris@101: return time_point(); Chris@101: } Chris@101: } Chris@101: if (!BOOST_CHRONO_IS_THROWS(ec)) Chris@101: { Chris@101: ec.clear(); Chris@101: } Chris@101: Chris@101: // convert to nanoseconds Chris@101: duration user = duration( Chris@101: static_cast( info.user_time.seconds ) * 1000000000 Chris@101: + static_cast(info.user_time.microseconds ) * 1000); Chris@101: Chris@101: duration system = duration( Chris@101: static_cast( info.system_time.seconds ) * 1000000000 Chris@101: + static_cast( info.system_time.microseconds ) * 1000); Chris@101: Chris@101: return time_point( user + system ); Chris@101: } Chris@101: #endif Chris@101: } }