Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2008-07-16 10:41:12


Author: anthonyw
Date: 2008-07-16 10:41:09 EDT (Wed, 16 Jul 2008)
New Revision: 47472
URL: http://svn.boost.org/trac/boost/changeset/47472

Log:
Split lock and try_lock into mutex and range overloads without using enable_if, so it works on Borland compilers
Text files modified:
   trunk/boost/thread/locks.hpp | 309 +++++++++++++++++++++++++++++----------
   trunk/libs/thread/test/test_generic_locks.cpp | 10 +
   2 files changed, 241 insertions(+), 78 deletions(-)

Modified: trunk/boost/thread/locks.hpp
==============================================================================
--- trunk/boost/thread/locks.hpp (original)
+++ trunk/boost/thread/locks.hpp 2008-07-16 10:41:09 EDT (Wed, 16 Jul 2008)
@@ -10,14 +10,14 @@
 #include <algorithm>
 #include <iterator>
 #include <boost/thread/thread_time.hpp>
-#include <boost/utility/enable_if.hpp>
 
 #include <boost/config/abi_prefix.hpp>
 
 namespace boost
 {
     struct xtime;
-
+
+#ifndef __BORLANDC__
     namespace detail
     {
         template<typename T>
@@ -79,7 +79,13 @@
                               detail::has_member_try_lock<T>::value);
         
     };
-
+#else
+ template<typename T>
+ struct is_mutex_type
+ {
+ BOOST_STATIC_CONSTANT(bool, value = false);
+ };
+#endif
 
     struct defer_lock_t
     {};
@@ -99,6 +105,74 @@
     class upgrade_lock;
 
     template<typename Mutex>
+ class unique_lock;
+
+ namespace detail
+ {
+ template<typename Mutex>
+ class try_lock_wrapper;
+ }
+
+#ifdef __BORLANDC__
+ template<typename T>
+ struct is_mutex_type<unique_lock<T> >
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+
+ template<typename T>
+ struct is_mutex_type<shared_lock<T> >
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+
+ template<typename T>
+ struct is_mutex_type<upgrade_lock<T> >
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+
+ template<typename T>
+ struct is_mutex_type<detail::try_lock_wrapper<T> >
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+
+ class mutex;
+ class timed_mutex;
+ class recursive_mutex;
+ class recursive_timed_mutex;
+ class shared_mutex;
+
+ template<>
+ struct is_mutex_type<mutex>
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+ template<>
+ struct is_mutex_type<timed_mutex>
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+ template<>
+ struct is_mutex_type<recursive_mutex>
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+ template<>
+ struct is_mutex_type<recursive_timed_mutex>
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+ template<>
+ struct is_mutex_type<shared_mutex>
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+
+#endif
+
+ template<typename Mutex>
     class lock_guard
     {
     private:
@@ -987,28 +1061,63 @@
         }
     }
 
- template<typename MutexType1,typename MutexType2>
- typename enable_if<is_mutex_type<MutexType1>, void>::type lock(MutexType1& m1,MutexType2& m2)
+ namespace detail
     {
- unsigned const lock_count=2;
- unsigned lock_first=0;
- while(true)
+ template<bool x>
+ struct is_mutex_type_wrapper
+ {};
+
+ template<typename MutexType1,typename MutexType2>
+ void lock_impl(MutexType1& m1,MutexType2& m2,is_mutex_type_wrapper<true>)
         {
- switch(lock_first)
+ unsigned const lock_count=2;
+ unsigned lock_first=0;
+ while(true)
             {
- case 0:
- lock_first=detail::lock_helper(m1,m2);
- if(!lock_first)
- return;
- break;
- case 1:
- lock_first=detail::lock_helper(m2,m1);
- if(!lock_first)
- return;
- lock_first=(lock_first+1)%lock_count;
- break;
+ switch(lock_first)
+ {
+ case 0:
+ lock_first=detail::lock_helper(m1,m2);
+ if(!lock_first)
+ return;
+ break;
+ case 1:
+ lock_first=detail::lock_helper(m2,m1);
+ if(!lock_first)
+ return;
+ lock_first=(lock_first+1)%lock_count;
+ break;
+ }
             }
         }
+
+ template<typename Iterator>
+ void lock_impl(Iterator begin,Iterator end,is_mutex_type_wrapper<false>);
+ }
+
+
+ template<typename MutexType1,typename MutexType2>
+ void lock(MutexType1& m1,MutexType2& m2)
+ {
+ detail::lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
+ }
+
+ template<typename MutexType1,typename MutexType2>
+ void lock(const MutexType1& m1,MutexType2& m2)
+ {
+ detail::lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
+ }
+
+ template<typename MutexType1,typename MutexType2>
+ void lock(MutexType1& m1,const MutexType2& m2)
+ {
+ detail::lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
+ }
+
+ template<typename MutexType1,typename MutexType2>
+ void lock(const MutexType1& m1,const MutexType2& m2)
+ {
+ detail::lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
     }
 
     template<typename MutexType1,typename MutexType2,typename MutexType3>
@@ -1123,10 +1232,52 @@
         }
     }
 
+ namespace detail
+ {
+ template<typename Mutex,bool x=is_mutex_type<Mutex>::value>
+ struct try_lock_impl_return
+ {
+ typedef int type;
+ };
+
+ template<typename Iterator>
+ struct try_lock_impl_return<Iterator,false>
+ {
+ typedef Iterator type;
+ };
+
+ template<typename MutexType1,typename MutexType2>
+ int try_lock_impl(MutexType1& m1,MutexType2& m2,is_mutex_type_wrapper<true>)
+ {
+ return ((int)detail::try_lock_internal(m1,m2))-1;
+ }
+
+ template<typename Iterator>
+ Iterator try_lock_impl(Iterator begin,Iterator end,is_mutex_type_wrapper<false>);
+ }
+
+ template<typename MutexType1,typename MutexType2>
+ typename detail::try_lock_impl_return<MutexType1>::type try_lock(MutexType1& m1,MutexType2& m2)
+ {
+ return detail::try_lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
+ }
+
     template<typename MutexType1,typename MutexType2>
- typename enable_if<is_mutex_type<MutexType1>, int>::type try_lock(MutexType1& m1,MutexType2& m2)
+ typename detail::try_lock_impl_return<MutexType1>::type try_lock(const MutexType1& m1,MutexType2& m2)
     {
- return ((int)detail::try_lock_internal(m1,m2))-1;
+ return detail::try_lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
+ }
+
+ template<typename MutexType1,typename MutexType2>
+ typename detail::try_lock_impl_return<MutexType1>::type try_lock(MutexType1& m1,const MutexType2& m2)
+ {
+ return detail::try_lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
+ }
+
+ template<typename MutexType1,typename MutexType2>
+ typename detail::try_lock_impl_return<MutexType1>::type try_lock(const MutexType1& m1,const MutexType2& m2)
+ {
+ return detail::try_lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
     }
 
     template<typename MutexType1,typename MutexType2,typename MutexType3>
@@ -1148,9 +1299,6 @@
     }
     
 
- template<typename Iterator>
- typename disable_if<is_mutex_type<Iterator>, void>::type lock(Iterator begin,Iterator end);
-
     namespace detail
     {
         template<typename Iterator>
@@ -1178,70 +1326,59 @@
                 }
             }
         };
- }
 
- template<typename Iterator>
- typename disable_if<is_mutex_type<Iterator>, Iterator>::type try_lock(Iterator begin,Iterator end)
- {
- if(begin==end)
- {
- return end;
- }
- typedef typename std::iterator_traits<Iterator>::value_type lock_type;
- unique_lock<lock_type> guard(*begin,try_to_lock);
-
- if(!guard.owns_lock())
- {
- return begin;
- }
- Iterator const failed=try_lock(++begin,end);
- if(failed==end)
+ template<typename Iterator>
+ Iterator try_lock_impl(Iterator begin,Iterator end,is_mutex_type_wrapper<false>)
+
         {
- guard.release();
+ if(begin==end)
+ {
+ return end;
+ }
+ typedef typename std::iterator_traits<Iterator>::value_type lock_type;
+ unique_lock<lock_type> guard(*begin,try_to_lock);
+
+ if(!guard.owns_lock())
+ {
+ return begin;
+ }
+ Iterator const failed=try_lock(++begin,end);
+ if(failed==end)
+ {
+ guard.release();
+ }
+
+ return failed;
         }
-
- return failed;
     }
+
 
- template<typename Iterator>
- typename disable_if<is_mutex_type<Iterator>, void>::type lock(Iterator begin,Iterator end)
+ namespace detail
     {
- typedef typename std::iterator_traits<Iterator>::value_type lock_type;
-
- if(begin==end)
+ template<typename Iterator>
+ void lock_impl(Iterator begin,Iterator end,is_mutex_type_wrapper<false>)
         {
- return;
- }
- bool start_with_begin=true;
- Iterator second=begin;
- ++second;
- Iterator next=second;
+ typedef typename std::iterator_traits<Iterator>::value_type lock_type;
         
- for(;;)
- {
- unique_lock<lock_type> begin_lock(*begin,defer_lock);
- if(start_with_begin)
+ if(begin==end)
             {
- begin_lock.lock();
- Iterator const failed_lock=try_lock(next,end);
- if(failed_lock==end)
- {
- begin_lock.release();
- return;
- }
- start_with_begin=false;
- next=failed_lock;
+ return;
             }
- else
+ bool start_with_begin=true;
+ Iterator second=begin;
+ ++second;
+ Iterator next=second;
+
+ for(;;)
             {
- detail::range_lock_guard<Iterator> guard(next,end);
- if(begin_lock.try_lock())
+ unique_lock<lock_type> begin_lock(*begin,defer_lock);
+ if(start_with_begin)
                 {
- Iterator const failed_lock=try_lock(second,next);
- if(failed_lock==next)
+ begin_lock.lock();
+ Iterator const failed_lock=try_lock(next,end);
+ if(failed_lock==end)
                     {
                         begin_lock.release();
- guard.release();
                         return;
                     }
                     start_with_begin=false;
@@ -1249,16 +1386,32 @@
                 }
                 else
                 {
- start_with_begin=true;
- next=second;
+ detail::range_lock_guard<Iterator> guard(next,end);
+ if(begin_lock.try_lock())
+ {
+ Iterator const failed_lock=try_lock(second,next);
+ if(failed_lock==next)
+ {
+ begin_lock.release();
+ guard.release();
+ return;
+ }
+ start_with_begin=false;
+ next=failed_lock;
+ }
+ else
+ {
+ start_with_begin=true;
+ next=second;
+ }
                 }
             }
         }
+
     }
     
 }
 
 #include <boost/config/abi_suffix.hpp>
-#include <boost/mpl/identity.hpp>
 
 #endif

Modified: trunk/libs/thread/test/test_generic_locks.cpp
==============================================================================
--- trunk/libs/thread/test/test_generic_locks.cpp (original)
+++ trunk/libs/thread/test/test_generic_locks.cpp 2008-07-16 10:41:09 EDT (Wed, 16 Jul 2008)
@@ -272,6 +272,16 @@
     }
 };
 
+namespace boost
+{
+ template<>
+ struct is_mutex_type<dummy_mutex>
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+}
+
+
 
 void test_lock_five_in_range()
 {


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