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: // Parts of the pthread code come from Boost Threads code. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_MUTEX_HPP Chris@16: #define BOOST_INTERPROCESS_MUTEX_HPP Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 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: Chris@16: #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) Chris@16: #include Chris@16: #define BOOST_INTERPROCESS_USE_POSIX Chris@16: //Experimental... Chris@16: #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS) Chris@16: #include Chris@16: #define BOOST_INTERPROCESS_USE_WINDOWS Chris@16: #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: #include Chris@16: #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: namespace ipcdetail{ Chris@16: namespace robust_emulation_helpers { Chris@16: Chris@16: template Chris@16: class mutex_traits; Chris@16: Chris@16: }}}} Chris@16: Chris@16: #endif Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: //!\file Chris@16: //!Describes a mutex class that can be placed in memory shared by Chris@16: //!several processes. Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: class interprocess_condition; Chris@16: Chris@16: //!Wraps a interprocess_mutex that can be placed in shared memory and can be Chris@16: //!shared between processes. Allows timed lock tries Chris@16: class interprocess_mutex Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: //Non-copyable Chris@16: interprocess_mutex(const interprocess_mutex &); Chris@16: interprocess_mutex &operator=(const interprocess_mutex &); Chris@16: friend class interprocess_condition; Chris@16: Chris@16: public: Chris@16: #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION) Chris@16: #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION Chris@16: typedef ipcdetail::spin_mutex internal_mutex_type; Chris@16: private: Chris@16: friend class ipcdetail::robust_emulation_helpers::mutex_traits; Chris@16: void take_ownership(){ m_mutex.take_ownership(); } Chris@16: public: Chris@16: #elif defined(BOOST_INTERPROCESS_USE_POSIX) Chris@16: #undef BOOST_INTERPROCESS_USE_POSIX Chris@16: typedef ipcdetail::posix_mutex internal_mutex_type; Chris@16: #elif defined(BOOST_INTERPROCESS_USE_WINDOWS) Chris@16: #undef BOOST_INTERPROCESS_USE_WINDOWS Chris@16: typedef ipcdetail::windows_mutex internal_mutex_type; Chris@16: #else Chris@16: #error "Unknown platform for interprocess_mutex" Chris@16: #endif Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: public: Chris@16: Chris@16: //!Constructor. Chris@16: //!Throws interprocess_exception on error. Chris@16: interprocess_mutex(); Chris@16: Chris@16: //!Destructor. If any process uses the mutex after the destructor is called Chris@16: //!the result is undefined. Does not throw. Chris@16: ~interprocess_mutex(); Chris@16: Chris@16: //!Effects: The calling thread tries to obtain ownership of the mutex, and Chris@16: //! if another thread has ownership of the mutex, it waits until it can Chris@16: //! obtain the ownership. If a thread takes ownership of the mutex the Chris@16: //! mutex must be unlocked by the same mutex. Chris@16: //!Throws: interprocess_exception on error. Chris@16: void lock(); Chris@16: Chris@16: //!Effects: The calling thread tries to obtain ownership of the mutex, and Chris@16: //! if another thread has ownership of the mutex returns immediately. Chris@16: //!Returns: If the thread acquires ownership of the mutex, returns true, if Chris@16: //! the another thread has ownership of the mutex, returns false. Chris@16: //!Throws: interprocess_exception on error. Chris@16: bool try_lock(); Chris@16: Chris@16: //!Effects: The calling thread will try to obtain exclusive ownership of the Chris@16: //! mutex if it can do so in until the specified time is reached. If the Chris@16: //! mutex supports recursive locking, the mutex must be unlocked the same Chris@16: //! number of times it is locked. Chris@16: //!Returns: If the thread acquires ownership of the mutex, returns true, if Chris@16: //! the timeout expires 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: //!Effects: The calling thread releases the exclusive ownership of the mutex. Chris@16: //!Throws: interprocess_exception on error. Chris@16: void unlock(); Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: internal_mutex_type &internal_mutex() Chris@16: { return m_mutex; } Chris@16: Chris@16: const internal_mutex_type &internal_mutex() const Chris@16: { return m_mutex; } Chris@16: Chris@16: private: Chris@16: internal_mutex_type m_mutex; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: inline interprocess_mutex::interprocess_mutex(){} Chris@16: Chris@16: inline interprocess_mutex::~interprocess_mutex(){} Chris@16: Chris@16: inline void interprocess_mutex::lock() Chris@16: { Chris@16: #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING Chris@16: boost::posix_time::ptime wait_time Chris@16: = boost::posix_time::microsec_clock::universal_time() Chris@16: + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS); Chris@16: if (!m_mutex.timed_lock(wait_time)) Chris@16: { Chris@16: throw interprocess_exception(timeout_when_locking_error Chris@16: , "Interprocess mutex timeout when locking. Possible deadlock: " Chris@16: "owner died without unlocking?"); Chris@16: } Chris@16: #else Chris@16: m_mutex.lock(); Chris@16: #endif Chris@16: } Chris@16: Chris@16: inline bool interprocess_mutex::try_lock() Chris@16: { return m_mutex.try_lock(); } Chris@16: Chris@16: inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time) Chris@16: { return m_mutex.timed_lock(abs_time); } Chris@16: Chris@16: inline void interprocess_mutex::unlock() Chris@16: { m_mutex.unlock(); } Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_MUTEX_HPP