Chris@16: /* Copyright 2006-2013 Joaquin M Lopez Munoz. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org/libs/flyweight for library home page. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_FLYWEIGHT_DETAIL_RECURSIVE_LW_MUTEX_HPP Chris@16: #define BOOST_FLYWEIGHT_DETAIL_RECURSIVE_LW_MUTEX_HPP Chris@16: Chris@101: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: /* Recursive lightweight mutex. Relies entirely on Chris@16: * boost::detail::lightweight_mutex, except in Pthreads, where we Chris@16: * explicitly use the PTHREAD_MUTEX_RECURSIVE attribute Chris@16: * (lightweight_mutex uses the default mutex type instead). Chris@16: */ Chris@16: Chris@16: #include Chris@16: Chris@16: #if !defined(BOOST_HAS_PTHREADS) Chris@16: #include Chris@16: namespace boost{ Chris@16: Chris@16: namespace flyweights{ Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: typedef boost::detail::lightweight_mutex recursive_lightweight_mutex; Chris@16: Chris@16: } /* namespace flyweights::detail */ Chris@16: Chris@16: } /* namespace flyweights */ Chris@16: Chris@16: } /* namespace boost */ Chris@16: #else Chris@16: /* code shamelessly ripped from */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ Chris@16: Chris@16: namespace flyweights{ Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: struct recursive_lightweight_mutex:noncopyable Chris@16: { Chris@16: recursive_lightweight_mutex() Chris@16: { Chris@16: pthread_mutexattr_t attr; Chris@16: BOOST_VERIFY(pthread_mutexattr_init(&attr)==0); Chris@16: BOOST_VERIFY(pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE)==0); Chris@16: BOOST_VERIFY(pthread_mutex_init(&m_,&attr)==0); Chris@16: BOOST_VERIFY(pthread_mutexattr_destroy(&attr)==0); Chris@16: } Chris@16: Chris@16: ~recursive_lightweight_mutex(){pthread_mutex_destroy(&m_);} Chris@16: Chris@16: struct scoped_lock; Chris@16: friend struct scoped_lock; Chris@16: struct scoped_lock:noncopyable Chris@16: { Chris@16: public: Chris@16: scoped_lock(recursive_lightweight_mutex& m):m_(m.m_) Chris@16: { Chris@16: BOOST_VERIFY(pthread_mutex_lock(&m_)==0); Chris@16: } Chris@16: Chris@16: ~scoped_lock(){BOOST_VERIFY(pthread_mutex_unlock(&m_)==0);} Chris@16: Chris@16: private: Chris@16: pthread_mutex_t& m_; Chris@16: }; Chris@16: Chris@16: private: Chris@16: pthread_mutex_t m_; Chris@16: }; Chris@16: Chris@16: } /* namespace flyweights::detail */ Chris@16: Chris@16: } /* namespace flyweights */ Chris@16: Chris@16: } /* namespace boost */ Chris@16: #endif Chris@16: Chris@16: #endif