Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85466 - in trunk/boost/sync: . detail locks
From: andrey.semashev_at_[hidden]
Date: 2013-08-25 13:22:51


Author: andysem
Date: 2013-08-25 13:22:51 EDT (Sun, 25 Aug 2013)
New Revision: 85466
URL: http://svn.boost.org/trac/boost/changeset/85466

Log:
Started to extract synchronization primitives from Boost.Thread to Boost.Sync. Work in progress, not usable ATM.

Added:
   trunk/boost/sync/
   trunk/boost/sync/detail/
   trunk/boost/sync/detail/config.hpp (contents, props changed)
   trunk/boost/sync/detail/footer.hpp (contents, props changed)
   trunk/boost/sync/detail/header.hpp (contents, props changed)
   trunk/boost/sync/detail/lockable_wrapper.hpp (contents, props changed)
   trunk/boost/sync/locks/
   trunk/boost/sync/locks.hpp (contents, props changed)
   trunk/boost/sync/locks/lock_guard.hpp (contents, props changed)
   trunk/boost/sync/locks/lock_options.hpp (contents, props changed)
   trunk/boost/sync/locks/unique_lock.hpp (contents, props changed)

Added: trunk/boost/sync/detail/config.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/detail/config.hpp 2013-08-25 13:22:51 EDT (Sun, 25 Aug 2013) (r85466)
@@ -0,0 +1,46 @@
+/*
+ * 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 2013 Andrey Semashev
+ */
+/*!
+ * \file config.hpp
+ *
+ * \brief This header is the Boost.Sync library implementation, see the library documentation
+ * at http://www.boost.org/doc/libs/release/libs/sync/doc/html/index.html. In this file
+ * internal configuration macros are defined.
+ */
+
+#ifndef BOOST_SYNC_DETAIL_CONFIG_HPP_INCLUDED_
+#define BOOST_SYNC_DETAIL_CONFIG_HPP_INCLUDED_
+
+#include <boost/config.hpp>
+
+#ifdef BOOST_HAS_PRAGMA_ONCE
+#pragma once
+#endif
+
+// BOOST_SYNC_USE_PTHREAD and BOOST_SYNC_USE_WINAPI_VERSION are user-configurable macros
+#if defined(BOOST_WINDOWS) && !defined(BOOST_SYNC_USE_WINAPI_VERSION)
+#if defined(_WIN32_WINNT)
+#define BOOST_SYNC_USE_WINAPI_VERSION _WIN32_WINNT
+#else
+// By default use Windows 2000 API: _WIN32_WINNT_WIN2K
+#define BOOST_SYNC_USE_WINAPI_VERSION 0x0500
+#endif
+
+#if defined(BOOST_SYNC_USE_PTHREAD)
+#define BOOST_SYNC_AUX_PLATFORM_PTHREAD
+#elif defined(BOOST_SYNC_USE_WINAPI_VERSION)
+#define BOOST_SYNC_AUX_PLATFORM_WINAPI
+#else
+#define BOOST_SYNC_AUX_PLATFORM_PTHREAD
+#endif
+
+#if defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX) || defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
+#define BOOST_SYNC_AUX_NO_CXX11_INITIALIZER_LISTS
+#endif
+
+#endif // BOOST_SYNC_DETAIL_CONFIG_HPP_INCLUDED_

Added: trunk/boost/sync/detail/footer.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/detail/footer.hpp 2013-08-25 13:22:51 EDT (Sun, 25 Aug 2013) (r85466)
@@ -0,0 +1,23 @@
+/*
+ * 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 2013 Andrey Semashev
+ */
+
+#if !defined(BOOST_SYNC_ENABLE_WARNINGS)
+
+#if defined(_MSC_VER)
+
+#pragma warning(pop)
+
+#elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
+
+#pragma GCC diagnostic pop
+
+#endif
+
+#endif // !defined(BOOST_SYNC_ENABLE_WARNINGS)
+
+#include <boost/config/abi_suffix.hpp>

Added: trunk/boost/sync/detail/header.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/detail/header.hpp 2013-08-25 13:22:51 EDT (Sun, 25 Aug 2013) (r85466)
@@ -0,0 +1,61 @@
+/*
+ * 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 2013 Andrey Semashev
+ */
+
+#include <boost/config/abi_prefix.hpp>
+
+#if !defined(BOOST_SYNC_ENABLE_WARNINGS)
+
+#if defined(_MSC_VER)
+
+#pragma warning(push, 3)
+// 'm_A' : class 'A' needs to have dll-interface to be used by clients of class 'B'
+#pragma warning(disable: 4251)
+// non dll-interface class 'A' used as base for dll-interface class 'B'
+#pragma warning(disable: 4275)
+// switch statement contains 'default' but no 'case' labels
+#pragma warning(disable: 4065)
+// 'this' : used in base member initializer list
+#pragma warning(disable: 4355)
+// 'int' : forcing value to bool 'true' or 'false' (performance warning)
+#pragma warning(disable: 4800)
+// unreferenced formal parameter
+#pragma warning(disable: 4100)
+// conditional expression is constant
+#pragma warning(disable: 4127)
+// default constructor could not be generated
+#pragma warning(disable: 4510)
+// copy constructor could not be generated
+#pragma warning(disable: 4511)
+// assignment operator could not be generated
+#pragma warning(disable: 4512)
+// struct 'A' can never be instantiated - user defined constructor required
+#pragma warning(disable: 4610)
+// function marked as __forceinline not inlined
+#pragma warning(disable: 4714)
+// decorated name length exceeded, name was truncated
+#pragma warning(disable: 4503)
+
+#elif defined(__GNUC__) && !(defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)) \
+ && (__GNUC__ * 100 + __GNUC_MINOR__) >= 406
+
+#pragma GCC diagnostic push
+// 'var' defined but not used
+#pragma GCC diagnostic ignored "-Wunused-variable"
+// unused parameter 'arg'
+#pragma GCC diagnostic ignored "-Wunused-parameter"
+// missing initializer for member var
+#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
+
+#if (__GNUC__ * 100 + __GNUC_MINOR__) >= 407
+// typedef ‘foo’ locally defined but not used
+#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
+#endif
+
+#endif
+
+#endif // !defined(BOOST_SYNC_ENABLE_WARNINGS)

Added: trunk/boost/sync/detail/lockable_wrapper.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/detail/lockable_wrapper.hpp 2013-08-25 13:22:51 EDT (Sun, 25 Aug 2013) (r85466)
@@ -0,0 +1,49 @@
+/*
+ * 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-2013 Vicente J. Botet Escriba
+ * (C) Copyright 2013 Andrey Semashev
+ */
+/*!
+ * \file lockable_wrapper.hpp
+ *
+ * \brief This header is the Boost.Sync library implementation, see the library documentation
+ * at http://www.boost.org/doc/libs/release/libs/sync/doc/html/index.html.
+ */
+
+#ifndef BOOST_SYNC_DETAIL_LOCKABLE_WRAPPER_HPP_INCLUDED_
+#define BOOST_SYNC_DETAIL_LOCKABLE_WRAPPER_HPP_INCLUDED_
+
+#include <boost/sync/detail/config.hpp>
+#include <boost/sync/detail/header.hpp>
+
+namespace boost {
+
+namespace sync {
+
+namespace aux {
+
+template< typename MutexT, typename OptionT = void >
+struct lockable_wrapper
+{
+ typedef MutexT mutex_type;
+ typedef OptionT lock_option;
+
+ mutex_type* m_mutex;
+
+ explicit lockable_wrapper(mutex_type& m) : m_mutex(&m)
+ {
+ }
+};
+
+} // namespace aux
+
+} // namespace sync
+
+} // namespace boost
+
+#include <boost/sync/detail/footer.hpp>
+
+#endif // BOOST_SYNC_DETAIL_LOCKABLE_WRAPPER_HPP_INCLUDED_

Added: trunk/boost/sync/locks.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/locks.hpp 2013-08-25 13:22:51 EDT (Sun, 25 Aug 2013) (r85466)
@@ -0,0 +1,25 @@
+/*
+ * 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 2013 Andrey Semashev
+ */
+/*!
+ * \file locks.hpp
+ *
+ * \brief This header includes all lock types.
+ */
+
+#ifndef BOOST_SYNC_LOCKS_HPP_INCLUDED_
+#define BOOST_SYNC_LOCKS_HPP_INCLUDED_
+
+#include <boost/sync/detail/config.hpp>
+
+#ifdef BOOST_HAS_PRAGMA_ONCE
+#pragma once
+#endif
+
+#include <boost/sync/locks/lock_guard.hpp>
+
+#endif // BOOST_SYNC_LOCKS_HPP_INCLUDED_

Added: trunk/boost/sync/locks/lock_guard.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/locks/lock_guard.hpp 2013-08-25 13:22:51 EDT (Sun, 25 Aug 2013) (r85466)
@@ -0,0 +1,103 @@
+/*
+ * 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 2007 Anthony Williams
+ * (C) Copyright 2011-2012 Vicente J. Botet Escriba
+ * (C) Copyright 2013 Andrey Semashev
+ */
+/*!
+ * \file locks/lock_guard.hpp
+ *
+ * \brief This header defines an exclusive lock guard.
+ */
+
+#ifndef BOOST_SYNC_LOCKS_LOCK_GUARD_HPP_INCLUDED_
+#define BOOST_SYNC_LOCKS_LOCK_GUARD_HPP_INCLUDED_
+
+#include <boost/sync/detail/config.hpp>
+#if !defined(BOOST_SYNC_AUX_NO_CXX11_INITIALIZER_LISTS)
+#include <initializer_list>
+#include <boost/sync/detail/lockable_wrapper.hpp>
+#endif
+#include <boost/sync/locks/lock_options.hpp>
+
+#include <boost/sync/detail/header.hpp>
+
+#ifdef BOOST_HAS_PRAGMA_ONCE
+#pragma once
+#endif
+
+namespace boost {
+
+namespace sync {
+
+/*!
+ * \brief A unique lock scope guard
+ */
+template< typename MutexT >
+class lock_guard
+{
+private:
+ MutexT& m_mutex;
+
+public:
+ typedef MutexT mutex_type;
+
+public:
+ explicit lock_guard(mutex_type& m) : m_mutex(m)
+ {
+ m.lock();
+ }
+
+ lock_guard(mutex_type& m, adopt_lock_t) : m_mutex(m)
+ {
+ }
+
+#if !defined(BOOST_SYNC_AUX_NO_CXX11_INITIALIZER_LISTS)
+
+ lock_guard(std::initializer_list< aux::lockable_wrapper< mutex_type > > l) :
+ m_mutex(*(l.begin()->m_mutex))
+ {
+ m_mutex.lock();
+ }
+
+ lock_guard(std::initializer_list< aux::lockable_wrapper< mutex_type, adopt_lock_t > > l) :
+ m_mutex(*(l.begin()->m_mutex))
+ {
+ }
+
+#endif // BOOST_SYNC_AUX_NO_CXX11_INITIALIZER_LISTS
+
+ ~lock_guard()
+ {
+ m_mutex.unlock();
+ }
+
+ BOOST_DELETED_FUNCTION(lock_guard(lock_guard const&))
+ BOOST_DELETED_FUNCTION(lock_guard& operator= (lock_guard const&))
+};
+
+#if !defined(BOOST_SYNC_AUX_NO_CXX11_INITIALIZER_LISTS)
+
+template< typename LockableT >
+inline lock_guard< LockableT > make_lock_guard(LockableT& mtx)
+{
+ return { aux::lockable_wrapper< LockableT >(mtx) };
+}
+template< typename LockableT >
+inline lock_guard< LockableT > make_lock_guard(LockableT& mtx, adopt_lock_t)
+{
+ return { aux::lockable_wrapper< LockableT, adopt_lock_t >(mtx) };
+}
+
+#endif // BOOST_SYNC_AUX_NO_CXX11_INITIALIZER_LISTS
+
+} // namespace sync
+
+} // namespace boost
+
+#include <boost/sync/detail/footer.hpp>
+
+#endif // BOOST_SYNC_LOCKS_LOCK_GUARD_HPP_INCLUDED_

Added: trunk/boost/sync/locks/lock_options.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/locks/lock_options.hpp 2013-08-25 13:22:51 EDT (Sun, 25 Aug 2013) (r85466)
@@ -0,0 +1,46 @@
+/*
+ * 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 2007 Anthony Williams
+ * (C) Copyright 2011-2012 Vicente J. Botet Escriba
+ * (C) Copyright 2013 Andrey Semashev
+ */
+
+#ifndef BOOST_SYNC_LOCKS_LOCK_OPTIONS_HPP_INCLUDED_
+#define BOOST_SYNC_LOCKS_LOCK_OPTIONS_HPP_INCLUDED_
+
+#include <boost/sync/detail/config.hpp>
+
+#ifdef BOOST_HAS_PRAGMA_ONCE
+#pragma once
+#endif
+
+#include <boost/sync/detail/header.hpp>
+
+namespace boost {
+
+namespace sync {
+
+struct defer_lock_t
+{
+};
+struct try_to_lock_t
+{
+};
+struct adopt_lock_t
+{
+};
+
+BOOST_CONSTEXPR_OR_CONST defer_lock_t defer_lock = {};
+BOOST_CONSTEXPR_OR_CONST try_to_lock_t try_to_lock = {};
+BOOST_CONSTEXPR_OR_CONST adopt_lock_t adopt_lock = {};
+
+} // namespace sync
+
+} // namespace boost
+
+#include <boost/sync/detail/footer.hpp>
+
+#endif // BOOST_SYNC_LOCKS_LOCK_OPTIONS_HPP_INCLUDED_

Added: trunk/boost/sync/locks/unique_lock.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/locks/unique_lock.hpp 2013-08-25 13:22:51 EDT (Sun, 25 Aug 2013) (r85466)
@@ -0,0 +1,418 @@
+/*
+ * 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 2007 Anthony Williams
+ * (C) Copyright 2011-2012 Vicente J. Botet Escriba
+ * (C) Copyright 2013 Andrey Semashev
+ */
+/*!
+ * \file locks/lock_guard.hpp
+ *
+ * \brief This header defines an exclusive lock guard.
+ */
+
+#ifndef BOOST_SYNC_LOCKS_LOCK_GUARD_HPP_INCLUDED_
+#define BOOST_SYNC_LOCKS_LOCK_GUARD_HPP_INCLUDED_
+
+#include <cstddef>
+#include <boost/utility/enable_if.hpp>
+#include <boost/sync/detail/config.hpp>
+#if !defined(BOOST_SYNC_AUX_NO_CXX11_INITIALIZER_LISTS)
+#include <initializer_list>
+#include <boost/sync/detail/lockable_wrapper.hpp>
+#endif
+#include <boost/sync/locks/lock_options.hpp>
+
+#include <boost/sync/detail/header.hpp>
+
+#ifdef BOOST_HAS_PRAGMA_ONCE
+#pragma once
+#endif
+
+namespace boost {
+
+namespace sync {
+
+/*!
+ * \brief A unique lock scope guard
+ */
+template< typename MutexT >
+class unique_lock
+{
+public:
+ typedef MutexT mutex_type;
+
+private:
+ mutex_type* m_mutex;
+ bool m_is_locked;
+
+private:
+ explicit unique_lock(upgrade_lock< mutex_type >&);
+ unique_lock& operator= (upgrade_lock< mutex_type >& other);
+
+public:
+ BOOST_THREAD_MOVABLE_ONLY( unique_lock)
+
+ unique_lock() BOOST_NOEXCEPT :
+ m_mutex(NULL), m_is_locked(false)
+ {
+ }
+
+ explicit unique_lock(mutex_type& m) :
+ m_mutex(&m), m_is_locked(false)
+ {
+ lock();
+ }
+ unique_lock(mutex_type& m, adopt_lock_t) BOOST_NOEXCEPT :
+ m_mutex(&m), m_is_locked(true)
+ {
+ }
+ unique_lock(mutex_type& m, defer_lock_t) BOOST_NOEXCEPT :
+ m_mutex(&m), m_is_locked(false)
+ {
+ }
+ unique_lock(mutex_type& m, try_to_lock_t) :
+ m_mutex(&m), m_is_locked(false)
+ {
+ try_lock();
+ }
+
+ template< typename TimeT >
+ unique_lock(typename enable_if_c< aux::time_traits< TimeT >::is_supported, mutex_type& >::type m, TimeT const& t) :
+ m_mutex(&m), m_is_locked(false)
+ {
+ tined_lock(t);
+ }
+
+ unique_lock(BOOST_THREAD_RV_REF(unique_lock) other) BOOST_NOEXCEPT :
+ m(BOOST_THREAD_RV(other).m),m_is_locked(BOOST_THREAD_RV(other).m_is_locked)
+ {
+ BOOST_THREAD_RV(other).m_is_locked = false;
+ BOOST_THREAD_RV(other).m_mutex = NULL;
+ }
+
+ BOOST_THREAD_EXPLICIT_LOCK_CONVERSION unique_lock(BOOST_THREAD_RV_REF_BEG upgrade_lock<Mutex> BOOST_THREAD_RV_REF_END other);
+
+#ifndef BOOST_THREAD_PROVIDES_EXPLICIT_LOCK_CONVERSION
+ //std-2104 unique_lock move-assignment should not be noexcept
+ unique_lock& operator=(BOOST_THREAD_RV_REF_BEG upgrade_lock<Mutex> BOOST_THREAD_RV_REF_END other) //BOOST_NOEXCEPT
+ {
+ unique_lock temp(::boost::move(other));
+ swap(temp);
+ return *this;
+ }
+#endif
+
+ //std-2104 unique_lock move-assignment should not be noexcept
+ unique_lock& operator=(BOOST_THREAD_RV_REF(unique_lock) other) //BOOST_NOEXCEPT
+ {
+ unique_lock temp(::boost::move(other));
+ swap(temp);
+ return *this;
+ }
+
+ // Conversion from upgrade lock
+ unique_lock(BOOST_THREAD_RV_REF_BEG upgrade_lock<mutex_type> BOOST_THREAD_RV_REF_END ul, try_to_lock_t) :
+ m_mutex(NULL), m_is_locked(false)
+ {
+ if (BOOST_THREAD_RV(ul).owns_lock())
+ {
+ if (BOOST_THREAD_RV(ul).mutex()->try_unlock_upgrade_and_lock())
+ {
+ m_mutex = BOOST_THREAD_RV(ul).release();
+ m_is_locked = true;
+ }
+ }
+ else
+ {
+ m_mutex = BOOST_THREAD_RV(ul).release();
+ }
+ }
+
+#ifdef BOOST_THREAD_USES_CHRONO
+ template <class Clock, class Duration>
+ unique_lock(BOOST_THREAD_RV_REF_BEG upgrade_lock<mutex_type> BOOST_THREAD_RV_REF_END ul,
+ const chrono::time_point<Clock, Duration>& abs_time)
+ : m(0),m_is_locked(false)
+ {
+ if (BOOST_THREAD_RV(ul).owns_lock())
+ {
+ if (BOOST_THREAD_RV(ul).mutex()->try_unlock_upgrade_and_lock_until(abs_time))
+ {
+ m = BOOST_THREAD_RV(ul).release();
+ m_is_locked = true;
+ }
+ }
+ else
+ {
+ m = BOOST_THREAD_RV(ul).release();
+ }
+ }
+
+ template <class Rep, class Period>
+ unique_lock(BOOST_THREAD_RV_REF_BEG upgrade_lock<mutex_type> BOOST_THREAD_RV_REF_END ul,
+ const chrono::duration<Rep, Period>& rel_time)
+ : m(0),m_is_locked(false)
+ {
+ if (BOOST_THREAD_RV(ul).owns_lock())
+ {
+ if (BOOST_THREAD_RV(ul).mutex()->try_unlock_upgrade_and_lock_for(rel_time))
+ {
+ m = BOOST_THREAD_RV(ul).release();
+ m_is_locked = true;
+ }
+ }
+ else
+ {
+ m = BOOST_THREAD_RV(ul).release();
+ }
+ }
+#endif
+
+#ifdef BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
+ // Conversion from shared locking
+ unique_lock(BOOST_THREAD_RV_REF_BEG shared_lock<mutex_type> BOOST_THREAD_RV_REF_END sl, try_to_lock_t)
+ : m(0),m_is_locked(false)
+ {
+ if (BOOST_THREAD_RV(sl).owns_lock())
+ {
+ if (BOOST_THREAD_RV(sl).mutex()->try_unlock_shared_and_lock())
+ {
+ m = BOOST_THREAD_RV(sl).release();
+ m_is_locked = true;
+ }
+ }
+ else
+ {
+ m = BOOST_THREAD_RV(sl).release();
+ }
+ }
+
+#ifdef BOOST_THREAD_USES_CHRONO
+ template <class Clock, class Duration>
+ unique_lock(BOOST_THREAD_RV_REF_BEG shared_lock<mutex_type> BOOST_THREAD_RV_REF_END sl,
+ const chrono::time_point<Clock, Duration>& abs_time)
+ : m(0),m_is_locked(false)
+ {
+ if (BOOST_THREAD_RV(sl).owns_lock())
+ {
+ if (BOOST_THREAD_RV(sl).mutex()->try_unlock_shared_and_lock_until(abs_time))
+ {
+ m = BOOST_THREAD_RV(sl).release();
+ m_is_locked = true;
+ }
+ }
+ else
+ {
+ m = BOOST_THREAD_RV(sl).release();
+ }
+ }
+
+ template <class Rep, class Period>
+ unique_lock(BOOST_THREAD_RV_REF_BEG shared_lock<mutex_type> BOOST_THREAD_RV_REF_END sl,
+ const chrono::duration<Rep, Period>& rel_time)
+ : m(0),m_is_locked(false)
+ {
+ if (BOOST_THREAD_RV(sl).owns_lock())
+ {
+ if (BOOST_THREAD_RV(sl).mutex()->try_unlock_shared_and_lock_for(rel_time))
+ {
+ m = BOOST_THREAD_RV(sl).release();
+ m_is_locked = true;
+ }
+ }
+ else
+ {
+ m = BOOST_THREAD_RV(sl).release();
+ }
+ }
+#endif // BOOST_THREAD_USES_CHRONO
+#endif // BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
+
+ void swap(unique_lock& other)BOOST_NOEXCEPT
+ {
+ std::swap(m,other.m);
+ std::swap(m_is_locked,other.m_is_locked);
+ }
+
+ ~unique_lock()
+ {
+ if (owns_lock())
+ {
+ m->unlock();
+ }
+ }
+
+ void lock()
+ {
+ if (m == 0)
+ {
+ boost::throw_exception(
+ boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock has no mutex"));
+ }
+ if (owns_lock())
+ {
+ boost::throw_exception(
+ boost::lock_error(system::errc::resource_deadlock_would_occur, "boost unique_lock owns already the mutex"));
+ }
+ m->lock();
+ m_is_locked = true;
+ }
+
+ bool try_lock()
+ {
+ if (m == 0)
+ {
+ boost::throw_exception(
+ boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock has no mutex"));
+ }
+ if (owns_lock())
+ {
+ boost::throw_exception(
+ boost::lock_error(system::errc::resource_deadlock_would_occur, "boost unique_lock owns already the mutex"));
+ }
+ m_is_locked = m->try_lock();
+ return m_is_locked;
+ }
+#if defined BOOST_THREAD_USES_DATETIME
+ template<typename TimeDuration>
+ bool timed_lock(TimeDuration const& relative_time)
+ {
+ if(m==0)
+ {
+ boost::throw_exception(boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock has no mutex"));
+ }
+ if(owns_lock())
+ {
+ boost::throw_exception(boost::lock_error(system::errc::resource_deadlock_would_occur, "boost unique_lock owns already the mutex"));
+ }
+ m_is_locked=m->timed_lock(relative_time);
+ return m_is_locked;
+ }
+
+ bool timed_lock(::boost::system_time const& absolute_time)
+ {
+ if(m==0)
+ {
+ boost::throw_exception(boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock has no mutex"));
+ }
+ if(owns_lock())
+ {
+ boost::throw_exception(boost::lock_error(system::errc::resource_deadlock_would_occur, "boost unique_lock owns already the mutex"));
+ }
+ m_is_locked=m->timed_lock(absolute_time);
+ return m_is_locked;
+ }
+ bool timed_lock(::boost::xtime const& absolute_time)
+ {
+ if(m==0)
+ {
+ boost::throw_exception(boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock has no mutex"));
+ }
+ if(owns_lock())
+ {
+ boost::throw_exception(boost::lock_error(system::errc::resource_deadlock_would_occur, "boost unique_lock owns already the mutex"));
+ }
+ m_is_locked=m->timed_lock(absolute_time);
+ return m_is_locked;
+ }
+#endif
+#ifdef BOOST_THREAD_USES_CHRONO
+
+ template <class Rep, class Period>
+ bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
+ {
+ if(m==0)
+ {
+ boost::throw_exception(boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock has no mutex"));
+ }
+ if(owns_lock())
+ {
+ boost::throw_exception(boost::lock_error(system::errc::resource_deadlock_would_occur, "boost unique_lock owns already the mutex"));
+ }
+ m_is_locked=m->try_lock_for(rel_time);
+ return m_is_locked;
+ }
+ template <class Clock, class Duration>
+ bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time)
+ {
+ if(m==0)
+ {
+ boost::throw_exception(boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock has no mutex"));
+ }
+ if(owns_lock())
+ {
+ boost::throw_exception(boost::lock_error(system::errc::resource_deadlock_would_occur, "boost unique_lock owns already the mutex"));
+ }
+ m_is_locked=m->try_lock_until(abs_time);
+ return m_is_locked;
+ }
+#endif
+
+ void unlock()
+ {
+ if (m == 0)
+ {
+ boost::throw_exception(
+ boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock has no mutex"));
+ }
+ if (!owns_lock())
+ {
+ boost::throw_exception(
+ boost::lock_error(system::errc::operation_not_permitted, "boost unique_lock doesn't own the mutex"));
+ }
+ m->unlock();
+ m_is_locked = false;
+ }
+
+#if defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
+ typedef void (unique_lock::*bool_type)();
+ operator bool_type() const BOOST_NOEXCEPT
+ {
+ return m_is_locked?&unique_lock::lock:0;
+ }
+ bool operator!() const BOOST_NOEXCEPT
+ {
+ return !owns_lock();
+ }
+#else
+ explicit operator bool() const BOOST_NOEXCEPT
+ {
+ return owns_lock();
+ }
+#endif
+ bool owns_lock() const BOOST_NOEXCEPT
+ {
+ return m_is_locked;
+ }
+
+ Mutex* mutex() const BOOST_NOEXCEPT
+ {
+ return m;
+ }
+
+ Mutex* release() BOOST_NOEXCEPT
+ {
+ Mutex* const res=m;
+ m=0;
+ m_is_locked=false;
+ return res;
+ }
+};
+
+template< typename MutexT >
+inline void swap(unique_lock< MutexT >& lhs, unique_lock< MutexT >& rhs) BOOST_NOEXCEPT
+{
+ lhs.swap(rhs);
+}
+
+
+} // namespace sync
+
+} // namespace boost
+
+#include <boost/sync/detail/footer.hpp>
+
+#endif // BOOST_SYNC_LOCKS_LOCK_GUARD_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