Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 09:46:03

0001 /* Copyright 2006-2024 Joaquin M Lopez Munoz.
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * See http://www.boost.org/libs/flyweight for library home page.
0007  */
0008 
0009 #ifndef BOOST_FLYWEIGHT_DETAIL_RECURSIVE_LW_MUTEX_HPP
0010 #define BOOST_FLYWEIGHT_DETAIL_RECURSIVE_LW_MUTEX_HPP
0011 
0012 #if defined(_MSC_VER)
0013 #pragma once
0014 #endif
0015 
0016 /* Recursive lightweight mutex.
0017  *   - If <mutex> is provided, it uses std::recursive_mutex.
0018  *   - Else if Pthreads is available, it uses a Pthreads mutex with
0019  *     PTHREAD_MUTEX_RECURSIVE attribute.
0020  *   - Else if on Windows/Cygwin, it relies on boost::detail::lightweight_mutex
0021  *     for an implementation based on (recursive) Win32 CRITICAL_SECTION.
0022  *   - Else, it provides a dummy implementation.
0023  */
0024 
0025 #include <boost/config.hpp>
0026 
0027 #if !defined(BOOST_NO_CXX11_HDR_MUTEX)
0028 #include <boost/noncopyable.hpp>
0029 #include <mutex>
0030 
0031 namespace boost{
0032 
0033 namespace flyweights{
0034 
0035 namespace detail{
0036 
0037 struct recursive_lightweight_mutex:noncopyable
0038 {
0039   recursive_lightweight_mutex(){}
0040 
0041   struct scoped_lock;
0042   friend struct scoped_lock;
0043   struct scoped_lock:noncopyable
0044   {
0045   public:
0046     scoped_lock(recursive_lightweight_mutex& m):m_(m.m_){m_.lock();}
0047     ~scoped_lock(){m_.unlock();}
0048 
0049   private:
0050     std::recursive_mutex& m_;
0051   };
0052 
0053 private:
0054   std::recursive_mutex m_;
0055 };
0056 
0057 } /* namespace flyweights::detail */
0058 
0059 } /* namespace flyweights */
0060 
0061 } /* namespace boost */
0062 #elif defined(BOOST_HAS_PTHREADS)
0063 /* code shamelessly ripped from <boost/smart_ptr/detail/lwm_pthreads.hpp> */
0064 
0065 #include <boost/assert.hpp>
0066 #include <boost/noncopyable.hpp>
0067 #include <pthread.h>
0068 
0069 namespace boost{
0070 
0071 namespace flyweights{
0072 
0073 namespace detail{
0074 
0075 struct recursive_lightweight_mutex:noncopyable
0076 {
0077   recursive_lightweight_mutex()
0078   {
0079     pthread_mutexattr_t attr;
0080     BOOST_VERIFY(pthread_mutexattr_init(&attr)==0);
0081     BOOST_VERIFY(pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE)==0);
0082     BOOST_VERIFY(pthread_mutex_init(&m_,&attr)==0);
0083     BOOST_VERIFY(pthread_mutexattr_destroy(&attr)==0);
0084   }
0085 
0086   ~recursive_lightweight_mutex(){pthread_mutex_destroy(&m_);}
0087 
0088   struct scoped_lock;
0089   friend struct scoped_lock;
0090   struct scoped_lock:noncopyable
0091   {
0092   public:
0093     scoped_lock(recursive_lightweight_mutex& m):m_(m.m_)
0094     {
0095       BOOST_VERIFY(pthread_mutex_lock(&m_)==0);
0096     }
0097 
0098     ~scoped_lock(){BOOST_VERIFY(pthread_mutex_unlock(&m_)==0);}
0099 
0100   private:
0101     pthread_mutex_t& m_;
0102   };
0103 
0104 private:
0105   pthread_mutex_t m_;
0106 };
0107 
0108 } /* namespace flyweights::detail */
0109 
0110 } /* namespace flyweights */
0111 
0112 } /* namespace boost */
0113 #elif defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
0114 #include <boost/smart_ptr/detail/lightweight_mutex.hpp>
0115 
0116 namespace boost{
0117 
0118 namespace flyweights{
0119 
0120 namespace detail{
0121 
0122 typedef boost::detail::lightweight_mutex recursive_lightweight_mutex;
0123 
0124 } /* namespace flyweights::detail */
0125 
0126 } /* namespace flyweights */
0127 
0128 } /* namespace boost */
0129 #else
0130 namespace boost{
0131 
0132 namespace flyweights{
0133 
0134 namespace detail{
0135 
0136 struct recursive_lightweight_mutex
0137 {
0138   typedef recursive_lightweight_mutex scoped_lock;
0139 };
0140 
0141 } /* namespace flyweights::detail */
0142 
0143 } /* namespace flyweights */
0144 
0145 } /* namespace boost */
0146 #endif
0147 
0148 #endif