[29] | 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_XLOCK_WEK070601_HPP |
---|
| 8 | #define BOOST_XLOCK_WEK070601_HPP |
---|
| 9 | |
---|
| 10 | #include <boost/thread/detail/config.hpp> |
---|
| 11 | |
---|
| 12 | #include <boost/utility.hpp> |
---|
| 13 | #include <boost/thread/exceptions.hpp> |
---|
| 14 | |
---|
| 15 | namespace boost { |
---|
| 16 | |
---|
| 17 | class condition; |
---|
| 18 | struct xtime; |
---|
| 19 | |
---|
| 20 | namespace detail { namespace thread { |
---|
| 21 | |
---|
| 22 | template <typename Mutex> |
---|
| 23 | class lock_ops : private noncopyable |
---|
| 24 | { |
---|
| 25 | private: |
---|
| 26 | lock_ops() { } |
---|
| 27 | |
---|
| 28 | public: |
---|
| 29 | typedef typename Mutex::cv_state lock_state; |
---|
| 30 | |
---|
| 31 | static void lock(Mutex& m) |
---|
| 32 | { |
---|
| 33 | m.do_lock(); |
---|
| 34 | } |
---|
| 35 | static bool trylock(Mutex& m) |
---|
| 36 | { |
---|
| 37 | return m.do_trylock(); |
---|
| 38 | } |
---|
| 39 | static bool timedlock(Mutex& m, const xtime& xt) |
---|
| 40 | { |
---|
| 41 | return m.do_timedlock(xt); |
---|
| 42 | } |
---|
| 43 | static void unlock(Mutex& m) |
---|
| 44 | { |
---|
| 45 | m.do_unlock(); |
---|
| 46 | } |
---|
| 47 | static void lock(Mutex& m, lock_state& state) |
---|
| 48 | { |
---|
| 49 | m.do_lock(state); |
---|
| 50 | } |
---|
| 51 | static void unlock(Mutex& m, lock_state& state) |
---|
| 52 | { |
---|
| 53 | m.do_unlock(state); |
---|
| 54 | } |
---|
| 55 | }; |
---|
| 56 | |
---|
| 57 | template <typename Mutex> |
---|
| 58 | class scoped_lock : private noncopyable |
---|
| 59 | { |
---|
| 60 | public: |
---|
| 61 | typedef Mutex mutex_type; |
---|
| 62 | |
---|
| 63 | explicit scoped_lock(Mutex& mx, bool initially_locked=true) |
---|
| 64 | : m_mutex(mx), m_locked(false) |
---|
| 65 | { |
---|
| 66 | if (initially_locked) lock(); |
---|
| 67 | } |
---|
| 68 | ~scoped_lock() |
---|
| 69 | { |
---|
| 70 | if (m_locked) unlock(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void lock() |
---|
| 74 | { |
---|
| 75 | if (m_locked) throw lock_error(); |
---|
| 76 | lock_ops<Mutex>::lock(m_mutex); |
---|
| 77 | m_locked = true; |
---|
| 78 | } |
---|
| 79 | void unlock() |
---|
| 80 | { |
---|
| 81 | if (!m_locked) throw lock_error(); |
---|
| 82 | lock_ops<Mutex>::unlock(m_mutex); |
---|
| 83 | m_locked = false; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | bool locked() const { return m_locked; } |
---|
| 87 | operator const void*() const { return m_locked ? this : 0; } |
---|
| 88 | |
---|
| 89 | private: |
---|
| 90 | friend class boost::condition; |
---|
| 91 | |
---|
| 92 | Mutex& m_mutex; |
---|
| 93 | bool m_locked; |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | template <typename TryMutex> |
---|
| 97 | class scoped_try_lock : private noncopyable |
---|
| 98 | { |
---|
| 99 | public: |
---|
| 100 | typedef TryMutex mutex_type; |
---|
| 101 | |
---|
| 102 | explicit scoped_try_lock(TryMutex& mx) |
---|
| 103 | : m_mutex(mx), m_locked(false) |
---|
| 104 | { |
---|
| 105 | try_lock(); |
---|
| 106 | } |
---|
| 107 | scoped_try_lock(TryMutex& mx, bool initially_locked) |
---|
| 108 | : m_mutex(mx), m_locked(false) |
---|
| 109 | { |
---|
| 110 | if (initially_locked) lock(); |
---|
| 111 | } |
---|
| 112 | ~scoped_try_lock() |
---|
| 113 | { |
---|
| 114 | if (m_locked) unlock(); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void lock() |
---|
| 118 | { |
---|
| 119 | if (m_locked) throw lock_error(); |
---|
| 120 | lock_ops<TryMutex>::lock(m_mutex); |
---|
| 121 | m_locked = true; |
---|
| 122 | } |
---|
| 123 | bool try_lock() |
---|
| 124 | { |
---|
| 125 | if (m_locked) throw lock_error(); |
---|
| 126 | return (m_locked = lock_ops<TryMutex>::trylock(m_mutex)); |
---|
| 127 | } |
---|
| 128 | void unlock() |
---|
| 129 | { |
---|
| 130 | if (!m_locked) throw lock_error(); |
---|
| 131 | lock_ops<TryMutex>::unlock(m_mutex); |
---|
| 132 | m_locked = false; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | bool locked() const { return m_locked; } |
---|
| 136 | operator const void*() const { return m_locked ? this : 0; } |
---|
| 137 | |
---|
| 138 | private: |
---|
| 139 | friend class boost::condition; |
---|
| 140 | |
---|
| 141 | TryMutex& m_mutex; |
---|
| 142 | bool m_locked; |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | template <typename TimedMutex> |
---|
| 146 | class scoped_timed_lock : private noncopyable |
---|
| 147 | { |
---|
| 148 | public: |
---|
| 149 | typedef TimedMutex mutex_type; |
---|
| 150 | |
---|
| 151 | scoped_timed_lock(TimedMutex& mx, const xtime& xt) |
---|
| 152 | : m_mutex(mx), m_locked(false) |
---|
| 153 | { |
---|
| 154 | timed_lock(xt); |
---|
| 155 | } |
---|
| 156 | scoped_timed_lock(TimedMutex& mx, bool initially_locked) |
---|
| 157 | : m_mutex(mx), m_locked(false) |
---|
| 158 | { |
---|
| 159 | if (initially_locked) lock(); |
---|
| 160 | } |
---|
| 161 | ~scoped_timed_lock() |
---|
| 162 | { |
---|
| 163 | if (m_locked) unlock(); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | void lock() |
---|
| 167 | { |
---|
| 168 | if (m_locked) throw lock_error(); |
---|
| 169 | lock_ops<TimedMutex>::lock(m_mutex); |
---|
| 170 | m_locked = true; |
---|
| 171 | } |
---|
| 172 | bool try_lock() |
---|
| 173 | { |
---|
| 174 | if (m_locked) throw lock_error(); |
---|
| 175 | return (m_locked = lock_ops<TimedMutex>::trylock(m_mutex)); |
---|
| 176 | } |
---|
| 177 | bool timed_lock(const xtime& xt) |
---|
| 178 | { |
---|
| 179 | if (m_locked) throw lock_error(); |
---|
| 180 | return (m_locked = lock_ops<TimedMutex>::timedlock(m_mutex, xt)); |
---|
| 181 | } |
---|
| 182 | void unlock() |
---|
| 183 | { |
---|
| 184 | if (!m_locked) throw lock_error(); |
---|
| 185 | lock_ops<TimedMutex>::unlock(m_mutex); |
---|
| 186 | m_locked = false; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | bool locked() const { return m_locked; } |
---|
| 190 | operator const void*() const { return m_locked ? this : 0; } |
---|
| 191 | |
---|
| 192 | private: |
---|
| 193 | friend class boost::condition; |
---|
| 194 | |
---|
| 195 | TimedMutex& m_mutex; |
---|
| 196 | bool m_locked; |
---|
| 197 | }; |
---|
| 198 | |
---|
| 199 | } // namespace thread |
---|
| 200 | } // namespace detail |
---|
| 201 | } // namespace boost |
---|
| 202 | |
---|
| 203 | #endif // BOOST_XLOCK_WEK070601_HPP |
---|
| 204 | |
---|
| 205 | // Change Log: |
---|
| 206 | // 8 Feb 01 WEKEMPF Initial version. |
---|
| 207 | // 22 May 01 WEKEMPF Modified to use xtime for time outs. |
---|
| 208 | // 30 Jul 01 WEKEMPF Moved lock types into boost::detail::thread. Renamed |
---|
| 209 | // some types. Added locked() methods. |
---|