Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-09 08:48:35

0001 ////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2023-2024. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_TIMED_UTILS_HPP
0012 #define BOOST_INTERPROCESS_TIMED_UTILS_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 0018 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 #  pragma once
0021 #endif
0022 
0023 #include <boost/interprocess/detail/config_begin.hpp>
0024 #include <boost/interprocess/detail/workaround.hpp>
0025 #include <boost/interprocess/detail/timed_utils.hpp>
0026 
0027 
0028 //!\file
0029 //!Describes some simple time-related utilities that can be used to call synchronization primitive and ipc methods that required
0030 //!waiting until the resource is signalled or a timeout expires.
0031 //!
0032 //! These utilities are provided for those users that want to avoid dependence on std::chrono or boost::chrono or boost::date_time
0033 //! and just want to implement simple portable waits.
0034 
0035 namespace boost {
0036 namespace interprocess {
0037 
0038 //!Describes a simple duration type with microsecond resolution that can be used with the ustime time-point utility to call timed functions
0039 //! of Boost.Interprocess' synchronization classes that expect a time-point (timed_wait, wait_until, timed_lock, lock_until...)
0040 class ustime;
0041 
0042 //!Describes a simple duration type with microsecond resolution that can be used with the ustime time-point utility to call timed functions
0043 //! of Boost.Interprocess' synchronization classes that expect a duration type (wait_for, lock_for...)
0044 class usduration
0045 {
0046    public:
0047    friend class ustime;
0048 
0049    //!Constructs a duration type that stores microseconds from
0050    //!the passed count
0051    explicit usduration(boost::uint64_t microsecs = 0u)
0052       : m_microsecs(microsecs)
0053    {}
0054 
0055    //!Returns the stored microsecond
0056    //!count
0057    boost::uint64_t get_microsecs() const
0058    {  return m_microsecs;  }
0059 
0060    bool operator < (const usduration &other) const
0061    {  return m_microsecs < other.m_microsecs; }
0062 
0063    bool operator > (const usduration &other) const
0064    {  return m_microsecs > other.m_microsecs; }
0065 
0066    bool operator <= (const usduration &other) const
0067    {  return m_microsecs <= other.m_microsecs; }
0068 
0069    bool operator >= (const usduration &other) const
0070    {  return m_microsecs >= other.m_microsecs; }
0071 
0072    private:
0073    boost::uint64_t m_microsecs;
0074 };
0075 
0076 class ustime
0077 {
0078    public:
0079    //!Constructs a time point that is "microsecs" duration away
0080    //!from the epoch of the system
0081    explicit ustime(boost::uint64_t microsecs = 0u)
0082       : m_microsecs(microsecs)
0083    {}
0084 
0085    ustime &operator += (const usduration &other)
0086    {  m_microsecs += other.m_microsecs; return *this; }
0087 
0088    ustime operator + (const usduration &other)
0089    {  ustime r(*this); r += other; return r; }
0090 
0091    ustime &operator -= (const usduration &other)
0092    {  m_microsecs -= other.m_microsecs; return *this; }
0093 
0094    ustime operator - (const usduration &other)
0095    {  ustime r(*this); r -= other; return r; }
0096 
0097    friend usduration operator - (const ustime &l, const ustime &r)
0098    {  return usduration(l.m_microsecs - r.m_microsecs); }
0099 
0100    bool operator < (const ustime &other) const
0101    {  return m_microsecs < other.m_microsecs; }
0102 
0103    bool operator > (const ustime &other) const
0104    {  return m_microsecs > other.m_microsecs; }
0105 
0106    bool operator <= (const ustime &other) const
0107    {  return m_microsecs <= other.m_microsecs; }
0108 
0109    bool operator >= (const ustime &other) const
0110    {  return m_microsecs >= other.m_microsecs; }
0111 
0112    //!Returns the stored count
0113    //!that represents microseconds from epoch
0114    boost::uint64_t get_microsecs() const
0115    {  return m_microsecs;  }
0116 
0117    private:
0118    boost::uint64_t m_microsecs;
0119 };
0120 
0121 //!Utility that returns a duration from
0122 //!a seconds count
0123 inline usduration usduration_from_seconds(boost::uint64_t sec)
0124 {  return usduration(sec*uint64_t(1000000u));   }
0125 
0126 //!Utility that returns a duration from
0127 //!a milliseconds count
0128 inline usduration usduration_from_milliseconds(boost::uint64_t millisec)
0129 {  return usduration(millisec*1000u);   }
0130 
0131 //!Utility that returns a time_point in the future that is "msecs"
0132 //!milliseconds in the future from now.
0133 inline ustime ustime_delay_milliseconds(unsigned msecs)
0134 {
0135    return ustime(ipcdetail::universal_time_u64_us()) + usduration(msecs*1000u);
0136 }
0137 
0138 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0139 
0140 namespace ipcdetail {
0141 
0142 template<>
0143 class microsec_clock<ustime>
0144 {
0145    public:
0146    typedef ustime time_point;
0147 
0148    static ustime universal_time()
0149    {  return ustime(universal_time_u64_us());   }
0150 };
0151 
0152 // duration_to_usduration
0153 
0154 template<class Duration>
0155 inline usduration duration_to_usduration(const Duration &d, typename enable_if_ptime_duration<Duration>::type* = 0)
0156 {
0157    return usduration(static_cast<boost::uint64_t>(d.total_microseconds()));
0158 }
0159 
0160 template<class Duration>
0161 inline usduration duration_to_usduration(const Duration &d, typename enable_if_duration<Duration>::type* = 0)
0162 {
0163    const double factor = double(Duration::period::num)*1000000.0/double(Duration::period::den);
0164    return usduration(static_cast<boost::uint64_t>(double(d.count())*factor));
0165 }
0166 
0167 inline usduration duration_to_usduration(const usduration &d)
0168 {
0169    return d;
0170 }
0171 
0172 // duration_to_ustime
0173 
0174 template<class Duration>
0175 inline ustime duration_to_ustime(const Duration &d)
0176 {
0177    return microsec_clock<ustime>::universal_time() + (duration_to_usduration)(d);
0178 }
0179 
0180 
0181 }  //namespace ipcdetail {
0182 
0183 #endif   //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0184 
0185 }  //namespace interprocess {
0186 }  //namespace boost {
0187 
0188 #include <boost/interprocess/detail/config_end.hpp>
0189 
0190 #endif   //BOOST_INTERPROCESS_TIMED_UTILS_HPP