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 
0006 //  Distributed under the Boost Software License, Version 1.0.
0007 //  See http://www.boost.org/LICENSE_1_0.txt
0008 
0009 //  See http://www.boost.org/libs/chrono for documentation.
0010 
0011 //--------------------------------------------------------------------------------------//
0012 
0013 #include <boost/chrono/config.hpp>
0014 #include <boost/chrono/thread_clock.hpp>
0015 #include <cassert>
0016 #include <boost/assert.hpp>
0017 
0018 #if !defined(__VXWORKS__)
0019 # include <sys/times.h>
0020 #endif
0021 # include <pthread.h>
0022 # include <unistd.h>
0023 
0024 namespace boost { namespace chrono {
0025 
0026     thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT
0027     {
0028       struct timespec ts;
0029 #if defined CLOCK_THREAD_CPUTIME_ID
0030         // get the timespec associated to the thread clock
0031         if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
0032 #else
0033         // get the current thread
0034         pthread_t pth=pthread_self();
0035         // get the clock_id associated to the current thread
0036         clockid_t clock_id;
0037         pthread_getcpuclockid(pth, &clock_id);
0038         // get the timespec associated to the thread clock
0039         if ( ::clock_gettime( clock_id, &ts ) )
0040 #endif
0041         {
0042           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0043         }
0044 
0045         // transform to nanoseconds
0046         return time_point(duration(
0047             static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
0048 
0049     }
0050 
0051 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0052     thread_clock::time_point thread_clock::now( system::error_code & ec )
0053     {
0054       struct timespec ts;
0055 #if defined CLOCK_THREAD_CPUTIME_ID
0056         // get the timespec associated to the thread clock
0057         if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
0058 #else
0059         // get the current thread
0060         pthread_t pth=pthread_self();
0061         // get the clock_id associated to the current thread
0062         clockid_t clock_id;
0063         pthread_getcpuclockid(pth, &clock_id);
0064         // get the timespec associated to the thread clock
0065         if ( ::clock_gettime( clock_id, &ts ) )
0066 #endif
0067         {
0068             if (::boost::chrono::is_throws(ec))
0069             {
0070                 boost::throw_exception(
0071                         system::system_error(
0072                                 errno,
0073                                 ::boost::system::system_category(),
0074                                 "chrono::thread_clock" ));
0075             }
0076             else
0077             {
0078                 ec.assign( errno, ::boost::system::system_category() );
0079                 return time_point();
0080             }
0081         }
0082         if (!::boost::chrono::is_throws(ec))
0083         {
0084             ec.clear();
0085         }
0086         // transform to nanoseconds
0087         return time_point(duration(
0088             static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
0089 
0090     }
0091 #endif
0092 } }