1 /* Copyright 2006-2013 Joaquin M Lopez Munoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
6 * See http://www.boost.org/libs/flyweight for library home page.
9 #ifndef BOOST_FLYWEIGHT_DETAIL_RECURSIVE_LW_MUTEX_HPP
10 #define BOOST_FLYWEIGHT_DETAIL_RECURSIVE_LW_MUTEX_HPP
16 /* Recursive lightweight mutex. Relies entirely on
17 * boost::detail::lightweight_mutex, except in Pthreads, where we
18 * explicitly use the PTHREAD_MUTEX_RECURSIVE attribute
19 * (lightweight_mutex uses the default mutex type instead).
22 #include <boost/config.hpp>
24 #if !defined(BOOST_HAS_PTHREADS)
25 #include <boost/detail/lightweight_mutex.hpp>
32 typedef boost::detail::lightweight_mutex recursive_lightweight_mutex;
34 } /* namespace flyweights::detail */
36 } /* namespace flyweights */
38 } /* namespace boost */
40 /* code shamelessly ripped from <boost/detail/lwm_pthreads.hpp> */
42 #include <boost/assert.hpp>
43 #include <boost/noncopyable.hpp>
52 struct recursive_lightweight_mutex:noncopyable
54 recursive_lightweight_mutex()
56 pthread_mutexattr_t attr;
57 BOOST_VERIFY(pthread_mutexattr_init(&attr)==0);
58 BOOST_VERIFY(pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE)==0);
59 BOOST_VERIFY(pthread_mutex_init(&m_,&attr)==0);
60 BOOST_VERIFY(pthread_mutexattr_destroy(&attr)==0);
63 ~recursive_lightweight_mutex(){pthread_mutex_destroy(&m_);}
66 friend struct scoped_lock;
67 struct scoped_lock:noncopyable
70 scoped_lock(recursive_lightweight_mutex& m):m_(m.m_)
72 BOOST_VERIFY(pthread_mutex_lock(&m_)==0);
75 ~scoped_lock(){BOOST_VERIFY(pthread_mutex_unlock(&m_)==0);}
85 } /* namespace flyweights::detail */
87 } /* namespace flyweights */
89 } /* namespace boost */