123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #ifndef BOOST_THREAD_TESTABLE_LOCKABLE_HPP
- #define BOOST_THREAD_TESTABLE_LOCKABLE_HPP
- #include <boost/thread/detail/config.hpp>
- #include <boost/thread/thread_only.hpp>
- #include <boost/atomic.hpp>
- #include <boost/assert.hpp>
- #include <boost/config/abi_prefix.hpp>
- namespace boost
- {
-
- template <typename Lockable>
- class testable_mutex
- {
- Lockable mtx_;
- atomic<thread::id> id_;
- public:
-
- typedef Lockable lockable_type;
-
- BOOST_THREAD_NO_COPYABLE(testable_mutex)
- testable_mutex() : id_(thread::id()) {}
- void lock()
- {
- BOOST_ASSERT(! is_locked_by_this_thread());
- mtx_.lock();
- id_ = this_thread::get_id();
- }
- void unlock()
- {
- BOOST_ASSERT(is_locked_by_this_thread());
- id_ = thread::id();
- mtx_.unlock();
- }
- bool try_lock()
- {
- BOOST_ASSERT(! is_locked_by_this_thread());
- if (mtx_.try_lock())
- {
- id_ = this_thread::get_id();
- return true;
- }
- else
- {
- return false;
- }
- }
- #ifdef BOOST_THREAD_USES_CHRONO
- template <class Rep, class Period>
- bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
- {
- BOOST_ASSERT(! is_locked_by_this_thread());
- if (mtx_.try_lock_for(rel_time))
- {
- id_ = this_thread::get_id();
- return true;
- }
- else
- {
- return false;
- }
- }
- template <class Clock, class Duration>
- bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time)
- {
- BOOST_ASSERT(! is_locked_by_this_thread());
- if (mtx_.try_lock_until(abs_time))
- {
- id_ = this_thread::get_id();
- return true;
- }
- else
- {
- return false;
- }
- }
- #endif
- bool is_locked_by_this_thread() const
- {
- return this_thread::get_id() == id_;
- }
- bool is_locked() const
- {
- return ! (thread::id() == id_);
- }
- thread::id get_id() const
- {
- return id_;
- }
-
- };
- template <typename Lockable>
- struct is_testable_lockable : false_type
- {};
- template <typename Lockable>
- struct is_testable_lockable<testable_mutex<Lockable> > : true_type
- {};
- }
- #include <boost/config/abi_suffix.hpp>
- #endif
|