1 // boost thread_clock.cpp -----------------------------------------------------------//
3 // Copyright 2010 Vicente J. Botet Escriba
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
8 // See http://www.boost.org/libs/chrono for documentation.
10 //--------------------------------------------------------------------------------------//
11 #ifndef BOOST_CHRONO_DETAIL_INLINED_WIN_THREAD_CLOCK_HPP
12 #define BOOST_CHRONO_DETAIL_INLINED_WIN_THREAD_CLOCK_HPP
14 #include <boost/chrono/config.hpp>
15 #include <boost/chrono/thread_clock.hpp>
17 #include <boost/assert.hpp>
19 #include <boost/detail/winapi/get_last_error.hpp>
20 #include <boost/detail/winapi/get_current_thread.hpp>
21 #include <boost/detail/winapi/get_thread_times.hpp>
28 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
29 thread_clock::time_point thread_clock::now( system::error_code & ec )
31 // note that Windows uses 100 nanosecond ticks for FILETIME
32 boost::detail::winapi::FILETIME_ creation, exit, user_time, system_time;
34 if ( boost::detail::winapi::GetThreadTimes(
35 boost::detail::winapi::GetCurrentThread (), &creation, &exit,
36 &system_time, &user_time ) )
38 duration user = duration(
39 ((static_cast<duration::rep>(user_time.dwHighDateTime) << 32)
40 | user_time.dwLowDateTime) * 100 );
42 duration system = duration(
43 ((static_cast<duration::rep>(system_time.dwHighDateTime) << 32)
44 | system_time.dwLowDateTime) * 100 );
46 if (!BOOST_CHRONO_IS_THROWS(ec))
50 return time_point(system+user);
55 if (BOOST_CHRONO_IS_THROWS(ec))
57 boost::throw_exception(
59 boost::detail::winapi::GetLastError(),
60 BOOST_CHRONO_SYSTEM_CATEGORY,
61 "chrono::thread_clock" ));
65 ec.assign( boost::detail::winapi::GetLastError(), BOOST_CHRONO_SYSTEM_CATEGORY );
66 return thread_clock::time_point(duration(0));
72 thread_clock::time_point thread_clock::now() BOOST_NOEXCEPT
75 // note that Windows uses 100 nanosecond ticks for FILETIME
76 boost::detail::winapi::FILETIME_ creation, exit, user_time, system_time;
78 if ( boost::detail::winapi::GetThreadTimes(
79 boost::detail::winapi::GetCurrentThread (), &creation, &exit,
80 &system_time, &user_time ) )
82 duration user = duration(
83 ((static_cast<duration::rep>(user_time.dwHighDateTime) << 32)
84 | user_time.dwLowDateTime) * 100 );
86 duration system = duration(
87 ((static_cast<duration::rep>(system_time.dwHighDateTime) << 32)
88 | system_time.dwLowDateTime) * 100 );
90 return time_point(system+user);
94 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
100 } // namespace chrono