diff options
Diffstat (limited to 'boost/interprocess/sync/file_lock.hpp')
-rw-r--r-- | boost/interprocess/sync/file_lock.hpp | 102 |
1 files changed, 15 insertions, 87 deletions
diff --git a/boost/interprocess/sync/file_lock.hpp b/boost/interprocess/sync/file_lock.hpp index e0f75465fb..e8d3903dbe 100644 --- a/boost/interprocess/sync/file_lock.hpp +++ b/boost/interprocess/sync/file_lock.hpp @@ -1,6 +1,6 @@ ////////////////////////////////////////////////////////////////////////////// // -// (C) Copyright Ion Gaztanaga 2005-2011. Distributed under the Boost +// (C) Copyright Ion Gaztanaga 2005-2012. 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) // @@ -11,7 +11,7 @@ #ifndef BOOST_INTERPROCESS_FILE_LOCK_HPP #define BOOST_INTERPROCESS_FILE_LOCK_HPP -#if (defined _MSC_VER) && (_MSC_VER >= 1200) +#if defined(_MSC_VER) # pragma once #endif @@ -21,7 +21,9 @@ #include <boost/interprocess/detail/os_file_functions.hpp> #include <boost/interprocess/detail/os_thread_functions.hpp> #include <boost/interprocess/detail/posix_time_types_wrk.hpp> -#include <boost/move/move.hpp> +#include <boost/interprocess/sync/detail/common_algorithms.hpp> +#include <boost/interprocess/sync/detail/locks.hpp> +#include <boost/move/utility_core.hpp> //!\file //!Describes a class that wraps file locking capabilities. @@ -37,10 +39,10 @@ namespace interprocess { //!process so just use file locks to synchronize threads from different processes. class file_lock { - /// @cond + #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) //Non-copyable BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock) - /// @endcond + #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED public: //!Constructs an empty file mapping. @@ -64,10 +66,10 @@ class file_lock //!After the call, "moved" does not represent any file mapping. //!Does not throw file_lock &operator=(BOOST_RV_REF(file_lock) moved) - { + { file_lock tmp(boost::move(moved)); this->swap(tmp); - return *this; + return *this; } //!Closes a file lock. Does not throw. @@ -81,7 +83,7 @@ class file_lock m_file_hnd = other.m_file_hnd; other.m_file_hnd = tmp; } - + //Exclusive locking //!Effects: The calling thread tries to obtain exclusive ownership of the mutex, @@ -137,66 +139,11 @@ class file_lock //!Effects: The calling thread releases the sharable ownership of the mutex. //!Throws: An exception derived from interprocess_exception on error. void unlock_sharable(); - /// @cond + #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) private: file_handle_t m_file_hnd; - bool timed_acquire_file_lock - (file_handle_t hnd, bool &acquired, const boost::posix_time::ptime &abs_time) - { - //Obtain current count and target time - boost::posix_time::ptime now = microsec_clock::universal_time(); - using namespace boost::detail; - - if(now >= abs_time) return false; - - do{ - if(!ipcdetail::try_acquire_file_lock(hnd, acquired)) - return false; - - if(acquired) - return true; - else{ - now = microsec_clock::universal_time(); - - if(now >= abs_time){ - acquired = false; - return true; - } - // relinquish current time slice - ipcdetail::thread_yield(); - } - }while (true); - } - - bool timed_acquire_file_lock_sharable - (file_handle_t hnd, bool &acquired, const boost::posix_time::ptime &abs_time) - { - //Obtain current count and target time - boost::posix_time::ptime now = microsec_clock::universal_time(); - using namespace boost::detail; - - if(now >= abs_time) return false; - - do{ - if(!ipcdetail::try_acquire_file_lock_sharable(hnd, acquired)) - return false; - - if(acquired) - return true; - else{ - now = microsec_clock::universal_time(); - - if(now >= abs_time){ - acquired = false; - return true; - } - // relinquish current time slice - ipcdetail::thread_yield(); - } - }while (true); - } - /// @endcond + #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED }; inline file_lock::file_lock(const char *name) @@ -236,18 +183,7 @@ inline bool file_lock::try_lock() } inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time) -{ - if(abs_time == boost::posix_time::pos_infin){ - this->lock(); - return true; - } - bool result; - if(!this->timed_acquire_file_lock(m_file_hnd, result, abs_time)){ - error_info err(system_error_code()); - throw interprocess_exception(err); - } - return result; -} +{ return ipcdetail::try_based_timed_lock(*this, abs_time); } inline void file_lock::unlock() { @@ -277,16 +213,8 @@ inline bool file_lock::try_lock_sharable() inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time) { - if(abs_time == boost::posix_time::pos_infin){ - this->lock_sharable(); - return true; - } - bool result; - if(!this->timed_acquire_file_lock_sharable(m_file_hnd, result, abs_time)){ - error_info err(system_error_code()); - throw interprocess_exception(err); - } - return result; + ipcdetail::lock_to_sharable<file_lock> lsh(*this); + return ipcdetail::try_based_timed_lock(lsh, abs_time); } inline void file_lock::unlock_sharable() |