annotate DEPENDENCIES/generic/include/boost/thread/testable_mutex.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 // (C) Copyright 2012 Vicente J. Botet Escriba
Chris@16 2 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 3 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 4 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 5
Chris@16 6
Chris@16 7 #ifndef BOOST_THREAD_TESTABLE_LOCKABLE_HPP
Chris@16 8 #define BOOST_THREAD_TESTABLE_LOCKABLE_HPP
Chris@16 9
Chris@16 10 #include <boost/thread/detail/config.hpp>
Chris@16 11
Chris@16 12 #include <boost/thread/thread_only.hpp>
Chris@16 13
Chris@16 14 #include <boost/atomic.hpp>
Chris@16 15 #include <boost/assert.hpp>
Chris@16 16
Chris@16 17 #include <boost/config/abi_prefix.hpp>
Chris@16 18
Chris@16 19 namespace boost
Chris@16 20 {
Chris@16 21 /**
Chris@16 22 * Based on Associate Mutexes with Data to Prevent Races, By Herb Sutter, May 13, 2010
Chris@16 23 * http://www.drdobbs.com/windows/associate-mutexes-with-data-to-prevent-r/224701827?pgno=3
Chris@16 24 *
Chris@16 25 * Make our mutex testable if it isn't already.
Chris@16 26 *
Chris@16 27 * Many mutex services (including boost::mutex) don't provide a way to ask,
Chris@16 28 * "Do I already hold a lock on this mutex?"
Chris@16 29 * Sometimes it is needed to know if a method like is_locked to be available.
Chris@16 30 * This wrapper associates an arbitrary lockable type with a thread id that stores the ID of the thread that
Chris@16 31 * currently holds the lockable. The thread id initially holds an invalid value that means no threads own the mutex.
Chris@16 32 * When we acquire a lock, we set the thread id; and when we release a lock, we reset it back to its default no id state.
Chris@16 33 *
Chris@16 34 */
Chris@16 35 template <typename Lockable>
Chris@16 36 class testable_mutex
Chris@16 37 {
Chris@16 38 Lockable mtx_;
Chris@16 39 atomic<thread::id> id_;
Chris@16 40 public:
Chris@16 41 /// the type of the wrapped lockable
Chris@16 42 typedef Lockable lockable_type;
Chris@16 43
Chris@16 44 /// Non copyable
Chris@16 45 BOOST_THREAD_NO_COPYABLE(testable_mutex)
Chris@16 46
Chris@16 47 testable_mutex() : id_(thread::id()) {}
Chris@16 48
Chris@16 49 void lock()
Chris@16 50 {
Chris@16 51 BOOST_ASSERT(! is_locked_by_this_thread());
Chris@16 52 mtx_.lock();
Chris@16 53 id_ = this_thread::get_id();
Chris@16 54 }
Chris@16 55
Chris@16 56 void unlock()
Chris@16 57 {
Chris@16 58 BOOST_ASSERT(is_locked_by_this_thread());
Chris@16 59 id_ = thread::id();
Chris@16 60 mtx_.unlock();
Chris@16 61 }
Chris@16 62
Chris@16 63 bool try_lock()
Chris@16 64 {
Chris@16 65 BOOST_ASSERT(! is_locked_by_this_thread());
Chris@16 66 if (mtx_.try_lock())
Chris@16 67 {
Chris@16 68 id_ = this_thread::get_id();
Chris@16 69 return true;
Chris@16 70 }
Chris@16 71 else
Chris@16 72 {
Chris@16 73 return false;
Chris@16 74 }
Chris@16 75 }
Chris@16 76 #ifdef BOOST_THREAD_USES_CHRONO
Chris@16 77 template <class Rep, class Period>
Chris@16 78 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
Chris@16 79 {
Chris@16 80 BOOST_ASSERT(! is_locked_by_this_thread());
Chris@16 81 if (mtx_.try_lock_for(rel_time))
Chris@16 82 {
Chris@16 83 id_ = this_thread::get_id();
Chris@16 84 return true;
Chris@16 85 }
Chris@16 86 else
Chris@16 87 {
Chris@16 88 return false;
Chris@16 89 }
Chris@16 90 }
Chris@16 91 template <class Clock, class Duration>
Chris@16 92 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time)
Chris@16 93 {
Chris@16 94 BOOST_ASSERT(! is_locked_by_this_thread());
Chris@16 95 if (mtx_.try_lock_until(abs_time))
Chris@16 96 {
Chris@16 97 id_ = this_thread::get_id();
Chris@16 98 return true;
Chris@16 99 }
Chris@16 100 else
Chris@16 101 {
Chris@16 102 return false;
Chris@16 103 }
Chris@16 104 }
Chris@16 105 #endif
Chris@16 106
Chris@16 107 bool is_locked_by_this_thread() const
Chris@16 108 {
Chris@16 109 return this_thread::get_id() == id_;
Chris@16 110 }
Chris@16 111 bool is_locked() const
Chris@16 112 {
Chris@16 113 return ! (thread::id() == id_);
Chris@16 114 }
Chris@16 115
Chris@16 116 thread::id get_id() const
Chris@16 117 {
Chris@16 118 return id_;
Chris@16 119 }
Chris@16 120
Chris@16 121 // todo add the shared and upgrade mutex functions
Chris@16 122 };
Chris@16 123
Chris@16 124 template <typename Lockable>
Chris@16 125 struct is_testable_lockable : false_type
Chris@16 126 {};
Chris@16 127
Chris@16 128 template <typename Lockable>
Chris@16 129 struct is_testable_lockable<testable_mutex<Lockable> > : true_type
Chris@16 130 {};
Chris@16 131
Chris@16 132 // /**
Chris@16 133 // * Overloaded function used to check if the mutex is locked when it is testable and do nothing otherwise.
Chris@16 134 // *
Chris@16 135 // * This function is used usually to assert the pre-condition when the function can only be called when the mutex
Chris@16 136 // * must be locked by the current thread.
Chris@16 137 // */
Chris@16 138 // template <typename Lockable>
Chris@16 139 // bool is_locked_by_this_thread(testable_mutex<Lockable> const& mtx)
Chris@16 140 // {
Chris@16 141 // return mtx.is_locked();
Chris@16 142 // }
Chris@16 143 // template <typename Lockable>
Chris@16 144 // bool is_locked_by_this_thread(Lockable const&)
Chris@16 145 // {
Chris@16 146 // return true;
Chris@16 147 // }
Chris@16 148 }
Chris@16 149
Chris@16 150 #include <boost/config/abi_suffix.hpp>
Chris@16 151
Chris@16 152 #endif // header