Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //  boost process_timer.cpp  -----------------------------------------------------------//
0002 
0003 //  Copyright Beman Dawes 1994, 2006, 2008
0004 //  Copyright 2009-2010 Vicente J. Botet Escriba
0005 //  Copyright (c) Microsoft Corporation 2014
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 #ifndef BOOST_CHRONO_DETAIL_INLINED_WIN_PROCESS_CLOCK_HPP
0014 #define BOOST_CHRONO_DETAIL_INLINED_WIN_PROCESS_CLOCK_HPP
0015 
0016 #include <boost/chrono/config.hpp>
0017 #include <boost/chrono/process_cpu_clocks.hpp>
0018 #include <cassert>
0019 #include <time.h>
0020 #include <boost/assert.hpp>
0021 
0022 #include <boost/winapi/get_last_error.hpp>
0023 #include <boost/winapi/get_current_process.hpp>
0024 #if BOOST_PLAT_WINDOWS_DESKTOP
0025 #include <boost/winapi/get_process_times.hpp>
0026 #endif
0027 
0028 namespace boost
0029 {
0030 namespace chrono
0031 {
0032 
0033 process_real_cpu_clock::time_point process_real_cpu_clock::now() BOOST_NOEXCEPT
0034 {
0035     clock_t c = ::clock();
0036     if ( c == clock_t(-1) ) // error
0037     {
0038       BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0039     }
0040     typedef ratio_divide<giga, ratio<CLOCKS_PER_SEC> >::type R;
0041     return time_point(
0042       duration(static_cast<rep>(c)*R::num/R::den)
0043     );
0044 }
0045 
0046 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0047 process_real_cpu_clock::time_point process_real_cpu_clock::now(
0048         system::error_code & ec)
0049 {
0050     clock_t c = ::clock();
0051     if ( c == clock_t(-1) ) // error
0052     {
0053             boost::throw_exception(
0054                     system::system_error(
0055                             errno,
0056                             ::boost::system::system_category(),
0057                             "chrono::process_real_cpu_clock" ));
0058     }
0059     if (!::boost::chrono::is_throws(ec))
0060     {
0061       ec.clear();
0062     }
0063     typedef ratio_divide<giga, ratio<CLOCKS_PER_SEC> >::type R;
0064     return time_point(
0065       duration(static_cast<rep>(c)*R::num/R::den)
0066     );
0067 }
0068 #endif
0069 
0070 #if BOOST_PLAT_WINDOWS_DESKTOP
0071 process_user_cpu_clock::time_point process_user_cpu_clock::now() BOOST_NOEXCEPT
0072 {
0073 
0074     //  note that Windows uses 100 nanosecond ticks for FILETIME
0075     boost::winapi::FILETIME_ creation, exit, user_time, system_time;
0076 
0077     if ( boost::winapi::GetProcessTimes(
0078             boost::winapi::GetCurrentProcess(), &creation, &exit,
0079             &system_time, &user_time ) )
0080     {
0081         return time_point(duration(
0082                 ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
0083                   | user_time.dwLowDateTime) * 100
0084                 ));
0085     }
0086     else
0087     {
0088         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0089         return time_point();
0090     }
0091 
0092 }
0093 
0094 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0095 process_user_cpu_clock::time_point process_user_cpu_clock::now(
0096         system::error_code & ec)
0097 {
0098 
0099     //  note that Windows uses 100 nanosecond ticks for FILETIME
0100     boost::winapi::FILETIME_ creation, exit, user_time, system_time;
0101 
0102     if ( boost::winapi::GetProcessTimes(
0103             boost::winapi::GetCurrentProcess(), &creation, &exit,
0104             &system_time, &user_time ) )
0105     {
0106         if (!::boost::chrono::is_throws(ec))
0107         {
0108             ec.clear();
0109         }
0110         return time_point(duration(
0111                 ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
0112                   | user_time.dwLowDateTime) * 100
0113                 ));
0114     }
0115     else
0116     {
0117         boost::winapi::DWORD_ cause = boost::winapi::GetLastError();
0118         if (::boost::chrono::is_throws(ec))
0119         {
0120             boost::throw_exception(
0121                     system::system_error(
0122                             cause,
0123                             ::boost::system::system_category(),
0124                             "chrono::process_user_cpu_clock" ));
0125         }
0126         else
0127         {
0128             ec.assign( cause, ::boost::system::system_category() );
0129             return time_point();
0130         }
0131     }
0132 
0133 }
0134 #endif
0135 
0136 process_system_cpu_clock::time_point process_system_cpu_clock::now() BOOST_NOEXCEPT
0137 {
0138 
0139     //  note that Windows uses 100 nanosecond ticks for FILETIME
0140     boost::winapi::FILETIME_ creation, exit, user_time, system_time;
0141 
0142     if ( boost::winapi::GetProcessTimes(
0143             boost::winapi::GetCurrentProcess(), &creation, &exit,
0144             &system_time, &user_time ) )
0145     {
0146         return time_point(duration(
0147                 ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
0148                                     | system_time.dwLowDateTime) * 100
0149                 ));
0150     }
0151     else
0152     {
0153       BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0154       return time_point();
0155     }
0156 
0157 }
0158 
0159 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0160 process_system_cpu_clock::time_point process_system_cpu_clock::now(
0161         system::error_code & ec)
0162 {
0163 
0164     //  note that Windows uses 100 nanosecond ticks for FILETIME
0165     boost::winapi::FILETIME_ creation, exit, user_time, system_time;
0166 
0167     if ( boost::winapi::GetProcessTimes(
0168             boost::winapi::GetCurrentProcess(), &creation, &exit,
0169             &system_time, &user_time ) )
0170     {
0171         if (!::boost::chrono::is_throws(ec))
0172         {
0173             ec.clear();
0174         }
0175         return time_point(duration(
0176                 ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
0177                                     | system_time.dwLowDateTime) * 100
0178                 ));
0179     }
0180     else
0181     {
0182         boost::winapi::DWORD_ cause = boost::winapi::GetLastError();
0183         if (::boost::chrono::is_throws(ec))
0184         {
0185             boost::throw_exception(
0186                     system::system_error(
0187                             cause,
0188                             ::boost::system::system_category(),
0189                             "chrono::process_system_cpu_clock" ));
0190         }
0191         else
0192         {
0193             ec.assign( cause, ::boost::system::system_category() );
0194             return time_point();
0195         }
0196     }
0197 
0198 }
0199 #endif
0200 
0201 process_cpu_clock::time_point process_cpu_clock::now()  BOOST_NOEXCEPT
0202 {
0203 
0204     //  note that Windows uses 100 nanosecond ticks for FILETIME
0205     boost::winapi::FILETIME_ creation, exit, user_time, system_time;
0206 
0207     if ( boost::winapi::GetProcessTimes(
0208             boost::winapi::GetCurrentProcess(), &creation, &exit,
0209             &system_time, &user_time ) )
0210     {
0211         time_point::rep r(process_real_cpu_clock::now().time_since_epoch().count()
0212                             ,
0213                 ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
0214                         | user_time.dwLowDateTime
0215                 ) * 100,
0216                 ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
0217                         | system_time.dwLowDateTime
0218                 ) * 100
0219         );
0220         return time_point(duration(r));
0221     }
0222     else
0223     {
0224       BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0225       return time_point();
0226     }
0227 
0228 }
0229 
0230 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0231 process_cpu_clock::time_point process_cpu_clock::now(
0232         system::error_code & ec )
0233 {
0234 
0235     //  note that Windows uses 100 nanosecond ticks for FILETIME
0236     boost::winapi::FILETIME_ creation, exit, user_time, system_time;
0237 
0238     if ( boost::winapi::GetProcessTimes(
0239             boost::winapi::GetCurrentProcess(), &creation, &exit,
0240             &system_time, &user_time ) )
0241     {
0242         if (!::boost::chrono::is_throws(ec))
0243         {
0244             ec.clear();
0245         }
0246         time_point::rep r(process_real_cpu_clock::now().time_since_epoch().count()
0247                             ,
0248                 ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
0249                         | user_time.dwLowDateTime
0250                 ) * 100,
0251                 ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
0252                         | system_time.dwLowDateTime
0253                 ) * 100
0254         );
0255         return time_point(duration(r));
0256     }
0257     else
0258     {
0259         boost::winapi::DWORD_ cause = boost::winapi::GetLastError();
0260         if (::boost::chrono::is_throws(ec))
0261         {
0262             boost::throw_exception(
0263                     system::system_error(
0264                             cause,
0265                             ::boost::system::system_category(),
0266                             "chrono::process_cpu_clock" ));
0267         }
0268         else
0269         {
0270             ec.assign( cause, ::boost::system::system_category() );
0271             return time_point();
0272         }
0273     }
0274 
0275 }
0276 #endif
0277 #endif
0278 } // namespace chrono
0279 } // namespace boost
0280 
0281 #endif