mirror of
https://github.com/boostorg/thread.git
synced 2026-02-02 21:32:08 +00:00
Thread: Added make_strict_lock.
[SVN r81784]
This commit is contained in:
43
include/boost/thread/detail/lockable_wrapper.hpp
Normal file
43
include/boost/thread/detail/lockable_wrapper.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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)
|
||||
// (C) Copyright 2012 Vicente J. Botet Escriba
|
||||
|
||||
#ifndef BOOST_THREAD_DETAIL_LOCKABLE_WRAPPER_HPP
|
||||
#define BOOST_THREAD_DETAIL_LOCKABLE_WRAPPER_HPP
|
||||
|
||||
#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
#include <initializer_list>
|
||||
#endif
|
||||
#include <boost/config/abi_prefix.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
namespace thread_detail
|
||||
{
|
||||
template <typename Mutex>
|
||||
struct lockable_wrapper
|
||||
{
|
||||
Mutex* m;
|
||||
explicit lockable_wrapper(Mutex& m_) :
|
||||
m(&m_)
|
||||
{}
|
||||
};
|
||||
template <typename Mutex>
|
||||
struct lockable_adopt_wrapper
|
||||
{
|
||||
Mutex* m;
|
||||
explicit lockable_adopt_wrapper(Mutex& m_) :
|
||||
m(&m_)
|
||||
{}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#include <boost/config/abi_suffix.hpp>
|
||||
|
||||
#endif // header
|
||||
@@ -9,42 +9,18 @@
|
||||
|
||||
#include <boost/thread/detail/delete.hpp>
|
||||
#include <boost/thread/detail/move.hpp>
|
||||
#include <boost/thread/detail/lockable_wrapper.hpp>
|
||||
#include <boost/thread/lock_options.hpp>
|
||||
#if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
|
||||
#include <boost/thread/is_locked_by_this_thread.hpp>
|
||||
#endif
|
||||
#include <boost/assert.hpp>
|
||||
#include <iostream>
|
||||
#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
#include <initializer_list>
|
||||
#endif
|
||||
|
||||
#include <boost/config/abi_prefix.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
namespace thread_detail
|
||||
{
|
||||
template <typename Mutex>
|
||||
struct lockable_wrapper
|
||||
{
|
||||
Mutex* m;
|
||||
explicit lockable_wrapper(Mutex& m_) :
|
||||
m(&m_)
|
||||
{}
|
||||
};
|
||||
template <typename Mutex>
|
||||
struct lockable_adopt_wrapper
|
||||
{
|
||||
Mutex* m;
|
||||
explicit lockable_adopt_wrapper(Mutex& m_) :
|
||||
m(&m_)
|
||||
{}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename Mutex>
|
||||
class lock_guard
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#define BOOST_THREAD_STRICT_LOCK_HPP
|
||||
|
||||
#include <boost/thread/detail/delete.hpp>
|
||||
#include <boost/thread/detail/lockable_wrapper.hpp>
|
||||
#include <boost/thread/lock_options.hpp>
|
||||
#include <boost/thread/lock_traits.hpp>
|
||||
#include <boost/thread/lockable_traits.hpp>
|
||||
@@ -48,6 +49,15 @@ namespace boost
|
||||
mtx.lock();
|
||||
} /*< locks on construction >*/
|
||||
|
||||
|
||||
#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lockable> > l_) :
|
||||
mtx_(*(const_cast<thread_detail::lockable_wrapper<Lockable>*>(l_.begin())->m))
|
||||
{
|
||||
mtx_.lock();
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*
|
||||
@@ -97,7 +107,7 @@ namespace boost
|
||||
|
||||
/**
|
||||
* A nested strict lock is a scoped lock guard ensuring the mutex is locked on its
|
||||
* scope, by taking ownership of an nesting lock, and locking the mutex on construction if not already locked
|
||||
* scope, by taking ownership of an nesting lock, locking the mutex on construction if not already locked
|
||||
* and restoring the ownership to the nesting lock on destruction.
|
||||
*/
|
||||
//[nested_strict_lock
|
||||
@@ -117,8 +127,8 @@ namespace boost
|
||||
*
|
||||
* __Requires: <c>lk.mutex() != null_ptr</c>
|
||||
* __Effects: Stores the reference to the lock parameter and takes ownership on it.
|
||||
* If the lock doesn't owns the mutex @mtx lock it.
|
||||
* __Postconditions: @c owns_lock()
|
||||
* If the lock doesn't owns the mutex @c mtx lock it.
|
||||
* __Postconditions: @c owns_lock(lk.mutex())
|
||||
* __StrongException
|
||||
* __Throws:
|
||||
*
|
||||
@@ -127,7 +137,7 @@ namespace boost
|
||||
* - Any exception that @c lk.lock() can throw.
|
||||
*
|
||||
*/
|
||||
nested_strict_lock(Lock& lk) :
|
||||
explicit nested_strict_lock(Lock& lk) :
|
||||
lk_(lk) /*< Store reference to lk >*/
|
||||
{
|
||||
/*< Define BOOST_THREAD_DONT_CHECK_PRECONDITIONS if you don't want to check lk ownership >*/
|
||||
@@ -138,6 +148,19 @@ namespace boost
|
||||
tmp_lk_ = move(lk); /*< Move ownership to temporary lk >*/
|
||||
}
|
||||
|
||||
#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
nested_strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lock> > l_) :
|
||||
lk_(*(const_cast<thread_detail::lockable_wrapper<Lock>*>(l_.begin())->m))
|
||||
{
|
||||
/*< Define BOOST_THREAD_DONT_CHECK_PRECONDITIONS if you don't want to check lk ownership >*/
|
||||
BOOST_THREAD_ASSERT_PRECONDITION( lk_.mutex() != 0,
|
||||
lock_error()
|
||||
);
|
||||
if (!lk_.owns_lock()) lk_.lock(); /*< ensures it is locked >*/
|
||||
tmp_lk_ = move(lk_); /*< Move ownership to temporary lk >*/
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*
|
||||
@@ -180,6 +203,18 @@ public:
|
||||
{
|
||||
};
|
||||
|
||||
#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
template <typename Lockable>
|
||||
strict_lock<Lockable> make_strict_lock(Lockable& mtx)
|
||||
{
|
||||
return { thread_detail::lockable_wrapper<Lockable>(mtx) };
|
||||
}
|
||||
template <typename Lock>
|
||||
nested_strict_lock<Lock> make_nested_strict_lock(Lock& lk)
|
||||
{
|
||||
return { thread_detail::lockable_wrapper<Lock>(lk) };
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#include <boost/config/abi_suffix.hpp>
|
||||
|
||||
|
||||
@@ -480,7 +480,7 @@ rule thread-compile-fail ( sources : reqs * : name )
|
||||
[ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/default_pass.cpp : strict_lock__cons__default_p ]
|
||||
[ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/owns_lock_pass.cpp : strict_lock__owns_lock_p ]
|
||||
[ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/types_pass.cpp : strict_lock__types_p ]
|
||||
#[ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp : make_strict_lock_p ]
|
||||
[ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp : make_strict_lock_p ]
|
||||
;
|
||||
|
||||
#explicit ts_nested_strict_lock ;
|
||||
@@ -491,7 +491,7 @@ rule thread-compile-fail ( sources : reqs * : name )
|
||||
[ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/default_pass.cpp : nested_strict_lock__cons__default_p ]
|
||||
[ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/owns_lock_pass.cpp : nested_strict_lock__owns_lock_p ]
|
||||
[ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/types_pass.cpp : nested_strict_lock__types_p ]
|
||||
#[ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp : make_nested_strict_lock_p ]
|
||||
[ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp : make_nested_strict_lock_p ]
|
||||
;
|
||||
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ boost::mutex m1;
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::nested_strict_lock<boost::mutex> lk0(m0);
|
||||
boost::nested_strict_lock<boost::mutex> lk1 = lk0;
|
||||
boost::nested_strict_lock<boost::unique_lock<boost::mutex> > lk0(m0);
|
||||
boost::nested_strict_lock<boost::unique_lock<boost::mutex> > lk1 = lk0;
|
||||
}
|
||||
|
||||
#include "../../../../remove_error_code_unused_warning.hpp"
|
||||
|
||||
@@ -30,7 +30,7 @@ typedef boost::chrono::nanoseconds ns;
|
||||
|
||||
boost::mutex m;
|
||||
|
||||
#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && BOOST_THREAD_USES_CHRONO
|
||||
#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && defined BOOST_THREAD_USES_CHRONO
|
||||
|
||||
void f()
|
||||
{
|
||||
@@ -38,7 +38,7 @@ void f()
|
||||
time_point t1;
|
||||
boost::unique_lock<boost::mutex> lg(m);
|
||||
{
|
||||
const auto&& nlg = boost::make_strict_lock(lg); (void)nlg;
|
||||
const auto&& nlg = boost::make_nested_strict_lock(lg); (void)nlg;
|
||||
t1 = Clock::now();
|
||||
BOOST_THREAD_TRACE;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ typedef boost::chrono::nanoseconds ns;
|
||||
|
||||
boost::mutex m;
|
||||
|
||||
#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && BOOST_THREAD_USES_CHRONO
|
||||
#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && defined BOOST_THREAD_USES_CHRONO
|
||||
|
||||
void f()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user