Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-17 08:38:39

0001 /*
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * Copyright (c) 2025 Andrey Semashev
0007  */
0008 /*!
0009  * \file   atomic/detail/chrono.hpp
0010  *
0011  * This header contains \c std::chrono utilities.
0012  */
0013 
0014 #ifndef BOOST_ATOMIC_DETAIL_CHRONO_HPP_INCLUDED_
0015 #define BOOST_ATOMIC_DETAIL_CHRONO_HPP_INCLUDED_
0016 
0017 #include <time.h>
0018 #include <chrono>
0019 #if !defined(__cpp_lib_chrono) || (__cpp_lib_chrono < 201510l)
0020 #include <ratio>
0021 #include <type_traits>
0022 #endif // !defined(__cpp_lib_chrono) || (__cpp_lib_chrono < 201510l)
0023 #if defined(CLOCK_REALTIME)
0024 #include <boost/atomic/posix_clock_traits_fwd.hpp>
0025 #endif // defined(CLOCK_REALTIME)
0026 #include <boost/atomic/detail/config.hpp>
0027 #include <boost/atomic/detail/header.hpp>
0028 
0029 #ifdef BOOST_HAS_PRAGMA_ONCE
0030 #pragma once
0031 #endif
0032 
0033 namespace boost {
0034 namespace atomics {
0035 namespace detail {
0036 namespace chrono {
0037 
0038 #if defined(__cpp_lib_chrono) && (__cpp_lib_chrono >= 201510l)
0039 
0040 using std::chrono::ceil;
0041 
0042 #else // defined(__cpp_lib_chrono) && (__cpp_lib_chrono >= 201510l)
0043 
0044 template< typename To, typename Rep, typename Period >
0045 inline constexpr To ceil(std::chrono::duration< Rep, Period > from) noexcept
0046 {
0047     using conv_ratio = std::ratio_divide< Period, typename To::period >;
0048     using common_rep = typename std::common_type< Rep, typename To::rep, decltype(conv_ratio::num) >::type;
0049     return To(static_cast< typename To::rep >((static_cast< common_rep >(from.count()) * conv_ratio::num) / conv_ratio::den +
0050         static_cast< common_rep >(((static_cast< common_rep >(from.count()) * conv_ratio::num) % conv_ratio::den) != static_cast< common_rep >(0))));
0051 }
0052 
0053 #endif // defined(__cpp_lib_chrono) && (__cpp_lib_chrono >= 201510l)
0054 
0055 } // namespace chrono
0056 } // namespace detail
0057 
0058 #if defined(CLOCK_REALTIME)
0059 
0060 //! Integrate `std::chrono::system_clock` with POSIX clocks
0061 template< >
0062 struct posix_clock_traits< std::chrono::system_clock >
0063 {
0064     //! POSIX clock identifier
0065     static constexpr clockid_t clock_id = CLOCK_REALTIME;
0066 
0067     //! Function that converts a time point to a timespec structure
0068     static timespec to_timespec(std::chrono::system_clock::time_point time_point) noexcept
0069     {
0070         timespec ts{};
0071         std::chrono::nanoseconds::rep time_ns = std::chrono::duration_cast< std::chrono::nanoseconds >(time_point.time_since_epoch()).count();
0072         // Note: The standard doesn't require that std::chrono::system_clock epoch matches the POSIX CLOCK_REALTIME epoch. Also, std::chrono::system_clock::to_time_t
0073         //       is allowed to round or truncate the time point when converting to time_t resolution, which means to_time_t may return a time before or after time_point.
0074         ts.tv_sec = std::chrono::system_clock::to_time_t(std::chrono::system_clock::time_point()) + static_cast< decltype(ts.tv_sec) >(time_ns / 1000000000);
0075         time_ns %= 1000000000;
0076         if (BOOST_UNLIKELY(time_ns < 0))
0077         {
0078             --ts.tv_sec;
0079             time_ns += 1000000000;
0080         }
0081         ts.tv_nsec = static_cast< decltype(ts.tv_nsec) >(time_ns);
0082         return ts;
0083     }
0084 };
0085 
0086 #endif // defined(CLOCK_REALTIME)
0087 
0088 } // namespace atomics
0089 } // namespace boost
0090 
0091 #include <boost/atomic/detail/footer.hpp>
0092 
0093 #endif // BOOST_ATOMIC_DETAIL_CHRONO_HPP_INCLUDED_