|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r86212 - in trunk: boost/sync/condition_variables boost/sync/detail/condition_variables libs/sync/test libs/sync/test/run
From: andrey.semashev_at_[hidden]
Date: 2013-10-09 10:11:51
Author: andysem
Date: 2013-10-09 10:11:50 EDT (Wed, 09 Oct 2013)
New Revision: 86212
URL: http://svn.boost.org/trac/boost/changeset/86212
Log:
Completed Windows CV, ported test utils to Boost.Sync primitives.
Text files modified:
trunk/boost/sync/condition_variables/condition_variable.hpp | 2
trunk/boost/sync/detail/condition_variables/condition_variable_posix.hpp | 25 ++++--
trunk/boost/sync/detail/condition_variables/condition_variable_windows.hpp | 99 ++++----------------------
trunk/libs/sync/test/Jamfile.v2 | 1
trunk/libs/sync/test/run/utils.hpp | 143 ++++++++++++++++++++++-----------------
5 files changed, 114 insertions(+), 156 deletions(-)
Modified: trunk/boost/sync/condition_variables/condition_variable.hpp
==============================================================================
--- trunk/boost/sync/condition_variables/condition_variable.hpp Wed Oct 9 07:21:51 2013 (r86211)
+++ trunk/boost/sync/condition_variables/condition_variable.hpp 2013-10-09 10:11:50 EDT (Wed, 09 Oct 2013) (r86212)
@@ -278,7 +278,7 @@
#if defined(BOOST_SYNC_DETAIL_PLATFORM_PTHREAD)
#include <boost/sync/detail/condition_variables/condition_variable_posix.hpp>
#elif defined(BOOST_SYNC_DETAIL_PLATFORM_WINAPI)
-//#include <boost/sync/detail/condition_variables/condition_variable_windows.hpp>
+#include <boost/sync/detail/condition_variables/condition_variable_windows.hpp>
#else
#error Boost.Sync: Unsupported threading API
#endif
Modified: trunk/boost/sync/detail/condition_variables/condition_variable_posix.hpp
==============================================================================
--- trunk/boost/sync/detail/condition_variables/condition_variable_posix.hpp Wed Oct 9 07:21:51 2013 (r86211)
+++ trunk/boost/sync/detail/condition_variables/condition_variable_posix.hpp 2013-10-09 10:11:50 EDT (Wed, 09 Oct 2013) (r86212)
@@ -98,16 +98,16 @@
typename enable_if< is_condition_variable_compatible< Mutex >, void >::type wait(unique_lock< Mutex >& lock)
{
BOOST_ASSERT(lock.owns_lock());
- int const res = sync::detail::posix::pthread_cond_wait(&m_cond, lock.mutex()->native_handle());
- if (res != 0)
- BOOST_SYNC_DETAIL_THROW(wait_error, (res)("boost::sync::condition_variable::wait failed in pthread_cond_wait"));
+ this->priv_wait(lock.mutex()->native_handle());
}
template< typename Mutex, typename Predicate >
typename enable_if< is_condition_variable_compatible< Mutex >, void >::type wait(unique_lock< Mutex >& lock, Predicate pred)
{
+ BOOST_ASSERT(lock.owns_lock());
+ pthread_mutex_t* const mtx = lock.mutex()->native_handle();
while (!pred())
- this->wait(lock);
+ this->priv_wait(mtx);
}
template< typename Mutex, typename Time >
@@ -115,7 +115,7 @@
timed_wait(unique_lock< Mutex >& lock, Time const& t)
{
BOOST_ASSERT(lock.owns_lock());
- return priv_timed_wait(lock, sync::detail::time_traits< Time >::to_sync_unit(t)) == cv_status::no_timeout;
+ return priv_timed_wait(lock, sync::detail::time_traits< Time >::to_sync_unit(t)) == sync::cv_status::no_timeout;
}
template< typename Mutex, typename TimePoint, typename Predicate >
@@ -127,7 +127,7 @@
unit_type abs_timeout = sync::detail::time_traits< TimePoint >::to_sync_unit(t);
while (!pred())
{
- if (this->priv_timed_wait(lock, abs_timeout) != cv_status::no_timeout)
+ if (this->priv_timed_wait(lock, abs_timeout) != sync::cv_status::no_timeout)
return pred();
}
return true;
@@ -141,7 +141,7 @@
sync::detail::system_time_point abs_timeout = sync::detail::system_time_point::now() + sync::detail::time_traits< Duration >::to_sync_unit(t);
while (!pred())
{
- if (this->priv_timed_wait(lock, abs_timeout) != cv_status::no_timeout)
+ if (this->priv_timed_wait(lock, abs_timeout) != sync::cv_status::no_timeout)
return pred();
}
return true;
@@ -164,7 +164,7 @@
unit_type abs_timeout = sync::detail::time_traits< TimePoint >::to_sync_unit(abs_time);
while (!pred())
{
- if (this->priv_timed_wait(lock, abs_timeout) != cv_status::no_timeout)
+ if (this->priv_timed_wait(lock, abs_timeout) != sync::cv_status::no_timeout)
return pred();
}
return true;
@@ -186,7 +186,7 @@
sync::detail::system_time_point abs_timeout = sync::detail::system_time_point::now() + sync::detail::time_traits< Duration >::to_sync_unit(rel_time);
while (!pred())
{
- if (this->priv_timed_wait(lock, abs_timeout) != cv_status::no_timeout)
+ if (this->priv_timed_wait(lock, abs_timeout) != sync::cv_status::no_timeout)
return pred();
}
return true;
@@ -201,6 +201,13 @@
BOOST_DELETED_FUNCTION(condition_variable& operator= (condition_variable const&))
private:
+ void priv_wait(pthread_mutex_t* mtx)
+ {
+ int const res = sync::detail::posix::pthread_cond_wait(&m_cond, mtx);
+ if (res != 0)
+ BOOST_SYNC_DETAIL_THROW(wait_error, (res)("boost::sync::condition_variable::wait failed in pthread_cond_wait"));
+ }
+
template< typename Mutex >
sync::cv_status priv_timed_wait(unique_lock< Mutex >& lock, sync::detail::system_duration dur)
{
Modified: trunk/boost/sync/detail/condition_variables/condition_variable_windows.hpp
==============================================================================
--- trunk/boost/sync/detail/condition_variables/condition_variable_windows.hpp Wed Oct 9 07:21:51 2013 (r86211)
+++ trunk/boost/sync/detail/condition_variables/condition_variable_windows.hpp 2013-10-09 10:11:50 EDT (Wed, 09 Oct 2013) (r86212)
@@ -17,18 +17,15 @@
#ifndef BOOST_SYNC_DETAIL_CONDITION_VARIABLES_CONDITION_VARIABLE_WINDOWS_HPP_INCLUDED_
#define BOOST_SYNC_DETAIL_CONDITION_VARIABLES_CONDITION_VARIABLE_WINDOWS_HPP_INCLUDED_
-#include <cstddef>
#include <boost/assert.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/and.hpp>
#include <boost/sync/detail/config.hpp>
#include <boost/sync/locks/unique_lock_fwd.hpp>
-#include <boost/sync/exceptions/runtime_exception.hpp>
-#include <boost/sync/exceptions/resource_error.hpp>
#include <boost/sync/traits/is_condition_variable_compatible.hpp>
#include <boost/sync/detail/time_traits.hpp>
#include <boost/sync/detail/time_units.hpp>
-#include <boost/sync/detail/throw_exception.hpp>
+#include <boost/sync/detail/condition_variables/basic_condition_variable_windows.hpp>
#include <boost/sync/condition_variables/cv_status.hpp>
#include <boost/sync/detail/header.hpp>
@@ -45,63 +42,40 @@
class condition_variable
{
private:
- pthread_cond_t m_cond;
+ sync::detail::windows::basic_condition_variable m_cond;
public:
-#if defined(PTHREAD_COND_INITIALIZER)
-#if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
#if !defined(BOOST_NO_CXX11_CONSTEXPR)
#define BOOST_SYNC_DEFINES_CONDITION_VARIABLE_CONSTEXPR_CONSTRUCTOR
#endif
- BOOST_CONSTEXPR condition_variable() BOOST_NOEXCEPT : m_cond(PTHREAD_COND_INITIALIZER)
+ BOOST_CONSTEXPR condition_variable() BOOST_NOEXCEPT : m_cond()
{
}
-#else
- condition_variable() BOOST_NOEXCEPT
- {
- BOOST_CONSTEXPR_OR_CONST pthread_cond_t temp = PTHREAD_COND_INITIALIZER;
- m_cond = temp;
- }
-#endif
-#else // defined(PTHREAD_COND_INITIALIZER)
- condition_variable()
- {
- int const res = pthread_cond_init(&m_cond, NULL);
- if (res)
- BOOST_SYNC_DETAIL_THROW(resource_error, (res)("boost::sync::condition_variable constructor failed in pthread_cond_init"));
- }
-#endif // defined(PTHREAD_COND_INITIALIZER)
-
- ~condition_variable()
- {
- BOOST_VERIFY(::pthread_cond_destroy(&m_cond) == 0);
- }
void notify_one() BOOST_NOEXCEPT
{
- BOOST_VERIFY(::pthread_cond_signal(&m_cond) == 0);
+ m_cond.notify_one();
}
void notify_all() BOOST_NOEXCEPT
{
- BOOST_VERIFY(::pthread_cond_broadcast(&m_cond) == 0);
+ m_cond.notify_all();
}
template< typename Mutex >
typename enable_if< is_condition_variable_compatible< Mutex >, void >::type wait(unique_lock< Mutex >& lock)
{
BOOST_ASSERT(lock.owns_lock());
- int const res = sync::detail::posix::pthread_cond_wait(&m_cond, lock.mutex()->native_handle());
- if (res != 0)
- BOOST_SYNC_DETAIL_THROW(runtime_exception, (res)("boost::sync::condition_variable::wait failed in pthread_cond_wait"));
+ m_cond.wait(lock);
}
template< typename Mutex, typename Predicate >
typename enable_if< is_condition_variable_compatible< Mutex >, void >::type wait(unique_lock< Mutex >& lock, Predicate pred)
{
+ BOOST_ASSERT(lock.owns_lock());
while (!pred())
- this->wait(lock);
+ m_cond.wait(lock);
}
template< typename Mutex, typename Time >
@@ -109,7 +83,7 @@
timed_wait(unique_lock< Mutex >& lock, Time const& t)
{
BOOST_ASSERT(lock.owns_lock());
- return priv_timed_wait(lock, sync::detail::time_traits< Time >::to_sync_unit(t)) == cv_status::no_timeout;
+ return m_cond.timed_wait(lock, sync::detail::time_traits< Time >::to_sync_unit(t)) == sync::cv_status::no_timeout;
}
template< typename Mutex, typename TimePoint, typename Predicate >
@@ -121,7 +95,7 @@
unit_type abs_timeout = sync::detail::time_traits< TimePoint >::to_sync_unit(t);
while (!pred())
{
- if (this->priv_timed_wait(lock, abs_timeout) != cv_status::no_timeout)
+ if (m_cond.timed_wait(lock, abs_timeout) != sync::cv_status::no_timeout)
return pred();
}
return true;
@@ -135,18 +109,18 @@
sync::detail::system_time_point abs_timeout = sync::detail::system_time_point::now() + sync::detail::time_traits< Duration >::to_sync_unit(t);
while (!pred())
{
- if (this->priv_timed_wait(lock, abs_timeout) != cv_status::no_timeout)
+ if (m_cond.timed_wait(lock, abs_timeout) != sync::cv_status::no_timeout)
return pred();
}
return true;
}
template< typename Mutex, typename TimePoint >
- typename enable_if< mpl::and_< detail::is_time_tag_of< TimePoint, detail::time_point_tag >, is_condition_variable_compatible< Mutex > >, cv_status >::type
+ typename enable_if< mpl::and_< detail::is_time_tag_of< TimePoint, detail::time_point_tag >, is_condition_variable_compatible< Mutex > >, sync::cv_status >::type
wait_until(unique_lock< Mutex >& lock, TimePoint const& abs_time)
{
BOOST_ASSERT(lock.owns_lock());
- return priv_timed_wait(lock, sync::detail::time_traits< TimePoint >::to_sync_unit(abs_time));
+ return m_cond.timed_wait(lock, sync::detail::time_traits< TimePoint >::to_sync_unit(abs_time));
}
template< typename Mutex, typename TimePoint, typename Predicate >
@@ -158,18 +132,18 @@
unit_type abs_timeout = sync::detail::time_traits< TimePoint >::to_sync_unit(abs_time);
while (!pred())
{
- if (this->priv_timed_wait(lock, abs_timeout) != cv_status::no_timeout)
+ if (m_cond.timed_wait(lock, abs_timeout) != sync::cv_status::no_timeout)
return pred();
}
return true;
}
template< typename Mutex, typename Duration >
- typename enable_if< mpl::and_< detail::is_time_tag_of< Duration, detail::time_duration_tag >, is_condition_variable_compatible< Mutex > >, cv_status >::type
+ typename enable_if< mpl::and_< detail::is_time_tag_of< Duration, detail::time_duration_tag >, is_condition_variable_compatible< Mutex > >, sync::cv_status >::type
wait_for(unique_lock< Mutex >& lock, Duration const& rel_time)
{
BOOST_ASSERT(lock.owns_lock());
- return priv_timed_wait(lock, sync::detail::time_traits< Duration >::to_sync_unit(rel_time));
+ return m_cond.timed_wait(lock, sync::detail::time_traits< Duration >::to_sync_unit(rel_time));
}
template< typename Mutex, typename Duration, typename Predicate >
@@ -180,53 +154,14 @@
sync::detail::system_time_point abs_timeout = sync::detail::system_time_point::now() + sync::detail::time_traits< Duration >::to_sync_unit(rel_time);
while (!pred())
{
- if (this->priv_timed_wait(lock, abs_timeout) != cv_status::no_timeout)
+ if (m_cond.timed_wait(lock, abs_timeout) != sync::cv_status::no_timeout)
return pred();
}
return true;
}
- native_handle_type native_handle() BOOST_NOEXCEPT
- {
- return &m_cond;
- }
-
BOOST_DELETED_FUNCTION(condition_variable(condition_variable const&))
BOOST_DELETED_FUNCTION(condition_variable& operator= (condition_variable const&))
-
-private:
- template< typename Mutex >
- cv_status priv_timed_wait(unique_lock< Mutex >& lock, sync::detail::system_duration dur)
- {
- return priv_timed_wait(lock, sync::detail::system_time_point::now() + dur);
- }
-
- template< typename Mutex >
- cv_status priv_timed_wait(unique_lock< Mutex >& lock, sync::detail::system_time_point const& t)
- {
- int const res = sync::detail::posix::pthread_cond_timedwait(&m_cond, lock.mutex()->native_handle(), &t.get());
- if (res == ETIMEDOUT)
- return cv_status::timeout;
- else if (res != 0)
- BOOST_SYNC_DETAIL_THROW(runtime_exception, (res)("boost::sync::condition_variable timedwait failed in pthread_cond_timedwait"));
- return cv_status::no_timeout;
- }
-
- template< typename Mutex, typename TimePoint >
- cv_status priv_timed_wait(unique_lock< Mutex >& lock, sync::detail::chrono_time_point< TimePoint > const& t)
- {
- typedef TimePoint time_point;
- typedef typename time_point::clock clock;
- typedef typename time_point::duration duration;
- time_point now = clock::now();
- while (now < t.get())
- {
- if (priv_timed_wait(lock, sync::detail::time_traits< duration >::to_sync_unit(t.get() - now)) == cv_status::no_timeout)
- return cv_status::no_timeout;
- now = clock::now();
- }
- return cv_status::timeout;
- }
};
} // namespace posix
Modified: trunk/libs/sync/test/Jamfile.v2
==============================================================================
--- trunk/libs/sync/test/Jamfile.v2 Wed Oct 9 07:21:51 2013 (r86211)
+++ trunk/libs/sync/test/Jamfile.v2 2013-10-09 10:11:50 EDT (Wed, 09 Oct 2013) (r86212)
@@ -13,6 +13,7 @@
<hardcode-dll-paths>true
<library>/boost/system//boost_system
<library>/boost/thread//boost_thread
+ <library>/boost/date_time//boost_date_time
<library>/boost/test//boost_unit_test_framework
;
Modified: trunk/libs/sync/test/run/utils.hpp
==============================================================================
--- trunk/libs/sync/test/run/utils.hpp Wed Oct 9 07:21:51 2013 (r86211)
+++ trunk/libs/sync/test/run/utils.hpp 2013-10-09 10:11:50 EDT (Wed, 09 Oct 2013) (r86212)
@@ -5,13 +5,17 @@
// 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)
-#if !defined(UTIL_INL_WEK01242003)
-#define UTIL_INL_WEK01242003
+#ifndef BOOST_SYNC_LIBS_TEST_UTILS_HPP_INCLUDED_
+#define BOOST_SYNC_LIBS_TEST_UTILS_HPP_INCLUDED_
-#include <boost/thread/xtime.hpp>
-#include <boost/thread/mutex.hpp>
-#include <boost/thread/condition.hpp>
+#include <boost/config.hpp>
+#include <boost/sync/condition_variables/condition_variable.hpp>
+#include <boost/sync/mutexes/mutex.hpp>
+#include <boost/sync/locks/unique_lock.hpp>
+#include <boost/sync/support/boost_date_time.hpp>
#include <boost/thread/thread.hpp>
+#include <boost/thread/thread_time.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
#ifndef DEFAULT_EXECUTION_MONITOR_TYPE
# define DEFAULT_EXECUTION_MONITOR_TYPE execution_monitor::use_condition
@@ -20,42 +24,19 @@
// boostinspect:nounnamed
-
-namespace
-{
-inline boost::xtime delay(int secs, int msecs=0, int nsecs=0)
-{
- const int MILLISECONDS_PER_SECOND = 1000;
- const int NANOSECONDS_PER_SECOND = 1000000000;
- const int NANOSECONDS_PER_MILLISECOND = 1000000;
-
- boost::xtime xt;
- if (boost::TIME_UTC_ != boost::xtime_get (&xt, boost::TIME_UTC_))
- BOOST_ERROR ("boost::xtime_get != boost::TIME_UTC_");
-
- nsecs += xt.nsec;
- msecs += nsecs / NANOSECONDS_PER_MILLISECOND;
- secs += msecs / MILLISECONDS_PER_SECOND;
- nsecs += (msecs % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
- xt.nsec = nsecs % NANOSECONDS_PER_SECOND;
- xt.sec += secs + (nsecs / NANOSECONDS_PER_SECOND);
-
- return xt;
-}
-
-}
namespace boost
{
namespace threads
{
namespace test
{
-inline bool in_range(const boost::xtime& xt, int secs=1)
+inline bool in_range(const boost::system_time& t, unsigned int secs = 1)
{
- boost::xtime min = delay(-secs);
- boost::xtime max = delay(0);
- return (boost::xtime_cmp(xt, min) >= 0) &&
- (boost::xtime_cmp(xt, max) <= 0);
+ boost::system_time now = boost::get_system_time();
+ boost::posix_time::time_duration diff = t - now;
+ if (diff < boost::posix_time::time_duration())
+ diff = -diff;
+ return diff <= boost::posix_time::seconds(secs);
}
}
}
@@ -66,39 +47,55 @@
{
class execution_monitor
{
+ typedef boost::sync::mutex mutex_type;
+
public:
enum wait_type { use_sleep_only, use_mutex, use_condition };
- execution_monitor(wait_type type, int secs)
- : done(false), type(type), secs(secs) { }
+ execution_monitor(wait_type type, int secs) :
+ done(false), type(type), secs(secs)
+ {
+ }
+
void start()
{
- if (type != use_sleep_only) {
- boost::unique_lock<boost::mutex> lock(mutex); done = false;
- } else {
+ if (type != use_sleep_only)
+ {
+ boost::sync::unique_lock< mutex_type > lock(mutex);
+ done = false;
+ }
+ else
+ {
done = false;
}
}
+
void finish()
{
- if (type != use_sleep_only) {
- boost::unique_lock<boost::mutex> lock(mutex);
+ if (type != use_sleep_only)
+ {
+ boost::sync::unique_lock< mutex_type > lock(mutex);
done = true;
if (type == use_condition)
cond.notify_one();
- } else {
+ }
+ else
+ {
done = true;
}
}
+
bool wait()
{
- boost::xtime xt = delay(secs);
+ boost::system_time t = boost::get_system_time() + boost::posix_time::seconds(secs);
if (type != use_condition)
- boost::thread::sleep(xt);
- if (type != use_sleep_only) {
- boost::unique_lock<boost::mutex> lock(mutex);
- while (type == use_condition && !done) {
- if (!cond.timed_wait(lock, xt))
+ boost::this_thread::sleep(t);
+ if (type != use_sleep_only)
+ {
+ boost::sync::unique_lock< mutex_type > lock(mutex);
+ while (type == use_condition && !done)
+ {
+ if (!cond.timed_wait(lock, t))
break;
}
return done;
@@ -107,21 +104,28 @@
}
private:
- boost::mutex mutex;
- boost::condition cond;
+ mutex_type mutex;
+ boost::sync::condition_variable cond;
bool done;
wait_type type;
int secs;
};
-}
+
+} // namespace
+
namespace thread_detail_anon
{
template <typename F>
class indirect_adapter
{
public:
- indirect_adapter(F func, execution_monitor& monitor)
- : func(func), monitor(monitor) { }
+ typedef void result_type;
+
+ indirect_adapter(F const& func, execution_monitor& monitor) :
+ func(func), monitor(monitor)
+ {
+ }
+
void operator()() const
{
try
@@ -137,10 +141,11 @@
monitor.finish();
}
+ BOOST_DELETED_FUNCTION(indirect_adapter& operator= (indirect_adapter const&))
+
private:
F func;
execution_monitor& monitor;
- void operator=(indirect_adapter&);
};
}
@@ -150,7 +155,7 @@
template <typename F>
void timed_test(F func, int secs,
- execution_monitor::wait_type type=DEFAULT_EXECUTION_MONITOR_TYPE)
+ execution_monitor::wait_type type = DEFAULT_EXECUTION_MONITOR_TYPE)
{
execution_monitor monitor(type, secs);
thread_detail_anon::indirect_adapter<F> ifunc(func, monitor);
@@ -169,8 +174,13 @@
class thread_binder
{
public:
- thread_binder(const F& func, const T& param)
- : func(func), param(param) { }
+ typedef void result_type;
+
+ thread_binder(const F& func, const T& param) :
+ func(func), param(param)
+ {
+ }
+
void operator()() const { func(param); }
private:
@@ -184,7 +194,7 @@
namespace
{
template <typename F, typename T>
-thread_detail_anon::thread_binder<F, T> bind(const F& func, const T& param)
+inline thread_detail_anon::thread_binder<F, T> bind(const F& func, const T& param)
{
return thread_detail_anon::thread_binder<F, T>(func, param);
}
@@ -197,13 +207,18 @@
class thread_member_binder
{
public:
- thread_member_binder(R (T::*func)(), T& param)
- : func(func), param(param) { }
+ typedef void result_type;
+
+ thread_member_binder(R (T::*func)(), T& param) :
+ func(func), param(param)
+ {
+ }
+
void operator()() const { (param.*func)(); }
+ BOOST_DELETED_FUNCTION(thread_member_binder& operator= (thread_member_binder const&))
+
private:
- void operator=(thread_member_binder&);
-
R (T::*func)();
T& param;
};
@@ -214,10 +229,10 @@
namespace
{
template <typename R, typename T>
-thread_detail_anon::thread_member_binder<R, T> bind(R (T::*func)(), T& param)
+inline thread_detail_anon::thread_member_binder<R, T> bind(R (T::*func)(), T& param)
{
return thread_detail_anon::thread_member_binder<R, T>(func, param);
}
} // namespace
-#endif
+#endif // BOOST_SYNC_LIBS_TEST_UTILS_HPP_INCLUDED_
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk