Add timed_utils header and refactor all dependent header to use the new header

This commit is contained in:
Ion Gaztañaga
2024-02-27 00:13:47 +01:00
parent b12499e3fb
commit 8e4caa9fca
39 changed files with 288 additions and 153 deletions

View File

@@ -33,7 +33,7 @@
#include <boost/interprocess/permissions.hpp>
#include <boost/container/detail/type_traits.hpp> //alignment_of, aligned_storage
#include <boost/interprocess/sync/spin/wait.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/move/move.hpp>
#include <boost/cstdint.hpp>
@@ -388,7 +388,7 @@ class managed_open_or_create_impl
, const void *addr, ConstructFunc construct_func
, bool ronly, bool cow)
{
const usduration TimeoutSec(usduration_seconds(MaxInitializeTimeSec));
const usduration TimeoutSec(usduration_from_seconds(MaxInitializeTimeSec));
if(FileBased){
offset_t filesize = 0;

View File

@@ -42,6 +42,10 @@
namespace boost {
namespace interprocess {
class ustime;
class usduration;
namespace ipcdetail {
BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(time_duration_type)
@@ -74,6 +78,16 @@ struct enable_if_duration
: enable_if_c< BOOST_INTRUSIVE_HAS_TYPE(boost::interprocess::ipcdetail::, T, rep) >
{};
template<class T>
struct enable_if_ustime
: enable_if_c< is_same<T, ustime>::value >
{};
template<class T>
struct enable_if_usduration
: enable_if_c< is_same<T, usduration>::value >
{};
#if defined(BOOST_INTERPROCESS_HAS_REENTRANT_STD_FUNCTIONS)
inline std::tm* interprocess_gmtime(const std::time_t* t, std::tm* result)
@@ -135,82 +149,24 @@ inline boost::uint64_t file_time_to_microseconds(const boost::winapi::FILETIME_
}
#endif
class ustime;
class usduration
inline boost::uint64_t universal_time_u64_us()
{
public:
friend class ustime;
#ifdef BOOST_HAS_GETTIMEOFDAY
timeval tv;
gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux.
boost::uint64_t micros = boost::uint64_t(tv.tv_sec)*1000000u;
micros += (boost::uint64_t)tv.tv_usec;
#elif defined(BOOST_HAS_FTIME)
boost::winapi::FILETIME_ ft;
boost::winapi::GetSystemTimeAsFileTime(&ft);
boost::uint64_t micros = file_time_to_microseconds(ft); // it will not wrap, since ft is the current time
// and cannot be before 1970-Jan-01
#else
#error "Unsupported date-time error: neither gettimeofday nor FILETIME support is detected"
#endif
return micros;
}
explicit usduration(boost::uint64_t microsecs)
: m_microsecs(microsecs)
{}
boost::uint64_t get_microsecs() const
{ return m_microsecs; }
bool operator < (const usduration &other) const
{ return m_microsecs < other.m_microsecs; }
bool operator > (const usduration &other) const
{ return m_microsecs > other.m_microsecs; }
bool operator <= (const usduration &other) const
{ return m_microsecs <= other.m_microsecs; }
bool operator >= (const usduration &other) const
{ return m_microsecs >= other.m_microsecs; }
private:
boost::uint64_t m_microsecs;
};
class ustime
{
public:
explicit ustime(boost::uint64_t microsecs = 0u)
: m_microsecs(microsecs)
{}
ustime &operator += (const usduration &other)
{ m_microsecs += other.m_microsecs; return *this; }
ustime operator + (const usduration &other)
{ ustime r(*this); r += other; return r; }
ustime &operator -= (const usduration &other)
{ m_microsecs -= other.m_microsecs; return *this; }
ustime operator - (const usduration &other)
{ ustime r(*this); r -= other; return r; }
friend usduration operator - (const ustime &l, const ustime &r)
{ return usduration(l.m_microsecs - r.m_microsecs); }
bool operator < (const ustime &other) const
{ return m_microsecs < other.m_microsecs; }
bool operator > (const ustime &other) const
{ return m_microsecs > other.m_microsecs; }
bool operator <= (const ustime &other) const
{ return m_microsecs <= other.m_microsecs; }
bool operator >= (const ustime &other) const
{ return m_microsecs >= other.m_microsecs; }
boost::uint64_t get_microsecs() const
{ return m_microsecs; }
private:
boost::uint64_t m_microsecs;
};
inline usduration usduration_milliseconds(boost::uint64_t millisec)
{ return usduration(millisec*1000u); }
inline usduration usduration_seconds(boost::uint64_t sec)
{ return usduration(sec*uint64_t(1000000u)); }
template<class TimeType, class Enable = void>
class microsec_clock;
@@ -266,31 +222,6 @@ class microsec_clock<TimeType, typename enable_if_ptime<TimeType>::type>
}
};
template<>
class microsec_clock<ustime>
{
public:
typedef ustime time_point;
static ustime universal_time()
{
#ifdef BOOST_HAS_GETTIMEOFDAY
timeval tv;
gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux.
boost::uint64_t micros = boost::uint64_t(tv.tv_sec)*1000000u;
micros += (boost::uint64_t)tv.tv_usec;
#elif defined(BOOST_HAS_FTIME)
boost::winapi::FILETIME_ ft;
boost::winapi::GetSystemTimeAsFileTime(&ft);
boost::uint64_t micros = file_time_to_microseconds(ft); // it will not wrap, since ft is the current time
// and cannot be before 1970-Jan-01
#else
#error "Unsupported date-time error: neither gettimeofday nor FILETIME support is detected"
#endif
return ustime(micros);
}
};
template<class TimePoint>
class microsec_clock<TimePoint, typename enable_if_time_point<TimePoint>::type>
{
@@ -329,35 +260,12 @@ inline boost::uint64_t duration_to_milliseconds(const Duration &d, typename enab
return static_cast<boost::uint64_t>(double(d.count())*factor);
}
inline boost::uint64_t duration_to_milliseconds(const usduration &d)
{
return d.get_microsecs()/1000;
}
// duration_to_usduration
template<class Duration>
inline usduration duration_to_usduration(const Duration &d, typename enable_if_ptime_duration<Duration>::type* = 0)
inline boost::uint64_t duration_to_milliseconds(const Duration&d, typename enable_if_usduration<Duration>::type* = 0)
{
return usduration(static_cast<boost::uint64_t>(d.total_microseconds()));
return d.get_microsecs()/1000u;
}
template<class Duration>
inline usduration duration_to_usduration(const Duration &d, typename enable_if_duration<Duration>::type* = 0)
{
const double factor = double(Duration::period::num)*1000000.0/double(Duration::period::den);
return usduration(static_cast<boost::uint64_t>(double(d.count())*factor));
}
// duration_to_ustime
template<class Duration>
inline ustime duration_to_ustime(const Duration &d)
{
return microsec_clock<ustime>::universal_time() + (duration_to_usduration)(d);
}
} //namespace ipcdetail {
} //namespace interprocess {
} //namespace boost {

View File

@@ -28,7 +28,7 @@
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/detail/utilities.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/offset_ptr.hpp>
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/exceptions.hpp>

View File

@@ -49,6 +49,7 @@
#ifndef BOOST_NO_EXCEPTIONS
#include <exception>
#endif
#include <typeinfo>
//!\file
//!Describes the object placed in a memory segment that provides

View File

@@ -23,7 +23,7 @@
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/sync/spin/wait.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
namespace boost {
namespace interprocess {
@@ -78,7 +78,7 @@ void timeout_when_locking_aware_lock(MutexType &m)
{
#ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
if (!m.timed_lock(microsec_clock<ustime>::universal_time()
+ usduration_milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS)))
+ usduration_from_milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS)))
{
throw interprocess_exception(timeout_when_locking_error
, "Interprocess mutex timeout when locking. Possible deadlock: "

View File

@@ -22,6 +22,7 @@
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/detail/os_file_functions.hpp>
#include <boost/interprocess/detail/os_thread_functions.hpp>
#include <boost/interprocess/sync/detail/common_algorithms.hpp>

View File

@@ -27,6 +27,7 @@
#include <boost/interprocess/sync/cv_status.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/detail/locks.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/limits.hpp>
#include <boost/assert.hpp>

View File

@@ -25,6 +25,7 @@
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/sync/cv_status.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/exceptions.hpp>

View File

@@ -28,6 +28,7 @@
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/assert.hpp>
#include <boost/interprocess/sync/detail/common_algorithms.hpp>

View File

@@ -26,6 +26,7 @@
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <climits>

View File

@@ -24,6 +24,7 @@
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <climits>

View File

@@ -25,6 +25,7 @@
#include <boost/interprocess/sync/cv_status.hpp>
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/detail/interprocess_tester.hpp>
#include <boost/interprocess/permissions.hpp>
#include <boost/interprocess/sync/detail/locks.hpp>

View File

@@ -25,6 +25,7 @@
#include <boost/interprocess/sync/cv_status.hpp>
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/detail/interprocess_tester.hpp>
#include <boost/interprocess/permissions.hpp>
#include <boost/interprocess/sync/detail/locks.hpp>

View File

@@ -23,6 +23,7 @@
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/detail/interprocess_tester.hpp>
#include <boost/interprocess/permissions.hpp>

View File

@@ -23,6 +23,7 @@
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/permissions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
#include <boost/interprocess/sync/windows/named_recursive_mutex.hpp>

View File

@@ -24,6 +24,7 @@
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
#include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
#include <boost/interprocess/sync/shm/named_creation_functor.hpp>

View File

@@ -28,6 +28,7 @@
#include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
#include <boost/interprocess/sync/shm/named_creation_functor.hpp>
#include <boost/interprocess/permissions.hpp>
#include <boost/interprocess/timed_utils.hpp>
//!\file
//!Describes a named upgradable mutex class for inter-process synchronization

View File

@@ -27,7 +27,7 @@
#include <errno.h>
#include <boost/interprocess/sync/posix/pthread_helpers.hpp>
#include <boost/interprocess/sync/posix/timepoint_to_timespec.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/sync/posix/mutex.hpp>
#include <boost/assert.hpp>

View File

@@ -44,7 +44,7 @@
#include <boost/interprocess/sync/posix/timepoint_to_timespec.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/sync/posix/pthread_helpers.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
#ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS

View File

@@ -25,7 +25,7 @@
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/detail/interprocess_tester.hpp>
#include <boost/interprocess/permissions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/sync/posix/named_semaphore.hpp>
namespace boost {

View File

@@ -42,7 +42,7 @@
#include <errno.h>
#include <boost/interprocess/sync/posix/pthread_helpers.hpp>
#include <boost/interprocess/sync/posix/timepoint_to_timespec.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/exceptions.hpp>
#ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
# include <boost/interprocess/detail/os_thread_functions.hpp>

View File

@@ -23,7 +23,7 @@
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/detail/os_file_functions.hpp>
#include <boost/interprocess/detail/shared_dir_helpers.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/permissions.hpp>
#include <fcntl.h> //O_CREAT, O_*...

View File

@@ -21,7 +21,7 @@
#include <boost/interprocess/detail/mpl.hpp>
#include <boost/interprocess/detail/type_traits.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
namespace boost {

View File

@@ -33,6 +33,7 @@
#include <boost/interprocess/sync/shm/named_creation_functor.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/permissions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>

View File

@@ -35,6 +35,7 @@
#include <boost/interprocess/permissions.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
//!\file

View File

@@ -30,6 +30,7 @@
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
#include <boost/interprocess/sync/shm/named_creation_functor.hpp>
#include <boost/interprocess/timed_utils.hpp>
//!\file
//!Describes a named mutex class for inter-process synchronization

View File

@@ -28,6 +28,7 @@
#include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
#include <boost/interprocess/sync/shm/named_creation_functor.hpp>
#include <boost/interprocess/permissions.hpp>
#include <boost/interprocess/timed_utils.hpp>
//!\file
//!Describes a named shm_named_recursive_mutex class for inter-process synchronization

View File

@@ -28,7 +28,7 @@
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/detail/os_thread_functions.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/sync/spin/wait.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/cstdint.hpp>

View File

@@ -26,6 +26,7 @@
#include <boost/cstdint.hpp>
#include <boost/interprocess/detail/os_thread_functions.hpp>
#include <boost/interprocess/sync/detail/common_algorithms.hpp>
#include <boost/interprocess/timed_utils.hpp>
namespace boost {
namespace interprocess {

View File

@@ -44,6 +44,7 @@
#include <boost/cstdint.hpp>
#include <boost/interprocess/detail/os_thread_functions.hpp>
#include <boost/interprocess/sync/spin/mutex.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/assert.hpp>
namespace boost {

View File

@@ -28,6 +28,7 @@
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/sync/windows/semaphore.hpp>
#include <boost/interprocess/sync/windows/mutex.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <boost/interprocess/sync/detail/condition_algorithm_8a.hpp>

View File

@@ -26,6 +26,7 @@
#include <boost/interprocess/sync/windows/sync_utils.hpp>
#include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/timed_utils.hpp>
namespace boost {

View File

@@ -29,6 +29,7 @@
#include <boost/interprocess/sync/windows/named_sync.hpp>
#include <boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp>
#include <boost/interprocess/sync/detail/condition_algorithm_8a.hpp>
#include <boost/interprocess/timed_utils.hpp>
namespace boost {
namespace interprocess {

View File

@@ -28,6 +28,7 @@
#include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
#include <boost/interprocess/errors.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <limits>
namespace boost {

View File

@@ -24,7 +24,7 @@
#include <boost/interprocess/detail/win32_api.hpp>
#include <boost/interprocess/errors.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <limits>
namespace boost {

View File

@@ -0,0 +1,189 @@
////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2023-2024. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_INTERPROCESS_TIMED_UTILS_HPP
#define BOOST_INTERPROCESS_TIMED_UTILS_HPP
#ifndef BOOST_CONFIG_HPP
# include <boost/config.hpp>
#endif
#
#if defined(BOOST_HAS_PRAGMA_ONCE)
# pragma once
#endif
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/detail/timed_utils.hpp>
//!\file
//!Describes some simple time-related utilities that can be used to call synchronization primitive and ipc methods that required
//!waiting until the resource is signalled or a timeout expires.
//!
//! These utilities are provided for those users that want to avoid dependence on std::chrono or boost::chrono or boost::date_time
//! and just want to implement simple portable waits.
namespace boost {
namespace interprocess {
//!Describes a simple duration type with microsecond resolution that can be used with the ustime time-point utility to call timed functions
//! of Boost.Interprocess' synchronization classes that expect a time-point (timed_wait, wait_until, timed_lock, lock_until...)
class ustime;
//!Describes a simple duration type with microsecond resolution that can be used with the ustime time-point utility to call timed functions
//! of Boost.Interprocess' synchronization classes that expect a duration type (wait_for, lock_for...)
class usduration
{
public:
friend class ustime;
//!Constructs a duration type that stores microseconds from
//!the passed count
explicit usduration(boost::uint64_t microsecs = 0u)
: m_microsecs(microsecs)
{}
//!Returns the stored microsecond
//!count
boost::uint64_t get_microsecs() const
{ return m_microsecs; }
bool operator < (const usduration &other) const
{ return m_microsecs < other.m_microsecs; }
bool operator > (const usduration &other) const
{ return m_microsecs > other.m_microsecs; }
bool operator <= (const usduration &other) const
{ return m_microsecs <= other.m_microsecs; }
bool operator >= (const usduration &other) const
{ return m_microsecs >= other.m_microsecs; }
private:
boost::uint64_t m_microsecs;
};
class ustime
{
public:
//!Constructs a time point that is "microsecs" duration away
//!from the epoch of the system
explicit ustime(boost::uint64_t microsecs = 0u)
: m_microsecs(microsecs)
{}
ustime &operator += (const usduration &other)
{ m_microsecs += other.m_microsecs; return *this; }
ustime operator + (const usduration &other)
{ ustime r(*this); r += other; return r; }
ustime &operator -= (const usduration &other)
{ m_microsecs -= other.m_microsecs; return *this; }
ustime operator - (const usduration &other)
{ ustime r(*this); r -= other; return r; }
friend usduration operator - (const ustime &l, const ustime &r)
{ return usduration(l.m_microsecs - r.m_microsecs); }
bool operator < (const ustime &other) const
{ return m_microsecs < other.m_microsecs; }
bool operator > (const ustime &other) const
{ return m_microsecs > other.m_microsecs; }
bool operator <= (const ustime &other) const
{ return m_microsecs <= other.m_microsecs; }
bool operator >= (const ustime &other) const
{ return m_microsecs >= other.m_microsecs; }
//!Returns the stored count
//!that represents microseconds from epoch
boost::uint64_t get_microsecs() const
{ return m_microsecs; }
private:
boost::uint64_t m_microsecs;
};
//!Utility that returns a duration from
//!a seconds count
inline usduration usduration_from_seconds(boost::uint64_t sec)
{ return usduration(sec*uint64_t(1000000u)); }
//!Utility that returns a duration from
//!a milliseconds count
inline usduration usduration_from_milliseconds(boost::uint64_t millisec)
{ return usduration(millisec*1000u); }
//!Utility that returns a time_point in the future that is "msecs"
//!milliseconds in the future from now.
inline ustime ustime_delay_milliseconds(unsigned msecs)
{
return ustime(ipcdetail::universal_time_u64_us()) + usduration(msecs*1000u);
}
#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
namespace ipcdetail {
template<>
class microsec_clock<ustime>
{
public:
typedef ustime time_point;
static ustime universal_time()
{ return ustime(universal_time_u64_us()); }
};
// duration_to_usduration
template<class Duration>
inline usduration duration_to_usduration(const Duration &d, typename enable_if_ptime_duration<Duration>::type* = 0)
{
return usduration(static_cast<boost::uint64_t>(d.total_microseconds()));
}
template<class Duration>
inline usduration duration_to_usduration(const Duration &d, typename enable_if_duration<Duration>::type* = 0)
{
const double factor = double(Duration::period::num)*1000000.0/double(Duration::period::den);
return usduration(static_cast<boost::uint64_t>(double(d.count())*factor));
}
inline usduration duration_to_usduration(const usduration &d)
{
return d;
}
// duration_to_ustime
template<class Duration>
inline ustime duration_to_ustime(const Duration &d)
{
return microsec_clock<ustime>::universal_time() + (duration_to_usduration)(d);
}
} //namespace ipcdetail {
#endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
} //namespace interprocess {
} //namespace boost {
#include <boost/interprocess/detail/config_end.hpp>
#endif //BOOST_INTERPROCESS_TIMED_UTILS_HPP

View File

@@ -20,7 +20,7 @@
#include "mutex_test_template.hpp"
#include "get_process_id_name.hpp"
#include <exception>
#include <boost/interprocess/detail/timed_utils.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include <exception>

View File

@@ -23,15 +23,16 @@
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#include "named_creation_template.hpp"
#include "mutex_test_template.hpp"
namespace boost { namespace interprocess { namespace test {
#include <boost/interprocess/exceptions.hpp>
#include "named_creation_template.hpp"
#include "mutex_test_template.hpp"
namespace boost { namespace interprocess { namespace test {
static const std::size_t SemCount = 1;
static const std::size_t RecSemCount = 100;

View File

@@ -24,6 +24,7 @@
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/detail/os_thread_functions.hpp>
#include <boost/interprocess/timed_utils.hpp>
#if defined(BOOST_CLANG) || (defined(BOOST_GCC) && (BOOST_GCC >= 40600))
#pragma GCC diagnostic push
@@ -37,10 +38,10 @@
# endif
#endif
#include <boost/date_time/posix_time/posix_time_types.hpp>
#if BOOST_CXX_VERSION >= 201103L
#define BOOST_CHRONO_HEADER_ONLY
#include <boost/chrono/system_clocks.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#endif
#include <boost/version.hpp>
@@ -57,6 +58,9 @@ namespace boost {
namespace interprocess {
namespace test {
// ptime_delay_ms + ptime_ms
#if BOOST_CXX_VERSION >= 201103L
inline boost::posix_time::ptime ptime_delay_ms(unsigned msecs)
{
using namespace boost::posix_time;
@@ -68,10 +72,19 @@ inline boost::posix_time::ptime ptime_delay_ms(unsigned msecs)
inline boost::posix_time::time_duration ptime_ms(unsigned msecs)
{
using namespace boost::posix_time;
int count = static_cast<int>(double(msecs)*
(double(time_duration::ticks_per_second())/double(1000.0)));
int count = static_cast<int>(double(msecs) *
(double(time_duration::ticks_per_second()) / double(1000.0)));
return time_duration(0, 0, 0, count);
}
#else
inline ustime ptime_delay_ms(unsigned msecs)
{ return ustime_delay_milliseconds(msecs); }
inline usduration ptime_ms(unsigned msecs)
{ return usduration_from_milliseconds(msecs); }
#endif
// boost_systemclock_delay_ms + boost_systemclock_ms
#if BOOST_CXX_VERSION >= 201103L
inline boost::chrono::system_clock::time_point boost_systemclock_delay_ms(unsigned msecs)
@@ -80,14 +93,15 @@ inline boost::posix_time::time_duration ptime_ms(unsigned msecs)
inline boost::chrono::milliseconds boost_systemclock_ms(unsigned msecs)
{ return boost::chrono::milliseconds(msecs); }
#else
inline boost::posix_time::ptime boost_systemclock_delay_ms(unsigned msecs)
{ return ptime_delay_ms(msecs); }
inline boost::posix_time::time_duration boost_systemclock_ms(unsigned msecs)
{ return ptime_ms(msecs); }
inline ustime boost_systemclock_delay_ms(unsigned msecs)
{ return ustime_delay_milliseconds(msecs); }
inline usduration boost_systemclock_ms(unsigned msecs)
{ return usduration_from_milliseconds(msecs); }
#endif
// std_systemclock_delay_ms + std_systemclock_ms
#if !defined(BOOST_NO_CXX11_HDR_CHRONO)
//Use std chrono if available
inline std::chrono::system_clock::time_point std_systemclock_delay_ms(unsigned msecs)
@@ -103,15 +117,15 @@ inline boost::posix_time::time_duration ptime_ms(unsigned msecs)
inline boost::chrono::milliseconds std_systemclock_ms(unsigned msecs)
{ return boost_systemclock_ms(msecs); }
#else
inline boost::posix_time::ptime std_systemclock_delay_ms(unsigned msecs)
{ return ptime_delay_ms(msecs); }
inline ustime std_systemclock_delay_ms(unsigned msecs)
{ return ustime_delay_milliseconds(msecs); }
inline boost::posix_time::time_duration std_systemclock_ms(unsigned msecs)
{ return ptime_ms(msecs); }
inline usduration std_systemclock_ms(unsigned msecs)
{ return usduration_from_milliseconds(msecs); }
#endif
// thread_adapter + data
template <typename P>
class thread_adapter