| 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_MUTEX_WEK070601_HPP |
|---|
| 8 | #define BOOST_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 | |
|---|
| 33 | class BOOST_THREAD_DECL mutex |
|---|
| 34 | : private noncopyable |
|---|
| 35 | { |
|---|
| 36 | public: |
|---|
| 37 | friend class detail::thread::lock_ops<mutex>; |
|---|
| 38 | |
|---|
| 39 | typedef detail::thread::scoped_lock<mutex> scoped_lock; |
|---|
| 40 | |
|---|
| 41 | mutex(); |
|---|
| 42 | ~mutex(); |
|---|
| 43 | |
|---|
| 44 | private: |
|---|
| 45 | #if defined(BOOST_HAS_WINTHREADS) |
|---|
| 46 | typedef void* cv_state; |
|---|
| 47 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 48 | struct cv_state |
|---|
| 49 | { |
|---|
| 50 | pthread_mutex_t* pmutex; |
|---|
| 51 | }; |
|---|
| 52 | #elif defined(BOOST_HAS_MPTASKS) |
|---|
| 53 | struct cv_state |
|---|
| 54 | { |
|---|
| 55 | }; |
|---|
| 56 | #endif |
|---|
| 57 | void do_lock(); |
|---|
| 58 | void do_unlock(); |
|---|
| 59 | void do_lock(cv_state& state); |
|---|
| 60 | void do_unlock(cv_state& state); |
|---|
| 61 | |
|---|
| 62 | #if defined(BOOST_HAS_WINTHREADS) |
|---|
| 63 | void* m_mutex; |
|---|
| 64 | bool m_critical_section; |
|---|
| 65 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 66 | pthread_mutex_t m_mutex; |
|---|
| 67 | #elif defined(BOOST_HAS_MPTASKS) |
|---|
| 68 | threads::mac::detail::scoped_critical_region m_mutex; |
|---|
| 69 | threads::mac::detail::scoped_critical_region m_mutex_mutex; |
|---|
| 70 | #endif |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | class BOOST_THREAD_DECL try_mutex |
|---|
| 74 | : private noncopyable |
|---|
| 75 | { |
|---|
| 76 | public: |
|---|
| 77 | friend class detail::thread::lock_ops<try_mutex>; |
|---|
| 78 | |
|---|
| 79 | typedef detail::thread::scoped_lock<try_mutex> scoped_lock; |
|---|
| 80 | typedef detail::thread::scoped_try_lock<try_mutex> scoped_try_lock; |
|---|
| 81 | |
|---|
| 82 | try_mutex(); |
|---|
| 83 | ~try_mutex(); |
|---|
| 84 | |
|---|
| 85 | private: |
|---|
| 86 | #if defined(BOOST_HAS_WINTHREADS) |
|---|
| 87 | typedef void* cv_state; |
|---|
| 88 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 89 | struct cv_state |
|---|
| 90 | { |
|---|
| 91 | pthread_mutex_t* pmutex; |
|---|
| 92 | }; |
|---|
| 93 | #elif defined(BOOST_HAS_MPTASKS) |
|---|
| 94 | struct cv_state |
|---|
| 95 | { |
|---|
| 96 | }; |
|---|
| 97 | #endif |
|---|
| 98 | void do_lock(); |
|---|
| 99 | bool do_trylock(); |
|---|
| 100 | void do_unlock(); |
|---|
| 101 | void do_lock(cv_state& state); |
|---|
| 102 | void do_unlock(cv_state& state); |
|---|
| 103 | |
|---|
| 104 | #if defined(BOOST_HAS_WINTHREADS) |
|---|
| 105 | void* m_mutex; |
|---|
| 106 | bool m_critical_section; |
|---|
| 107 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 108 | pthread_mutex_t m_mutex; |
|---|
| 109 | #elif defined(BOOST_HAS_MPTASKS) |
|---|
| 110 | threads::mac::detail::scoped_critical_region m_mutex; |
|---|
| 111 | threads::mac::detail::scoped_critical_region m_mutex_mutex; |
|---|
| 112 | #endif |
|---|
| 113 | }; |
|---|
| 114 | |
|---|
| 115 | class BOOST_THREAD_DECL timed_mutex |
|---|
| 116 | : private noncopyable |
|---|
| 117 | { |
|---|
| 118 | public: |
|---|
| 119 | friend class detail::thread::lock_ops<timed_mutex>; |
|---|
| 120 | |
|---|
| 121 | typedef detail::thread::scoped_lock<timed_mutex> scoped_lock; |
|---|
| 122 | typedef detail::thread::scoped_try_lock<timed_mutex> scoped_try_lock; |
|---|
| 123 | typedef detail::thread::scoped_timed_lock<timed_mutex> scoped_timed_lock; |
|---|
| 124 | |
|---|
| 125 | timed_mutex(); |
|---|
| 126 | ~timed_mutex(); |
|---|
| 127 | |
|---|
| 128 | private: |
|---|
| 129 | #if defined(BOOST_HAS_WINTHREADS) |
|---|
| 130 | typedef void* cv_state; |
|---|
| 131 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 132 | struct cv_state |
|---|
| 133 | { |
|---|
| 134 | pthread_mutex_t* pmutex; |
|---|
| 135 | }; |
|---|
| 136 | #elif defined(BOOST_HAS_MPTASKS) |
|---|
| 137 | struct cv_state |
|---|
| 138 | { |
|---|
| 139 | }; |
|---|
| 140 | #endif |
|---|
| 141 | void do_lock(); |
|---|
| 142 | bool do_trylock(); |
|---|
| 143 | bool do_timedlock(const xtime& xt); |
|---|
| 144 | void do_unlock(); |
|---|
| 145 | void do_lock(cv_state& state); |
|---|
| 146 | void do_unlock(cv_state& state); |
|---|
| 147 | |
|---|
| 148 | #if defined(BOOST_HAS_WINTHREADS) |
|---|
| 149 | void* m_mutex; |
|---|
| 150 | #elif defined(BOOST_HAS_PTHREADS) |
|---|
| 151 | pthread_mutex_t m_mutex; |
|---|
| 152 | pthread_cond_t m_condition; |
|---|
| 153 | bool m_locked; |
|---|
| 154 | #elif defined(BOOST_HAS_MPTASKS) |
|---|
| 155 | threads::mac::detail::scoped_critical_region m_mutex; |
|---|
| 156 | threads::mac::detail::scoped_critical_region m_mutex_mutex; |
|---|
| 157 | #endif |
|---|
| 158 | }; |
|---|
| 159 | #ifdef BOOST_MSVC |
|---|
| 160 | # pragma warning(pop) |
|---|
| 161 | #endif |
|---|
| 162 | } // namespace boost |
|---|
| 163 | |
|---|
| 164 | // Change Log: |
|---|
| 165 | // 8 Feb 01 WEKEMPF Initial version. |
|---|
| 166 | // 22 May 01 WEKEMPF Modified to use xtime for time outs. Factored out |
|---|
| 167 | // to three classes, mutex, try_mutex and timed_mutex. |
|---|
| 168 | // 3 Jan 03 WEKEMPF Modified for DLL implementation. |
|---|
| 169 | |
|---|
| 170 | #endif // BOOST_MUTEX_WEK070601_HPP |
|---|