Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r51974 - in sandbox/synchro: boost/synchro boost/synchro/thread libs/synchro/test
From: vicente.botet_at_[hidden]
Date: 2009-03-25 18:21:10


Author: viboes
Date: 2009-03-25 18:21:08 EDT (Wed, 25 Mar 2009)
New Revision: 51974
URL: http://svn.boost.org/trac/boost/changeset/51974

Log:
0.3.1 : Adding synchronize macros

Added:
   sandbox/synchro/boost/synchro/synchronized.hpp (contents, props changed)
Text files modified:
   sandbox/synchro/boost/synchro/lockers.hpp | 7 ++++++
   sandbox/synchro/boost/synchro/thread/mutex.hpp | 13 ++++++++++++
   sandbox/synchro/libs/synchro/test/test_generic_locks.cpp | 43 ++++++++++++++++++++++++++-------------
   3 files changed, 49 insertions(+), 14 deletions(-)

Modified: sandbox/synchro/boost/synchro/lockers.hpp
==============================================================================
--- sandbox/synchro/boost/synchro/lockers.hpp (original)
+++ sandbox/synchro/boost/synchro/lockers.hpp 2009-03-25 18:21:08 EDT (Wed, 25 Mar 2009)
@@ -15,10 +15,17 @@
 #include <boost/thread/locks.hpp>
 #include <boost/synchro/lockable_traits.hpp>
 #include <boost/synchro/locker_options.hpp>
+#include <boost/synchro/is_lockable.hpp>
 
 namespace boost { namespace synchro {
     template<typename Mutex, typename ScopeTag=typename scope_tag<Mutex>::type>
     class unique_locker;
+
+ template<typename T>
+ struct is_lockable<boost::synchro::unique_locker<T> >
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
 
     template<typename Mutex, typename ScopeTag=typename scope_tag<Mutex>::type>
     class try_unique_locker;

Added: sandbox/synchro/boost/synchro/synchronized.hpp
==============================================================================
--- (empty file)
+++ sandbox/synchro/boost/synchro/synchronized.hpp 2009-03-25 18:21:08 EDT (Wed, 25 Mar 2009)
@@ -0,0 +1,80 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2008-2009. 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/synchro for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_SYNCHRO_SYNCHRONIZED__HPP
+#define BOOST_SYNCHRO_SYNCHRONIZED__HPP
+
+#include <boost/synchro/thread/locks.hpp>
+
+namespace boost { namespace synchro { namespace detail {
+ bool no_opt_false() {return false;}
+}}}
+
+#define BOOST_SYNCHRONIZE_EXT(VAR_DECL) \
+ if (bool __stop = boost::synchro::detail::no_opt_false()) {} else \
+ for (VAR_DECL;!__stop; __stop= true)
+
+#define BOOST_SYNCHRONIZE_TYPE(TYPE, VAR, LOCKABLE) \
+ BOOST_SYNCHRONIZE_EXT(boost::synchro::unique_locker<TYPE> VAR(LOCKABLE))
+
+#define BOOST_ADOPT_SYNCHRONIZE_TYPE(TYPE, VAR, LOCKABLE) \
+ BOOST_SYNCHRONIZE_EXT(boost::synchro::unique_locker<TYPE> VAR(LOCKABLE, boost::synchro::adopt_lock))
+#define BOOST_DEFER_SYNCHRONIZE_TYPE(TYPE, VAR, LOCKABLE) \
+ BOOST_SYNCHRONIZE_EXT(boost::synchro::unique_locker<TYPE> VAR(LOCKABLE, boost::synchro::defer_lock))
+#define BOOST_TRY_TO_SYNCHRONIZE_TYPE(TYPE, VAR, LOCKABLE) \
+ BOOST_SYNCHRONIZE_EXT(boost::synchro::unique_locker<TYPE> VAR(LOCKABLE, boost::synchro::try_to_lock))
+#define BOOST_TRY_TO_SYNCHRONIZE_TYPE_UNTIL(TYPE, VAR, LOCKABLE, ABS_TIME) \
+ BOOST_SYNCHRONIZE_EXT(boost::synchro::unique_locker<TYPE> VAR(LOCKABLE, ABS_TIME))
+#define BOOST_SYNCHRONIZE_TYPE_UNTIL(TYPE, VAR, LOCKABLE, ABS_TIME) \
+ BOOST_SYNCHRONIZE_EXT(boost::synchro::unique_locker<TYPE> VAR(ABS_TIME, LOCKABLE))
+
+#define BOOST_SYNCHRONIZE_VAR(VAR, LOCKABLE) \
+ BOOST_SYNCHRONIZE_TYPE(boost::mutex, VAR, LOCKABLE)
+#define BOOST_ADOPT_SYNCHRONIZE_VAR(VAR, LOCKABLE) \
+ BOOST_ADOPT_SYNCHRONIZE_TYPE(boost::mutex, VAR, LOCKABLE)
+#define BOOST_DEFER_SYNCHRONIZE_VAR(VAR, LOCKABLE) \
+ BOOST_DEFER_SYNCHRONIZE_TYPE(boost::mutex, VAR, LOCKABLE)
+#define BOOST_TRY_TO_SYNCHRONIZE_VAR(VAR, LOCKABLE) \
+ BOOST_TRY_TO_SYNCHRONIZE_TYPE(boost::mutex, VAR, LOCKABLE)
+#define BOOST_SYNCHRONIZE_VAR_UNTIL(VAR, LOCKABLE, ABS_TIME) \
+ BOOST_SYNCHRONIZE_TYPE_UNTIL(boost::mutex, VAR, LOCKABLE, ABS_TIME)
+#define BOOST_TRY_TO_SYNCHRONIZE_VAR_UNTIL(VAR, LOCKABLE, ABS_TIME) \
+ BOOST_TRY_TO_SYNCHRONIZE_TYPE_UNTIL(boost::mutex, VAR, LOCKABLE, ABS_TIME)
+
+#define BOOST_SYNCHRONIZE(LOCKABLE) \
+ BOOST_SYNCHRONIZE_VAR(_, LOCKABLE)
+#define BOOST_ADOPT_SYNCHRONIZE(LOCKABLE) \
+ BOOST_ADOPT_SYNCHRONIZE_VAR(_, LOCKABLE)
+#define BOOST_DEFER_SYNCHRONIZE(LOCKABLE) \
+ BOOST_DEFER_SYNCHRONIZE_VAR(_, LOCKABLE)
+#define BOOST_TRY_TO_SYNCHRONIZE(LOCKABLE) \
+ BOOST_TRY_TO_SYNCHRONIZE_VAR(_, LOCKABLE)
+#define BOOST_SYNCHRONIZE_UNTIL(LOCKABLE, ABS_TIME) \
+ BOOST_SYNCHRONIZE_VAR_UNTIL(_, LOCKABLE, ABS_TIME)
+#define BOOST_TRY_TO_SYNCHRONIZE_UNTIL(LOCKABLE, ABS_TIME) \
+ BOOST_TRY_TO_SYNCHRONIZE_VAR_UNTIL(_, LOCKABLE, ABS_TIME)
+
+#define synchronize(LOCKABLE) BOOST_SYNCHRONIZE(LOCKABLE)
+#define adopt_synchronize(LOCKABLE) BOOST_ADOPT_SYNCHRONIZE(LOCKABLE)
+#define defer_synchronize(LOCKABLE) BOOST_DEFER_SYNCHRONIZE(LOCKABLE)
+#define try_to_synchronize(LOCKABLE) BOOST_TRY_TO_SYNCHRONIZE(LOCKABLE)
+#define try_to_synchronize_until(LOCKABLE, ABS_TIME) BOOST_TRY_TO_SYNCHRONIZE_UNTIL(LOCKABLE, ABS_TIME)
+#define synchronize_until(LOCKABLE, ABS_TIME) BOOST_SYNCHRONIZE_UNTIL(LOCKABLE, ABS_TIME)
+
+#define synchronize_var(VAR, LOCKABLE) BOOST_SYNCHRONIZE_VAR(VAR, LOCKABLE)
+#define adopt_synchronize_var(VAR, LOCKABLE) BOOST_ADOPT_SYNCHRONIZE_VAR(VAR, LOCKABLE)
+#define defer_synchronize_var(VAR, LOCKABLE) BOOST_DEFER_SYNCHRONIZE_VAR(VAR, LOCKABLE)
+#define try_to_synchronize_var(VAR, LOCKABLE) BOOST_TRY_TO_SYNCHRONIZE_VAR(VAR, LOCKABLE)
+#define try_to_synchronize_var_until(VAR, LOCKABLE, ABS_TIME) BOOST_TRY_TO_SYNCHRONIZE_VAR_UNTIL(LOCKABLE, ABS_TIME)
+#define synchronize_var_until(VAR, LOCKABLE, ABS_TIME) BOOST_SYNCHRONIZE_VAR_UNTIL(ABS_TIME, LOCKABLE)
+
+#define synchronize_type(TYPE, VAR, LOCKABLE) BOOST_SYNCHRONIZE_TYPE(TYPE, VAR, LOCKABLE)
+
+#endif

Modified: sandbox/synchro/boost/synchro/thread/mutex.hpp
==============================================================================
--- sandbox/synchro/boost/synchro/thread/mutex.hpp (original)
+++ sandbox/synchro/boost/synchro/thread/mutex.hpp 2009-03-25 18:21:08 EDT (Wed, 25 Mar 2009)
@@ -19,6 +19,7 @@
 #include <boost/synchro/thread/lockable_scope_traits.hpp>
 #include <boost/synchro/timeout_exception.hpp>
 #include <boost/synchro/detail/deleted_functions.hpp>
+#include <boost/synchro/is_lockable.hpp>
 #include <boost/synchro/lockable/functions.hpp>
 #include <boost/convert_to/chrono_time_point_to_posix_time_ptime.hpp>
 #include <boost/convert_to/chrono_duration_to_posix_time_duration.hpp>
@@ -49,6 +50,18 @@
     thread_mutex() {}
 };
 
+ template<>
+ struct is_lockable<boost::synchro::thread_mutex>
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+ template<>
+ struct is_lockable<boost::mutex>
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+
+
 template <>
 struct unique_lock_type<thread_mutex> {
     typedef boost::unique_lock<boost::mutex> type;

Modified: sandbox/synchro/libs/synchro/test/test_generic_locks.cpp
==============================================================================
--- sandbox/synchro/libs/synchro/test/test_generic_locks.cpp (original)
+++ sandbox/synchro/libs/synchro/test/test_generic_locks.cpp 2009-03-25 18:21:08 EDT (Wed, 25 Mar 2009)
@@ -16,20 +16,7 @@
 #include <boost/synchro/thread/locks.hpp>
 #include <boost/synchro/timeout_exception.hpp>
 #include <boost/synchro/lockers/array_locker.hpp>
-
-namespace boost { namespace synchro
-{
- template<>
- struct is_lockable<boost::synchro::thread_mutex>
- {
- BOOST_STATIC_CONSTANT(bool, value = true);
- };
- template<typename T>
- struct is_lockable<boost::synchro::unique_locker<T> >
- {
- BOOST_STATIC_CONSTANT(bool, value = true);
- };
-}}
+#include <boost/synchro/synchronized.hpp>
 
 struct dummy_mutex : boost::synchro::lock_traits_base<>
 {
@@ -151,6 +138,33 @@
     BOOST_CHECK(al.owns_lock());
 }
 
+#if 1
+void test_synchronize_two_uncontended()
+{
+ boost::mutex m1,m2;
+ defer_synchronize_var(l1,m1) defer_synchronize_var(l2,m2)
+ {
+ BOOST_CHECK(!l1.owns_lock());
+ BOOST_CHECK(!l2.owns_lock());
+
+ boost::synchro::lockables::lock(l1,l2);
+
+ BOOST_CHECK(l1.owns_lock());
+ BOOST_CHECK(l2.owns_lock());
+ }
+ boost::synchro::lockables::lock(m1,m2);
+ adopt_synchronize_var(l1,m1) adopt_synchronize_var(l2,m2)
+ {
+ BOOST_CHECK(l1.owns_lock());
+ BOOST_CHECK(l2.owns_lock());
+ }
+ synchronize_var(l1,m1) synchronize_var(l2,m2)
+ {
+ BOOST_CHECK(l1.owns_lock());
+ BOOST_CHECK(l2.owns_lock());
+ }
+}
+#endif
 
 struct wait_data
 {
@@ -855,6 +869,7 @@
     test->add(BOOST_TEST_CASE(&test_array_lock_two_uncontended));
     test->add(BOOST_TEST_CASE(&test_array_try_lock_three));
     
+ test->add(BOOST_TEST_CASE(&test_synchronize_two_uncontended));
 
 
     return test;


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