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_DETAIL_POSIX_RECURSIVE_MUTEX_HPP Chris@16: #define BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_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@101: # pragma once Chris@101: #endif Chris@101: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS Chris@16: # include Chris@101: # include Chris@16: #endif Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: namespace ipcdetail { Chris@16: Chris@16: class posix_recursive_mutex Chris@16: { Chris@16: posix_recursive_mutex(const posix_recursive_mutex &); Chris@16: posix_recursive_mutex &operator=(const posix_recursive_mutex &); Chris@16: public: Chris@16: Chris@16: posix_recursive_mutex(); Chris@16: ~posix_recursive_mutex(); Chris@16: Chris@16: void lock(); Chris@16: bool try_lock(); Chris@16: bool timed_lock(const boost::posix_time::ptime &abs_time); Chris@16: void unlock(); Chris@16: Chris@16: private: Chris@16: pthread_mutex_t m_mut; Chris@16: }; Chris@16: Chris@16: inline posix_recursive_mutex::posix_recursive_mutex() Chris@16: { Chris@16: mutexattr_wrapper mut_attr(true); Chris@16: mutex_initializer mut(m_mut, mut_attr); Chris@16: mut.release(); Chris@16: } Chris@16: Chris@16: inline posix_recursive_mutex::~posix_recursive_mutex() Chris@16: { Chris@16: int res = pthread_mutex_destroy(&m_mut); Chris@16: BOOST_ASSERT(res == 0);(void)res; Chris@16: } Chris@16: Chris@16: inline void posix_recursive_mutex::lock() Chris@16: { Chris@16: if (pthread_mutex_lock(&m_mut) != 0) Chris@16: throw lock_exception(); Chris@16: } Chris@16: Chris@16: inline bool posix_recursive_mutex::try_lock() Chris@16: { Chris@16: int res = pthread_mutex_trylock(&m_mut); Chris@16: if (!(res == 0 || res == EBUSY)) Chris@16: throw lock_exception(); Chris@16: return res == 0; Chris@16: } Chris@16: Chris@16: inline bool posix_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time) Chris@16: { Chris@101: #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS Chris@101: //Posix does not support infinity absolute time so handle it here Chris@16: if(abs_time == boost::posix_time::pos_infin){ Chris@16: this->lock(); Chris@16: return true; Chris@16: } Chris@16: Chris@16: timespec ts = ptime_to_timespec(abs_time); Chris@16: int res = pthread_mutex_timedlock(&m_mut, &ts); Chris@16: if (res != 0 && res != ETIMEDOUT) Chris@16: throw lock_exception(); Chris@16: return res == 0; Chris@16: Chris@16: #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS Chris@16: Chris@101: return ipcdetail::try_based_timed_lock(*this, abs_time); Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS Chris@16: } Chris@16: Chris@16: inline void posix_recursive_mutex::unlock() Chris@16: { Chris@16: int res = 0; Chris@16: res = pthread_mutex_unlock(&m_mut); Chris@16: BOOST_ASSERT(res == 0); (void)res; Chris@16: } Chris@16: Chris@16: } //namespace ipcdetail { Chris@16: } //namespace interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP