Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:46

0001 //  boost thread_clock.cpp  -----------------------------------------------------------//
0002 
0003 //  Copyright Beman Dawes 1994, 2006, 2008
0004 //  Copyright Vicente J. Botet Escriba 2009-2011
0005 //  Copyright Christopher Brown 2013
0006 
0007 //  Distributed under the Boost Software License, Version 1.0.
0008 //  See http://www.boost.org/LICENSE_1_0.txt
0009 
0010 //  See http://www.boost.org/libs/chrono for documentation.
0011 
0012 //--------------------------------------------------------------------------------------//
0013 
0014 #include <boost/chrono/config.hpp>
0015 #include <boost/chrono/thread_clock.hpp>
0016 #include <cassert>
0017 #include <boost/assert.hpp>
0018 
0019 # include <pthread.h>
0020 # include <mach/thread_act.h>
0021 
0022 namespace boost { namespace chrono {
0023 
0024     thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT
0025     {
0026         // get the thread port (borrowing pthread's reference)
0027         mach_port_t port = pthread_mach_thread_np(pthread_self());
0028 
0029         // get the thread info
0030         thread_basic_info_data_t info;
0031         mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
0032         if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS )
0033         {
0034             BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0035             return time_point();
0036         }
0037 
0038         // convert to nanoseconds
0039         duration user = duration(
0040                 static_cast<thread_clock::rep>( info.user_time.seconds ) * 1000000000
0041                         + static_cast<thread_clock::rep>(info.user_time.microseconds ) * 1000);
0042 
0043         duration system = duration(
0044                 static_cast<thread_clock::rep>( info.system_time.seconds ) * 1000000000
0045                         + static_cast<thread_clock::rep>( info.system_time.microseconds ) * 1000);
0046 
0047         return time_point( user + system );
0048     }
0049 
0050 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0051     thread_clock::time_point thread_clock::now( system::error_code & ec )
0052     {
0053         // get the thread port (borrowing pthread's reference)
0054         mach_port_t port = pthread_mach_thread_np(pthread_self());
0055 
0056         // get the thread info
0057         thread_basic_info_data_t info;
0058         mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
0059         if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS )
0060         {
0061             if (::boost::chrono::is_throws(ec))
0062             {
0063                 boost::throw_exception(
0064                         system::system_error(
0065                                 EINVAL,
0066                                 ::boost::system::system_category(),
0067                                 "chrono::thread_clock" ));
0068             }
0069             else
0070             {
0071                 ec.assign( errno, ::boost::system::system_category() );
0072                 return time_point();
0073             }
0074         }
0075         if (!::boost::chrono::is_throws(ec))
0076         {
0077             ec.clear();
0078         }
0079 
0080         // convert to nanoseconds
0081         duration user = duration(
0082                 static_cast<thread_clock::rep>( info.user_time.seconds ) * 1000000000
0083                         + static_cast<thread_clock::rep>(info.user_time.microseconds ) * 1000);
0084 
0085         duration system = duration(
0086                 static_cast<thread_clock::rep>( info.system_time.seconds ) * 1000000000
0087                         + static_cast<thread_clock::rep>( info.system_time.microseconds ) * 1000);
0088 
0089         return time_point( user + system );
0090     }
0091 #endif
0092 } }