| 1 | // Copyright (C) 2001-2003 |
|---|
| 2 | // William E. Kempf |
|---|
| 3 | // |
|---|
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
|---|
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 6 | |
|---|
| 7 | #ifndef BOOST_RECURSIVE_MUTEX_WEK070601_HPP |
|---|
| 8 | #define BOOST_RECURSIVE_MUTEX_WEK070601_HPP |
|---|
| 9 | |
|---|
| 10 | #include <boost/thread/detail/config.hpp> |
|---|
| 11 | |
|---|
| 12 | #include <boost/utility.hpp> |
|---|
| 13 | #include <boost/thread/detail/lock.hpp> |
|---|
| 14 | |
|---|
| 15 | #if defined(BOOST_HAS_PTHREADS) |
|---|
| 16 | # include <pthread.h> |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | #if defined(BOOST_HAS_MPTASKS) |
|---|
| 20 | # include "scoped_critical_region.hpp" |
|---|
| 21 | #endif |
|---|
| 22 | |
|---|
| 23 | namespace boost { |
|---|
| 24 | |
|---|
| 25 | struct xtime; |
|---|
| 26 | // disable warnings about non dll import |
|---|
| 27 | // see: http://www.boost.org/more/separate_compilation.html#dlls |
|---|
| 28 | #ifdef BOOST_MSVC |
|---|
| 29 | # pragma warning(push) |
|---|
| 30 | # pragma warning(disable: 4251 4231 4660 4275) |
|---|
| 31 | #endif |
|---|
| 32 | class BOOST_THREAD_DECL recursive_mutex |
|---|
| 33 | : private noncopyable |
|---|
| 34 | { |
|---|
| 35 | public: |
|---|
| 36 | friend class detail::thread::lock_ops<recursive_mutex>; |
|---|
| 37 | |
|---|
| 38 | typedef detail::thread::scoped_lock<recursive_mutex> scoped_lock; |
|---|
| 39 | |
|---|
| 40 | recursive_mutex(); |
|---|
| 41 | ~recursive_mutex(); |
|---|
| 42 | |
|---|
| 43 | private: |
|---|
| 44 | #if (defined(BOOST_HAS_WINTHREADS) || defined(BOOST_HAS_MPTASKS)) |
|---|
| 45 | typedef std::size_t cv_state; |
|---|
| 46 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 47 | struct cv_state |
|---|
| 48 | { |
|---|
| 49 | long count; |
|---|
| 50 | pthread_mutex_t* pmutex; |
|---|
| 51 | }; |
|---|
| 52 | #endif |
|---|
| 53 | void do_lock(); |
|---|
| 54 | void do_unlock(); |
|---|
| 55 | void do_lock(cv_state& state); |
|---|
| 56 | void do_unlock(cv_state& state); |
|---|
| 57 | |
|---|
| 58 | #if defined(BOOST_HAS_WINTHREADS) |
|---|
| 59 | void* m_mutex; |
|---|
| 60 | bool m_critical_section; |
|---|
| 61 | unsigned long m_count; |
|---|
| 62 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 63 | pthread_mutex_t m_mutex; |
|---|
| 64 | unsigned m_count; |
|---|
| 65 | # if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE) |
|---|
| 66 | pthread_cond_t m_unlocked; |
|---|
| 67 | pthread_t m_thread_id; |
|---|
| 68 | bool m_valid_id; |
|---|
| 69 | # endif |
|---|
| 70 | #elif defined(BOOST_HAS_MPTASKS) |
|---|
| 71 | threads::mac::detail::scoped_critical_region m_mutex; |
|---|
| 72 | threads::mac::detail::scoped_critical_region m_mutex_mutex; |
|---|
| 73 | std::size_t m_count; |
|---|
| 74 | #endif |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | class BOOST_THREAD_DECL recursive_try_mutex |
|---|
| 78 | : private noncopyable |
|---|
| 79 | { |
|---|
| 80 | public: |
|---|
| 81 | friend class detail::thread::lock_ops<recursive_try_mutex>; |
|---|
| 82 | |
|---|
| 83 | typedef detail::thread::scoped_lock<recursive_try_mutex> scoped_lock; |
|---|
| 84 | typedef detail::thread::scoped_try_lock< |
|---|
| 85 | recursive_try_mutex> scoped_try_lock; |
|---|
| 86 | |
|---|
| 87 | recursive_try_mutex(); |
|---|
| 88 | ~recursive_try_mutex(); |
|---|
| 89 | |
|---|
| 90 | private: |
|---|
| 91 | #if (defined(BOOST_HAS_WINTHREADS) || defined(BOOST_HAS_MPTASKS)) |
|---|
| 92 | typedef std::size_t cv_state; |
|---|
| 93 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 94 | struct cv_state |
|---|
| 95 | { |
|---|
| 96 | long count; |
|---|
| 97 | pthread_mutex_t* pmutex; |
|---|
| 98 | }; |
|---|
| 99 | #endif |
|---|
| 100 | void do_lock(); |
|---|
| 101 | bool do_trylock(); |
|---|
| 102 | void do_unlock(); |
|---|
| 103 | void do_lock(cv_state& state); |
|---|
| 104 | void do_unlock(cv_state& state); |
|---|
| 105 | |
|---|
| 106 | #if defined(BOOST_HAS_WINTHREADS) |
|---|
| 107 | void* m_mutex; |
|---|
| 108 | bool m_critical_section; |
|---|
| 109 | unsigned long m_count; |
|---|
| 110 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 111 | pthread_mutex_t m_mutex; |
|---|
| 112 | unsigned m_count; |
|---|
| 113 | # if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE) |
|---|
| 114 | pthread_cond_t m_unlocked; |
|---|
| 115 | pthread_t m_thread_id; |
|---|
| 116 | bool m_valid_id; |
|---|
| 117 | # endif |
|---|
| 118 | #elif defined(BOOST_HAS_MPTASKS) |
|---|
| 119 | threads::mac::detail::scoped_critical_region m_mutex; |
|---|
| 120 | threads::mac::detail::scoped_critical_region m_mutex_mutex; |
|---|
| 121 | std::size_t m_count; |
|---|
| 122 | #endif |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | class BOOST_THREAD_DECL recursive_timed_mutex |
|---|
| 126 | : private noncopyable |
|---|
| 127 | { |
|---|
| 128 | public: |
|---|
| 129 | friend class detail::thread::lock_ops<recursive_timed_mutex>; |
|---|
| 130 | |
|---|
| 131 | typedef detail::thread::scoped_lock<recursive_timed_mutex> scoped_lock; |
|---|
| 132 | typedef detail::thread::scoped_try_lock< |
|---|
| 133 | recursive_timed_mutex> scoped_try_lock; |
|---|
| 134 | typedef detail::thread::scoped_timed_lock< |
|---|
| 135 | recursive_timed_mutex> scoped_timed_lock; |
|---|
| 136 | |
|---|
| 137 | recursive_timed_mutex(); |
|---|
| 138 | ~recursive_timed_mutex(); |
|---|
| 139 | |
|---|
| 140 | private: |
|---|
| 141 | #if (defined(BOOST_HAS_WINTHREADS) || defined(BOOST_HAS_MPTASKS)) |
|---|
| 142 | typedef std::size_t cv_state; |
|---|
| 143 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 144 | struct cv_state |
|---|
| 145 | { |
|---|
| 146 | long count; |
|---|
| 147 | pthread_mutex_t* pmutex; |
|---|
| 148 | }; |
|---|
| 149 | #endif |
|---|
| 150 | void do_lock(); |
|---|
| 151 | bool do_trylock(); |
|---|
| 152 | bool do_timedlock(const xtime& xt); |
|---|
| 153 | void do_unlock(); |
|---|
| 154 | void do_lock(cv_state& state); |
|---|
| 155 | void do_unlock(cv_state& state); |
|---|
| 156 | |
|---|
| 157 | #if defined(BOOST_HAS_WINTHREADS) |
|---|
| 158 | void* m_mutex; |
|---|
| 159 | unsigned long m_count; |
|---|
| 160 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 161 | pthread_mutex_t m_mutex; |
|---|
| 162 | pthread_cond_t m_unlocked; |
|---|
| 163 | pthread_t m_thread_id; |
|---|
| 164 | bool m_valid_id; |
|---|
| 165 | unsigned m_count; |
|---|
| 166 | #elif defined(BOOST_HAS_MPTASKS) |
|---|
| 167 | threads::mac::detail::scoped_critical_region m_mutex; |
|---|
| 168 | threads::mac::detail::scoped_critical_region m_mutex_mutex; |
|---|
| 169 | std::size_t m_count; |
|---|
| 170 | #endif |
|---|
| 171 | }; |
|---|
| 172 | #ifdef BOOST_MSVC |
|---|
| 173 | # pragma warning(pop) |
|---|
| 174 | #endif |
|---|
| 175 | } // namespace boost |
|---|
| 176 | |
|---|
| 177 | #endif // BOOST_RECURSIVE_MUTEX_WEK070601_HPP |
|---|
| 178 | |
|---|
| 179 | // Change Log: |
|---|
| 180 | // 8 Feb 01 WEKEMPF Initial version. |
|---|
| 181 | // 1 Jun 01 WEKEMPF Modified to use xtime for time outs. Factored out |
|---|
| 182 | // to three classes, mutex, try_mutex and timed_mutex. |
|---|
| 183 | // 11 Jun 01 WEKEMPF Modified to use PTHREAD_MUTEX_RECURSIVE if available. |
|---|
| 184 | // 3 Jan 03 WEKEMPF Modified for DLL implementation. |
|---|