PATH:
opt
/
cpanel
/
ea-ruby27
/
src
/
passenger-release-6.1.2
/
src
/
cxx_supportlib
/
vendor-modified
/
boost
/
thread
// (C) Copyright 2012 Vicente J. Botet Escriba // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #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 { /** * Based on Associate Mutexes with Data to Prevent Races, By Herb Sutter, May 13, 2010 * http://www.drdobbs.com/windows/associate-mutexes-with-data-to-prevent-r/224701827?pgno=3 * * Make our mutex testable if it isn't already. * * Many mutex services (including boost::mutex) don't provide a way to ask, * "Do I already hold a lock on this mutex?" * Sometimes it is needed to know if a method like is_locked to be available. * This wrapper associates an arbitrary lockable type with a thread id that stores the ID of the thread that * currently holds the lockable. The thread id initially holds an invalid value that means no threads own the mutex. * 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. * */ template <typename Lockable> class testable_mutex { Lockable mtx_; atomic<thread::id> id_; public: /// the type of the wrapped lockable typedef Lockable lockable_type; /// Non copyable 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_; } // todo add the shared and upgrade mutex functions }; template <typename Lockable> struct is_testable_lockable : false_type {}; template <typename Lockable> struct is_testable_lockable<testable_mutex<Lockable> > : true_type {}; // /** // * Overloaded function used to check if the mutex is locked when it is testable and do nothing otherwise. // * // * This function is used usually to assert the pre-condition when the function can only be called when the mutex // * must be locked by the current thread. // */ // template <typename Lockable> // bool is_locked_by_this_thread(testable_mutex<Lockable> const& mtx) // { // return mtx.is_locked(); // } // template <typename Lockable> // bool is_locked_by_this_thread(Lockable const&) // { // return true; // } } #include <boost/config/abi_suffix.hpp> #endif // header
[-] caller_context.hpp
[edit]
[-] thread_time.hpp
[edit]
[-] thread_guard.hpp
[edit]
[+]
csbl
[-] scoped_thread.hpp
[edit]
[+]
concurrent_queues
[-] with_lock_guard.hpp
[edit]
[-] ostream_buffer.hpp
[edit]
[-] testable_mutex.hpp
[edit]
[-] lockable_traits.hpp
[edit]
[-] externally_locked.hpp
[edit]
[+]
v2
[-] lock_concepts.hpp
[edit]
[-] thread.hpp
[edit]
[-] is_locked_by_this_thread.hpp
[edit]
[-] locks.hpp
[edit]
[-] latch.hpp
[edit]
[-] barrier.hpp
[edit]
[-] condition.hpp
[edit]
[-] thread_pool.hpp
[edit]
[-] shared_lock_guard.hpp
[edit]
[-] cv_status.hpp
[edit]
[+]
..
[-] condition_variable.hpp
[edit]
[-] thread_only.hpp
[edit]
[-] exceptions.hpp
[edit]
[+]
executors
[-] sync_bounded_queue.hpp
[edit]
[-] poly_lockable.hpp
[edit]
[-] lock_guard.hpp
[edit]
[+]
experimental
[-] future.hpp
[edit]
[-] lock_types.hpp
[edit]
[+]
futures
[-] sync_queue.hpp
[edit]
[-] exceptional_ptr.hpp
[edit]
[-] lock_traits.hpp
[edit]
[-] mutex.hpp
[edit]
[-] lock_options.hpp
[edit]
[-] poly_lockable_adapter.hpp
[edit]
[-] poly_shared_lockable.hpp
[edit]
[-] interruption.hpp
[edit]
[-] poly_shared_lockable_adapter.hpp
[edit]
[-] synchronized_value.hpp
[edit]
[-] executor.hpp
[edit]
[-] completion_latch.hpp
[edit]
[-] lock_algorithms.hpp
[edit]
[-] tss.hpp
[edit]
[+]
detail
[+]
pthread
[-] recursive_mutex.hpp
[edit]
[-] lock_factories.hpp
[edit]
[-] user_scheduler.hpp
[edit]
[-] null_mutex.hpp
[edit]
[-] lockable_adapter.hpp
[edit]
[-] strict_lock.hpp
[edit]
[-] lockable_concepts.hpp
[edit]
[-] reverse_lock.hpp
[edit]
[-] shared_mutex.hpp
[edit]
[-] xtime.hpp
[edit]
[-] externally_locked_stream.hpp
[edit]
[-] once.hpp
[edit]
[-] thread_functors.hpp
[edit]