Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-26 08:36:32

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