| 1 | // Copyright (C) 2000 Stephen Cleary |
| 2 | // Copyright (C) 2018 Peter Dimov |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See |
| 5 | // accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | // |
| 8 | // See http://www.boost.org for updates, documentation, and revision history. |
| 9 | |
| 10 | #ifndef BOOST_POOL_MUTEX_HPP |
| 11 | #define BOOST_POOL_MUTEX_HPP |
| 12 | |
| 13 | #include <boost/config.hpp> |
| 14 | |
| 15 | namespace boost{ namespace details{ namespace pool{ |
| 16 | |
| 17 | class null_mutex |
| 18 | { |
| 19 | private: |
| 20 | |
| 21 | null_mutex(const null_mutex &); |
| 22 | void operator=(const null_mutex &); |
| 23 | |
| 24 | public: |
| 25 | |
| 26 | null_mutex() {} |
| 27 | |
| 28 | static void lock() {} |
| 29 | static void unlock() {} |
| 30 | }; |
| 31 | |
| 32 | }}} // namespace boost::details::pool |
| 33 | |
| 34 | #if !defined(BOOST_HAS_THREADS) || defined(BOOST_NO_MT) || defined(BOOST_POOL_NO_MT) |
| 35 | |
| 36 | namespace boost{ namespace details{ namespace pool{ |
| 37 | |
| 38 | typedef null_mutex default_mutex; |
| 39 | |
| 40 | }}} // namespace boost::details::pool |
| 41 | |
| 42 | #elif !defined(BOOST_NO_CXX11_HDR_MUTEX) |
| 43 | |
| 44 | #include <mutex> |
| 45 | |
| 46 | namespace boost{ namespace details{ namespace pool{ |
| 47 | |
| 48 | typedef std::mutex default_mutex; |
| 49 | |
| 50 | }}} // namespace boost::details::pool |
| 51 | |
| 52 | #elif defined(BOOST_HAS_PTHREADS) |
| 53 | |
| 54 | #include <boost/assert.hpp> |
| 55 | #include <pthread.h> |
| 56 | |
| 57 | namespace boost{ namespace details{ namespace pool{ |
| 58 | |
| 59 | class pt_mutex |
| 60 | { |
| 61 | private: |
| 62 | |
| 63 | pthread_mutex_t m_; |
| 64 | |
| 65 | pt_mutex(pt_mutex const &); |
| 66 | pt_mutex & operator=(pt_mutex const &); |
| 67 | |
| 68 | public: |
| 69 | |
| 70 | pt_mutex() |
| 71 | { |
| 72 | BOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 ); |
| 73 | } |
| 74 | |
| 75 | ~pt_mutex() |
| 76 | { |
| 77 | BOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 ); |
| 78 | } |
| 79 | |
| 80 | void lock() |
| 81 | { |
| 82 | BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); |
| 83 | } |
| 84 | |
| 85 | void unlock() |
| 86 | { |
| 87 | BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | typedef pt_mutex default_mutex; |
| 92 | |
| 93 | }}} // namespace boost::details::pool |
| 94 | |
| 95 | #elif defined(BOOST_HAS_WINTHREADS) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) |
| 96 | |
| 97 | #include <boost/winapi/critical_section.hpp> |
| 98 | |
| 99 | namespace boost{ namespace details{ namespace pool{ |
| 100 | |
| 101 | class cs_mutex |
| 102 | { |
| 103 | private: |
| 104 | |
| 105 | boost::winapi::CRITICAL_SECTION_ cs_; |
| 106 | |
| 107 | cs_mutex(cs_mutex const &); |
| 108 | cs_mutex & operator=(cs_mutex const &); |
| 109 | |
| 110 | public: |
| 111 | |
| 112 | cs_mutex() |
| 113 | { |
| 114 | boost::winapi::InitializeCriticalSection( &cs_ ); |
| 115 | } |
| 116 | |
| 117 | ~cs_mutex() |
| 118 | { |
| 119 | boost::winapi::DeleteCriticalSection( &cs_ ); |
| 120 | } |
| 121 | |
| 122 | void lock() |
| 123 | { |
| 124 | boost::winapi::EnterCriticalSection( &cs_ ); |
| 125 | } |
| 126 | |
| 127 | void unlock() |
| 128 | { |
| 129 | boost::winapi::LeaveCriticalSection( &cs_ ); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | typedef cs_mutex default_mutex; |
| 134 | |
| 135 | }}} // namespace boost::details::pool |
| 136 | |
| 137 | #else |
| 138 | |
| 139 | // Use #define BOOST_DISABLE_THREADS to avoid this error |
| 140 | # error Unrecognized threading platform |
| 141 | |
| 142 | #endif |
| 143 | |
| 144 | #endif // #ifndef BOOST_POOL_MUTEX_HPP |
| 145 | |