Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:20

0001 /*
0002  *
0003  * Copyright (c) 2004
0004  * John Maddock
0005  *
0006  * Use, modification and distribution are subject to the 
0007  * Boost Software License, Version 1.0. (See accompanying file 
0008  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009  *
0010  */
0011  
0012  /*
0013   *   LOCATION:    see http://www.boost.org for most recent version.
0014   *   FILE         static_mutex.hpp
0015   *   VERSION      see <boost/version.hpp>
0016   *   DESCRIPTION: Declares static_mutex lock type, there are three different
0017   *                implementations: POSIX pthreads, WIN32 threads, and portable,
0018   *                these are described in more detail below.
0019   */
0020 
0021 #ifndef BOOST_REGEX_STATIC_MUTEX_HPP
0022 #define BOOST_REGEX_STATIC_MUTEX_HPP
0023 
0024 #include <boost/config.hpp>
0025 #include <boost/regex/config.hpp> // dll import/export options.
0026 
0027 #ifdef BOOST_HAS_PTHREADS
0028 #include <pthread.h>
0029 #endif
0030 
0031 #if defined(BOOST_HAS_PTHREADS) && defined(PTHREAD_MUTEX_INITIALIZER)
0032 //
0033 // pthreads version:
0034 // simple wrap around a pthread_mutex_t initialized with
0035 // PTHREAD_MUTEX_INITIALIZER.
0036 //
0037 namespace boost{
0038 
0039 class static_mutex;
0040 
0041 #define BOOST_STATIC_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER, }
0042 
0043 class BOOST_REGEX_DECL scoped_static_mutex_lock
0044 {
0045 public:
0046    scoped_static_mutex_lock(static_mutex& mut, bool lk = true);
0047    ~scoped_static_mutex_lock();
0048    inline bool locked()const
0049    {
0050       return m_have_lock;
0051    }
0052    inline operator void const*()const
0053    {
0054       return locked() ? this : 0;
0055    }
0056    void lock();
0057    void unlock();
0058 private:
0059    static_mutex& m_mutex;
0060    bool m_have_lock;
0061 };
0062 
0063 class static_mutex
0064 {
0065 public:
0066    typedef scoped_static_mutex_lock scoped_lock;
0067    pthread_mutex_t m_mutex;
0068 };
0069 
0070 } // namespace boost
0071 #elif defined(BOOST_HAS_WINTHREADS)
0072 //
0073 // Win32 version:
0074 // Use a 32-bit int as a lock, along with a test-and-set
0075 // implementation using InterlockedCompareExchange.
0076 //
0077 
0078 #include <boost/cstdint.hpp>
0079 
0080 namespace boost{
0081 
0082 class BOOST_REGEX_DECL scoped_static_mutex_lock;
0083 
0084 class static_mutex
0085 {
0086 public:
0087    typedef scoped_static_mutex_lock scoped_lock;
0088    boost::int32_t m_mutex;
0089 };
0090 
0091 #define BOOST_STATIC_MUTEX_INIT { 0, }
0092 
0093 class BOOST_REGEX_DECL scoped_static_mutex_lock
0094 {
0095 public:
0096    scoped_static_mutex_lock(static_mutex& mut, bool lk = true);
0097    ~scoped_static_mutex_lock();
0098    operator void const*()const
0099    {
0100       return locked() ? this : 0;
0101    }
0102    bool locked()const
0103    {
0104       return m_have_lock;
0105    }
0106    void lock();
0107    void unlock();
0108 private:
0109    static_mutex& m_mutex;
0110    bool m_have_lock;
0111    scoped_static_mutex_lock(const scoped_static_mutex_lock&);
0112    scoped_static_mutex_lock& operator=(const scoped_static_mutex_lock&);
0113 };
0114 
0115 } // namespace
0116 
0117 #else
0118 //
0119 // Portable version of a static mutex based on Boost.Thread library:
0120 // This has to use a single mutex shared by all instances of static_mutex
0121 // because boost::call_once doesn't alow us to pass instance information
0122 // down to the initialisation proceedure.  In fact the initialisation routine
0123 // may need to be called more than once - but only once per instance.
0124 //
0125 // Since this preprocessor path is almost never taken, we hide these header
0126 // dependencies so that build tools don't find them.
0127 //
0128 #define BOOST_REGEX_H1 <boost/thread/once.hpp>
0129 #define BOOST_REGEX_H2 <boost/thread/recursive_mutex.hpp>
0130 #define BOOST_REGEX_H3 <boost/thread/lock_types.hpp>
0131 #include BOOST_REGEX_H1
0132 #include BOOST_REGEX_H2
0133 #include BOOST_REGEX_H3
0134 #undef BOOST_REGEX_H1
0135 #undef BOOST_REGEX_H2
0136 #undef BOOST_REGEX_H3
0137 
0138 namespace boost{
0139 
0140 class BOOST_REGEX_DECL scoped_static_mutex_lock;
0141 extern "C" BOOST_REGEX_DECL void boost_regex_free_static_mutex();
0142 
0143 class BOOST_REGEX_DECL static_mutex
0144 {
0145 public:
0146    typedef scoped_static_mutex_lock scoped_lock;
0147    static void init();
0148    static boost::recursive_mutex* m_pmutex;
0149    static boost::once_flag m_once;
0150 };
0151 
0152 #define BOOST_STATIC_MUTEX_INIT {  }
0153 
0154 class BOOST_REGEX_DECL scoped_static_mutex_lock
0155 {
0156 public:
0157    scoped_static_mutex_lock(static_mutex& mut, bool lk = true);
0158    ~scoped_static_mutex_lock();
0159    operator void const*()const;
0160    bool locked()const;
0161    void lock();
0162    void unlock();
0163 private:
0164    boost::unique_lock<boost::recursive_mutex>* m_plock;
0165    bool m_have_lock;
0166 };
0167 
0168 inline scoped_static_mutex_lock::operator void const*()const
0169 {
0170    return locked() ? this : 0;
0171 }
0172 
0173 inline bool scoped_static_mutex_lock::locked()const
0174 {
0175    return m_have_lock;
0176 }
0177 
0178 } // namespace
0179 
0180 #endif
0181 
0182 #endif