[12] | 1 | // Copyright (C) 2001-2003 |
---|
| 2 | // William E. Kempf |
---|
| 3 | // |
---|
| 4 | // Permission to use, copy, modify, distribute and sell this software |
---|
| 5 | // and its documentation for any purpose is hereby granted without fee, |
---|
| 6 | // provided that the above copyright notice appear in all copies and |
---|
| 7 | // that both that copyright notice and this permission notice appear |
---|
| 8 | // in supporting documentation. William E. Kempf makes no representations |
---|
| 9 | // about the suitability of this software for any purpose. |
---|
| 10 | // It is provided "as is" without express or implied warranty. |
---|
| 11 | |
---|
| 12 | #ifndef BOOST_RECURSIVE_MUTEX_WEK070601_HPP |
---|
| 13 | #define BOOST_RECURSIVE_MUTEX_WEK070601_HPP |
---|
| 14 | |
---|
| 15 | #include <boost/thread/detail/config.hpp> |
---|
| 16 | |
---|
| 17 | #include <boost/utility.hpp> |
---|
| 18 | #include <boost/thread/detail/lock.hpp> |
---|
| 19 | |
---|
| 20 | #if defined(BOOST_HAS_PTHREADS) |
---|
| 21 | # include <pthread.h> |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | #if defined(BOOST_HAS_MPTASKS) |
---|
| 25 | # include "scoped_critical_region.hpp" |
---|
| 26 | #endif |
---|
| 27 | |
---|
| 28 | namespace boost { |
---|
| 29 | |
---|
| 30 | struct xtime; |
---|
| 31 | |
---|
| 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 | |
---|
| 173 | } // namespace boost |
---|
| 174 | |
---|
| 175 | #endif // BOOST_RECURSIVE_MUTEX_WEK070601_HPP |
---|
| 176 | |
---|
| 177 | // Change Log: |
---|
| 178 | // 8 Feb 01 WEKEMPF Initial version. |
---|
| 179 | // 1 Jun 01 WEKEMPF Modified to use xtime for time outs. Factored out |
---|
| 180 | // to three classes, mutex, try_mutex and timed_mutex. |
---|
| 181 | // 11 Jun 01 WEKEMPF Modified to use PTHREAD_MUTEX_RECURSIVE if available. |
---|
| 182 | // 3 Jan 03 WEKEMPF Modified for DLL implementation. |
---|