Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_FILE_LOCK_HPP Chris@16: #define BOOST_INTERPROCESS_FILE_LOCK_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@101: #include Chris@16: Chris@16: //!\file Chris@16: //!Describes a class that wraps file locking capabilities. Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: Chris@16: //!A file lock, is a mutual exclusion utility similar to a mutex using a Chris@16: //!file. A file lock has sharable and exclusive locking capabilities and Chris@16: //!can be used with scoped_lock and sharable_lock classes. Chris@16: //!A file lock can't guarantee synchronization between threads of the same Chris@16: //!process so just use file locks to synchronize threads from different processes. Chris@16: class file_lock Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: //Non-copyable Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock) Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: //!Constructs an empty file mapping. Chris@16: //!Does not throw Chris@16: file_lock() Chris@16: : m_file_hnd(file_handle_t(ipcdetail::invalid_file())) Chris@16: {} Chris@16: Chris@16: //!Opens a file lock. Throws interprocess_exception if the file does not Chris@16: //!exist or there are no operating system resources. Chris@16: file_lock(const char *name); Chris@16: Chris@16: //!Moves the ownership of "moved"'s file mapping object to *this. Chris@16: //!After the call, "moved" does not represent any file mapping object. Chris@16: //!Does not throw Chris@16: file_lock(BOOST_RV_REF(file_lock) moved) Chris@16: : m_file_hnd(file_handle_t(ipcdetail::invalid_file())) Chris@16: { this->swap(moved); } Chris@16: Chris@16: //!Moves the ownership of "moved"'s file mapping to *this. Chris@16: //!After the call, "moved" does not represent any file mapping. Chris@16: //!Does not throw Chris@16: file_lock &operator=(BOOST_RV_REF(file_lock) moved) Chris@16: { Chris@16: file_lock tmp(boost::move(moved)); Chris@16: this->swap(tmp); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //!Closes a file lock. Does not throw. Chris@16: ~file_lock(); Chris@16: Chris@16: //!Swaps two file_locks. Chris@16: //!Does not throw. Chris@16: void swap(file_lock &other) Chris@16: { Chris@16: file_handle_t tmp = m_file_hnd; Chris@16: m_file_hnd = other.m_file_hnd; Chris@16: other.m_file_hnd = tmp; Chris@16: } Chris@16: Chris@16: //Exclusive locking Chris@16: Chris@16: //!Effects: The calling thread tries to obtain exclusive ownership of the mutex, Chris@16: //! and if another thread has exclusive, or sharable ownership of Chris@16: //! the mutex, it waits until it can obtain the ownership. Chris@16: //!Throws: interprocess_exception on error. Chris@16: void lock(); Chris@16: Chris@16: //!Effects: The calling thread tries to acquire exclusive ownership of the mutex Chris@16: //! without waiting. If no other thread has exclusive, or sharable Chris@16: //! ownership of the mutex this succeeds. Chris@16: //!Returns: If it can acquire exclusive ownership immediately returns true. Chris@16: //! If it has to wait, returns false. Chris@16: //!Throws: interprocess_exception on error. Chris@16: bool try_lock(); Chris@16: Chris@16: //!Effects: The calling thread tries to acquire exclusive ownership of the mutex Chris@16: //! waiting if necessary until no other thread has exclusive, or sharable Chris@16: //! ownership of the mutex or abs_time is reached. Chris@16: //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false. Chris@16: //!Throws: interprocess_exception on error. Chris@16: bool timed_lock(const boost::posix_time::ptime &abs_time); Chris@16: Chris@16: //!Precondition: The thread must have exclusive ownership of the mutex. Chris@16: //!Effects: The calling thread releases the exclusive ownership of the mutex. Chris@16: //!Throws: An exception derived from interprocess_exception on error. Chris@16: void unlock(); Chris@16: Chris@16: //Sharable locking Chris@16: Chris@16: //!Effects: The calling thread tries to obtain sharable ownership of the mutex, Chris@16: //! and if another thread has exclusive ownership of the mutex, waits until Chris@16: //! it can obtain the ownership. Chris@16: //!Throws: interprocess_exception on error. Chris@16: void lock_sharable(); Chris@16: Chris@16: //!Effects: The calling thread tries to acquire sharable ownership of the mutex Chris@16: //! without waiting. If no other thread has exclusive ownership of the Chris@16: //! mutex this succeeds. Chris@16: //!Returns: If it can acquire sharable ownership immediately returns true. If it Chris@16: //! has to wait, returns false. Chris@16: //!Throws: interprocess_exception on error. Chris@16: bool try_lock_sharable(); Chris@16: Chris@16: //!Effects: The calling thread tries to acquire sharable ownership of the mutex Chris@16: //! waiting if necessary until no other thread has exclusive ownership of Chris@16: //! the mutex or abs_time is reached. Chris@16: //!Returns: If acquires sharable ownership, returns true. Otherwise returns false. Chris@16: //!Throws: interprocess_exception on error. Chris@16: bool timed_lock_sharable(const boost::posix_time::ptime &abs_time); Chris@16: Chris@16: //!Precondition: The thread must have sharable ownership of the mutex. Chris@16: //!Effects: The calling thread releases the sharable ownership of the mutex. Chris@16: //!Throws: An exception derived from interprocess_exception on error. Chris@16: void unlock_sharable(); Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: file_handle_t m_file_hnd; Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@16: inline file_lock::file_lock(const char *name) Chris@16: { Chris@16: m_file_hnd = ipcdetail::open_existing_file(name, read_write); Chris@16: Chris@16: if(m_file_hnd == ipcdetail::invalid_file()){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: } Chris@16: Chris@16: inline file_lock::~file_lock() Chris@16: { Chris@16: if(m_file_hnd != ipcdetail::invalid_file()){ Chris@16: ipcdetail::close_file(m_file_hnd); Chris@16: m_file_hnd = ipcdetail::invalid_file(); Chris@16: } Chris@16: } Chris@16: Chris@16: inline void file_lock::lock() Chris@16: { Chris@16: if(!ipcdetail::acquire_file_lock(m_file_hnd)){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: } Chris@16: Chris@16: inline bool file_lock::try_lock() Chris@16: { Chris@16: bool result; Chris@16: if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time) Chris@101: { return ipcdetail::try_based_timed_lock(*this, abs_time); } Chris@16: Chris@16: inline void file_lock::unlock() Chris@16: { Chris@16: if(!ipcdetail::release_file_lock(m_file_hnd)){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: } Chris@16: Chris@16: inline void file_lock::lock_sharable() Chris@16: { Chris@16: if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: } Chris@16: Chris@16: inline bool file_lock::try_lock_sharable() Chris@16: { Chris@16: bool result; Chris@16: if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time) Chris@16: { Chris@101: ipcdetail::lock_to_sharable lsh(*this); Chris@101: return ipcdetail::try_based_timed_lock(lsh, abs_time); Chris@16: } Chris@16: Chris@16: inline void file_lock::unlock_sharable() Chris@16: { Chris@16: if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){ Chris@16: error_info err(system_error_code()); Chris@16: throw interprocess_exception(err); Chris@16: } Chris@16: } Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_FILE_LOCK_HPP