Changeset 81784


Ignore:
Timestamp:
Dec 8, 2012, 8:15:49 AM (6 years ago)
Author:
viboes
Message:

Thread: Added make_strict_lock.

Location:
trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/boost/thread/lock_guard.hpp

    r81506 r81784  
    1010#include <boost/thread/detail/delete.hpp>
    1111#include <boost/thread/detail/move.hpp>
     12#include <boost/thread/detail/lockable_wrapper.hpp>
    1213#include <boost/thread/lock_options.hpp>
    1314#if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
    1415#include <boost/thread/is_locked_by_this_thread.hpp>
     16#include <boost/assert.hpp>
    1517#endif
    16 #include <boost/assert.hpp>
    17 #include <iostream>
    18 #if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
    19 #include <initializer_list>
    20 #endif
     18
    2119#include <boost/config/abi_prefix.hpp>
    2220
    2321namespace boost
    2422{
    25 
    26 #if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
    27   namespace thread_detail
    28   {
    29     template <typename Mutex>
    30     struct lockable_wrapper
    31     {
    32       Mutex* m;
    33       explicit lockable_wrapper(Mutex& m_) :
    34         m(&m_)
    35       {}
    36     };
    37     template <typename Mutex>
    38     struct lockable_adopt_wrapper
    39     {
    40       Mutex* m;
    41       explicit lockable_adopt_wrapper(Mutex& m_) :
    42         m(&m_)
    43       {}
    44     };
    45   }
    46 #endif
    4723
    4824  template <typename Mutex>
  • trunk/boost/thread/strict_lock.hpp

    r81753 r81784  
    88
    99#include <boost/thread/detail/delete.hpp>
     10#include <boost/thread/detail/lockable_wrapper.hpp>
    1011#include <boost/thread/lock_options.hpp>
    1112#include <boost/thread/lock_traits.hpp>
     
    4950    } /*< locks on construction >*/
    5051
     52
     53#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
     54    strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lockable> > l_) :
     55      mtx_(*(const_cast<thread_detail::lockable_wrapper<Lockable>*>(l_.begin())->m))
     56    {
     57      mtx_.lock();
     58    }
     59#endif
     60
    5161    /**
    5262     * Destructor
     
    98108  /**
    99109   * A nested strict lock is a scoped lock guard ensuring the mutex is locked on its
    100    * scope, by taking ownership of an nesting lock, and locking the mutex on construction if not already locked
     110   * scope, by taking ownership of an nesting lock, locking the mutex on construction if not already locked
    101111   * and restoring the ownership to the nesting lock on destruction.
    102112   */
     
    118128     * __Requires: <c>lk.mutex() != null_ptr</c>
    119129     * __Effects: Stores the reference to the lock parameter and takes ownership on it.
    120      * If the lock doesn't owns the mutex @mtx lock it.
    121      * __Postconditions: @c owns_lock()
     130     * If the lock doesn't owns the mutex @c mtx lock it.
     131     * __Postconditions: @c owns_lock(lk.mutex())
    122132     * __StrongException
    123133     * __Throws:
     
    128138     *
    129139     */
    130     nested_strict_lock(Lock& lk) :
     140    explicit nested_strict_lock(Lock& lk) :
    131141      lk_(lk) /*< Store reference to lk >*/
    132142    {
     
    139149    }
    140150
     151#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
     152    nested_strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lock> > l_) :
     153      lk_(*(const_cast<thread_detail::lockable_wrapper<Lock>*>(l_.begin())->m))
     154    {
     155      /*< Define BOOST_THREAD_DONT_CHECK_PRECONDITIONS if you don't want to check lk ownership >*/
     156      BOOST_THREAD_ASSERT_PRECONDITION(  lk_.mutex() != 0,
     157          lock_error()
     158      );
     159      if (!lk_.owns_lock()) lk_.lock(); /*< ensures it is locked >*/
     160      tmp_lk_ = move(lk_); /*< Move ownership to temporary lk >*/
     161    }
     162#endif
     163
    141164    /**
    142165     * Destructor
     
    181204  };
    182205
     206#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
     207  template <typename Lockable>
     208  strict_lock<Lockable> make_strict_lock(Lockable& mtx)
     209  {
     210    return { thread_detail::lockable_wrapper<Lockable>(mtx) };
     211  }
     212  template <typename Lock>
     213  nested_strict_lock<Lock> make_nested_strict_lock(Lock& lk)
     214  {
     215    return { thread_detail::lockable_wrapper<Lock>(lk) };
     216  }
     217#endif
    183218}
    184219#include <boost/config/abi_suffix.hpp>
  • trunk/libs/thread/test/Jamfile.v2

    r81753 r81784  
    481481          [ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/owns_lock_pass.cpp : strict_lock__owns_lock_p ]
    482482          [ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/types_pass.cpp : strict_lock__types_p ]
    483           #[ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp : make_strict_lock_p ]
     483          [ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp : make_strict_lock_p ]
    484484    ;
    485485
     
    492492          [ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/owns_lock_pass.cpp : nested_strict_lock__owns_lock_p ]
    493493          [ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/types_pass.cpp : nested_strict_lock__types_p ]
    494           #[ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp : make_nested_strict_lock_p ]
     494          [ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp : make_nested_strict_lock_p ]
    495495    ;
    496496
  • trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/copy_ctor_fail.cpp

    r81753 r81784  
    2121int main()
    2222{
    23   boost::nested_strict_lock<boost::mutex> lk0(m0);
    24   boost::nested_strict_lock<boost::mutex> lk1 = lk0;
     23  boost::nested_strict_lock<boost::unique_lock<boost::mutex> > lk0(m0);
     24  boost::nested_strict_lock<boost::unique_lock<boost::mutex> > lk1 = lk0;
    2525}
    2626
  • trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp

    r81753 r81784  
    3131boost::mutex m;
    3232
    33 #if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && BOOST_THREAD_USES_CHRONO
     33#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && defined BOOST_THREAD_USES_CHRONO
    3434
    3535void f()
     
    3939  boost::unique_lock<boost::mutex> lg(m);
    4040  {
    41     const auto&& nlg = boost::make_strict_lock(lg); (void)nlg;
     41    const auto&& nlg = boost::make_nested_strict_lock(lg); (void)nlg;
    4242    t1 = Clock::now();
    4343    BOOST_THREAD_TRACE;
  • trunk/libs/thread/test/sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp

    r81753 r81784  
    3030boost::mutex m;
    3131
    32 #if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && BOOST_THREAD_USES_CHRONO
     32#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && defined BOOST_THREAD_USES_CHRONO
    3333
    3434void f()
Note: See TracChangeset for help on using the changeset viewer.