Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //  posix/chrono.cpp  --------------------------------------------------------------//
0002 
0003 //  Copyright Beman Dawes 2008
0004 //  Copyright Vicente J. Botet Escriba 2009
0005 
0006 //  Distributed under the Boost Software License, Version 1.0.
0007 //  See http://www.boost.org/LICENSE_1_0.txt
0008 
0009 //----------------------------------------------------------------------------//
0010 //                                POSIX                                     //
0011 //----------------------------------------------------------------------------//
0012 
0013 #include <time.h>  // for clock_gettime
0014 #include <boost/assert.hpp>
0015 #include <boost/predef/os.h>
0016 
0017 namespace boost
0018 {
0019 namespace chrono
0020 {
0021 
0022   system_clock::time_point system_clock::now() BOOST_NOEXCEPT
0023   {
0024     timespec ts;
0025     if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
0026     {
0027       BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0028     }
0029 
0030     return time_point(duration(
0031       static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
0032   }
0033 
0034 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0035   system_clock::time_point system_clock::now(system::error_code & ec)
0036   {
0037     timespec ts;
0038     if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
0039     {
0040         if (::boost::chrono::is_throws(ec))
0041         {
0042             boost::throw_exception(
0043                     system::system_error(
0044                             errno,
0045                             ::boost::system::system_category(),
0046                             "chrono::system_clock" ));
0047         }
0048         else
0049         {
0050             ec.assign( errno, ::boost::system::system_category() );
0051             return time_point();
0052         }
0053     }
0054 
0055     if (!::boost::chrono::is_throws(ec))
0056     {
0057         ec.clear();
0058     }
0059     return time_point(duration(
0060       static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
0061   }
0062 #endif
0063 
0064   std::time_t system_clock::to_time_t(const system_clock::time_point& t) BOOST_NOEXCEPT
0065   {
0066       return static_cast<std::time_t>( t.time_since_epoch().count() / 1000000000 );
0067   }
0068 
0069   system_clock::time_point system_clock::from_time_t(std::time_t t) BOOST_NOEXCEPT
0070   {
0071       return time_point(duration(static_cast<system_clock::rep>(t) * 1000000000));
0072   }
0073 
0074 #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
0075 
0076   steady_clock::time_point steady_clock::now() BOOST_NOEXCEPT
0077   {
0078     timespec ts;
0079 #if BOOST_OS_CYGWIN
0080     // lack of thread safety in high resolution timer initialization
0081     // can lead to a timespec of zero without an error; was reported
0082     // to the cygwin mailing list and can be removed once fixed
0083     do
0084     {
0085 #endif
0086       if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
0087       {
0088         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0089       }
0090 #if BOOST_OS_CYGWIN
0091     } while (ts.tv_sec == 0 && ts.tv_nsec == 0);
0092 #endif
0093     return time_point(duration(
0094       static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
0095   }
0096 
0097 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0098   steady_clock::time_point steady_clock::now(system::error_code & ec)
0099   {
0100     timespec ts;
0101 #if BOOST_OS_CYGWIN
0102     // lack of thread safety in high resolution timer initialization
0103     // can lead to a timespec of zero without an error; was reported
0104     // to the cygwin mailing list and can be removed once fixed
0105     do
0106     {
0107 #endif
0108       if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
0109       {
0110         if (::boost::chrono::is_throws(ec))
0111         {
0112             boost::throw_exception(
0113                     system::system_error(
0114                             errno,
0115                             ::boost::system::system_category(),
0116                             "chrono::steady_clock" ));
0117         }
0118         else
0119         {
0120             ec.assign( errno, ::boost::system::system_category() );
0121             return time_point();
0122         }
0123       }
0124 #if BOOST_OS_CYGWIN
0125     } while (ts.tv_sec == 0 && ts.tv_nsec == 0);
0126 #endif
0127 
0128     if (!::boost::chrono::is_throws(ec))
0129     {
0130         ec.clear();
0131     }
0132     return time_point(duration(
0133       static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
0134   }
0135 #endif
0136 #endif
0137 
0138 }  // namespace chrono
0139 }  // namespace boost
0140 
0141