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: // Copyright (C) 2001-2003 Chris@16: // William E. Kempf Chris@16: // Chris@16: // Permission to use, copy, modify, distribute and sell this software Chris@16: // and its documentation for any purpose is hereby granted without fee, Chris@16: // provided that the above copyright notice appear in all copies and Chris@16: // that both that copyright notice and this permission notice appear Chris@16: // in supporting documentation. William E. Kempf makes no representations Chris@16: // about the suitability of this software for any purpose. Chris@16: // It is provided "as is" without express or implied warranty. Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP Chris@16: #define BOOST_INTERPROCESS_RECURSIVE_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: Chris@16: #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \ Chris@16: (defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && defined (BOOST_INTERPROCESS_POSIX_RECURSIVE_MUTEXES)) 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: #endif Chris@16: Chris@16: #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION) 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 interprocess_recursive_mutex and shared_recursive_try_mutex classes Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: //!Wraps a interprocess_mutex that can be placed in shared memory and can be Chris@16: //!shared between processes. Allows several locking calls by the same Chris@16: //!process. Allows timed lock tries Chris@16: class interprocess_recursive_mutex Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: //Non-copyable Chris@16: interprocess_recursive_mutex(const interprocess_recursive_mutex &); Chris@16: interprocess_recursive_mutex &operator=(const interprocess_recursive_mutex &); Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: public: Chris@16: //!Constructor. Chris@16: //!Throws interprocess_exception on error. Chris@16: interprocess_recursive_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_recursive_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. The mutex must be unlocked Chris@16: //! the same number of times it is locked. Chris@16: //!Throws: interprocess_exception on error. Chris@16: void lock(); Chris@16: Chris@16: //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex Chris@16: //!is already locked, returns true when success. The mutex must be unlocked Chris@16: //!the same number of times it is locked. Chris@16: //!Throws: interprocess_exception if a severe error is found Chris@16: bool try_lock(); Chris@16: Chris@16: //!Tries to lock the interprocess_mutex, if interprocess_mutex can't be locked before Chris@16: //!abs_time time, returns false. The mutex must be unlocked Chris@16: //! the same number of times it is locked. Chris@16: //!Throws: interprocess_exception if a severe error is found 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: //! If the mutex supports recursive locking, the mutex must be unlocked the Chris@16: //! same number of times it is locked. Chris@16: //!Throws: interprocess_exception on error. Chris@16: void unlock(); Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: Chris@16: #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION) Chris@16: #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION Chris@16: void take_ownership(){ mutex.take_ownership(); } Chris@16: friend class ipcdetail::robust_emulation_helpers::mutex_traits; Chris@16: ipcdetail::spin_recursive_mutex mutex; Chris@16: #elif defined(BOOST_INTERPROCESS_USE_POSIX) Chris@16: #undef BOOST_INTERPROCESS_USE_POSIX Chris@16: ipcdetail::posix_recursive_mutex mutex; Chris@16: #elif defined(BOOST_INTERPROCESS_USE_WINDOWS) Chris@16: #undef BOOST_INTERPROCESS_USE_WINDOWS Chris@16: ipcdetail::windows_recursive_mutex mutex; Chris@16: #else Chris@16: #error "Unknown platform for interprocess_mutex" Chris@16: #endif Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: inline interprocess_recursive_mutex::interprocess_recursive_mutex(){} Chris@16: Chris@16: inline interprocess_recursive_mutex::~interprocess_recursive_mutex(){} Chris@16: Chris@16: inline void interprocess_recursive_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 (!mutex.timed_lock(wait_time)){ Chris@16: throw interprocess_exception(timeout_when_locking_error, "Interprocess mutex timeout when locking. Possible deadlock: owner died without unlocking?"); Chris@16: } Chris@16: #else Chris@16: mutex.lock(); Chris@16: #endif Chris@16: } Chris@16: Chris@16: inline bool interprocess_recursive_mutex::try_lock() Chris@16: { return mutex.try_lock(); } Chris@16: Chris@16: inline bool interprocess_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time) Chris@16: { return mutex.timed_lock(abs_time); } Chris@16: Chris@16: inline void interprocess_recursive_mutex::unlock() Chris@16: { mutex.unlock(); } Chris@16: Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP